Advertisement
Guest User

GarciaPL

a guest
Sep 10th, 2014
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. import javax.mail.*;
  2. import javax.mail.internet.InternetAddress;
  3. import javax.mail.internet.MimeMessage;
  4.  
  5. private void sendEmail() {
  6.  
  7.         String sender = "[email protected]";
  8.         String receiver = "[email protected]";
  9.         String title = "YOUR_TITLE_TEXT";
  10.         String body = "YOUR_BODY_TEXT";
  11.  
  12.         Properties props = new Properties();
  13.         props.put("mail.transport.protocol", "smtp");
  14.         props.put("mail.smtp.host", "smtp.gmail.com");
  15.         props.put("mail.smtp.port", "25");
  16.         props.put("mail.smtp.starttls.enable", "true");
  17.         props.put("mail.smtp.EnableSSL.enable", "true");
  18.         props.put("mail.smtp.auth", "true");
  19.         props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  20.         props.setProperty("mail.smtp.socketFactory.fallback", "false");
  21.         props.setProperty("mail.smtp.port", "465");
  22.         props.setProperty("mail.smtp.socketFactory.port", "465");
  23.  
  24.         Authenticator authenticator = new Authenticator() {
  25.             protected PasswordAuthentication getPasswordAuthentication() {
  26.                 return new PasswordAuthentication("[email protected]", "password");
  27.             }
  28.         };
  29.  
  30.         Session session = Session.getDefaultInstance(props, authenticator);
  31.  
  32.         try {
  33.             Message msg = new MimeMessage(session);
  34.             msg.setFrom(new InternetAddress(sender));
  35.             msg.addRecipient(Message.RecipientType.TO,
  36.                     new InternetAddress(receiver, receiver));
  37.             msg.setSubject(title);
  38.             msg.setText(body);
  39.             Transport.send(msg);
  40.         } catch (MessagingException e) {
  41.             System.out.println("sendEmail (MessagingException) : " + e.getMessage());
  42.             e.printStackTrace();
  43.         } catch (UnsupportedEncodingException e) {
  44.             System.out.println("sendEmail (UnsupportedEncodingException) : " + e.getMessage());
  45.             e.printStackTrace();
  46.         } catch (Exception e) {
  47.             System.out.println("sendEmail (Exception) : " + e.getMessage());
  48.             e.printStackTrace();
  49.         }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement