Advertisement
Guest User

Untitled

a guest
Jun 1st, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1.     public static void sendGmail(String dest, String subject, String msgText) {
  2.         final String username = "adresse@gmail.com";
  3.         final String password = "password";
  4.    
  5.         Properties props = new Properties();
  6.         props.put("mail.smtp.host", "smtp.gmail.com");
  7.         props.put("mail.smtp.socketFactory.port", "465");
  8.         props.put("mail.smtp.socketFactory.class",
  9.                 "javax.net.ssl.SSLSocketFactory");
  10.         props.put("mail.smtp.auth", "true");
  11.         props.put("mail.smtp.port", "465");
  12.  
  13.    
  14.         Session session = Session.getInstance(props,
  15.               new javax.mail.Authenticator() {
  16.                 protected PasswordAuthentication getPasswordAuthentication() {
  17.                     return new PasswordAuthentication(username, password);
  18.                 }
  19.               });
  20.    
  21.         try {
  22.             Message message = new MimeMessage(session);
  23.             message.setHeader("Content-Type", "text/html; charset=UTF-8");
  24.             message.setFrom(new InternetAddress("Hotel Ocean Beach" + " <" + username + ">"));
  25.             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(dest));
  26.             message.setSubject(subject);
  27.             message.setContent(msgText, "text/html; charset=UTF-8");
  28.      
  29.             Transport.send(message);
  30.    
  31.         } catch (MessagingException e) {
  32.  
  33.             throw new RuntimeException(e);
  34.         }
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement