Advertisement
Guest User

Untitled

a guest
Dec 8th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. @Service
  2. public class MailService {
  3.  
  4.     public static void sendMailSSL(){
  5.         final String username = "plesa.dummy@gmail.com";
  6.         final String password = "Aceasta este parola";
  7.  
  8.         System.out.println("Sending mail...");
  9.         Properties props = new Properties();
  10.  
  11.         props.put("mail.smtp.ssl.enable", "true");
  12.         props.put("mail.smtp.ssl.socketFactory.fallback", "false");
  13.         props.put("mail.smtp.host", "smtp.gmail.com");
  14.         props.put("mail.smtp.socketFactory.port", "465");
  15.         props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  16.         props.put("mail.smtp.auth", "true");
  17.         props.put("mail.smtp.port", "465");
  18.  
  19.         Session session = Session.getDefaultInstance(props,
  20.                 new javax.mail.Authenticator() {
  21.                     protected PasswordAuthentication getPasswordAuthentication() {
  22.                         return new PasswordAuthentication(username,password);
  23.                     }
  24.                 });
  25.  
  26.         try {
  27.  
  28.             Message message = new MimeMessage(session);
  29.             message.setFrom(new InternetAddress(username));
  30.             message.setRecipients(Message.RecipientType.TO,
  31.                     InternetAddress.parse("plesha.gabi@gmail.com"));
  32.             message.setSubject("Testing Subject");
  33.             message.setText("Dear Mail Crawler," +
  34.                     "\n\n No spam to my email, please!");
  35.  
  36.             Transport.send(message);
  37.  
  38.             System.out.println("Done");
  39.  
  40.         } catch (MessagingException e) {
  41.             throw new RuntimeException(e);
  42.         }
  43.     }
  44.  
  45.    
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement