Guest User

Untitled

a guest
May 8th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. import javax.mail.*;
  2. import javax.mail.internet.*;
  3. import javax.mail.Authenticator;
  4. import javax.mail.PasswordAuthentication;
  5. import java.util.Properties;
  6. public class sendMailing{
  7. private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  8. private static final String SMTP_AUTH_USER = "username";//Give your gmail id
  9. private static final String SMTP_AUTH_PWD = "******";give ur gmail password
  10.  
  11. public static void main(String[] args) throws Exception{
  12. new sendMailing().test();
  13. }
  14.  
  15. public void test() throws Exception{
  16.  
  17. Properties props = new Properties();
  18. props.put("mail.transport.protocol", "smtp");
  19. props.put("mail.smtp.host", SMTP_HOST_NAME);
  20. props.put("mail.smtp.port","465");
  21. props.put("mail.smtp.starttls.enable","true");
  22. props.put("mail.smtp.auth", "true");
  23. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  24. props.put("mail.smtp.socketFactory.fallback", "false");
  25. Authenticator auth = new SMTPAuthenticator();
  26. Session mailSession = Session.getInstance(props, auth);
  27.  
  28. // uncomment for debugging infos to stdout
  29. mailSession.setDebug(true);
  30. Transport transport = mailSession.getTransport();
  31. MimeMessage message = new MimeMessage(mailSession);
  32. message.setSubject("Test mail");
  33. message.setContent("Hi" ,"text/plain");
  34. message.setFrom(new InternetAddress(from));
  35. message.addRecipient(Message.RecipientType.TO,
  36. new InternetAddress(to));
  37. transport.connect();
  38. transport.sendMessage(message,
  39. message.getRecipients(Message.RecipientType.TO));
  40. transport.close();
  41. }
  42.  
  43. private class SMTPAuthenticator extends javax.mail.Authenticator {
  44. public PasswordAuthentication getPasswordAuthentication() {
  45. String username = SMTP_AUTH_USER;
  46. String password = SMTP_AUTH_PWD;
  47. return new PasswordAuthentication(username, password);
  48. }
  49. }
  50. }
Add Comment
Please, Sign In to add comment