nasrax

Sending mail Using JavaMail to Yahoo and Gmail accounts

Oct 4th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. //For more codes visit : http://quamiller.com/5W5x
  2.  
  3. import java.io.File;
  4. import java.security.Security;
  5. import java.util.Properties;
  6.  
  7. import javax.activation.DataHandler;
  8. import javax.activation.DataSource;
  9. import javax.activation.FileDataSource;
  10. import javax.mail.Message;
  11. import javax.mail.MessagingException;
  12. import javax.mail.Multipart;
  13. import javax.mail.PasswordAuthentication;
  14. import javax.mail.Session;
  15. import javax.mail.Transport;
  16. import javax.mail.internet.InternetAddress;
  17. import javax.mail.internet.MimeBodyPart;
  18. import javax.mail.internet.MimeMessage;
  19. import javax.mail.internet.MimeMultipart;
  20.  
  21. public class GoogleTest {
  22.  
  23. private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  24. private static final String SMTP_PORT = "465";
  25. private static final String emailMsgTxt = "Test Message Contents";
  26. private static final String emailSubjectTxt = "A test from gmail";
  27. private static final String emailFromAddress = "xxx@gmail.com";
  28. private static final String SSL_FACTORY =
  29. "javax.net.ssl.SSLSocketFactory";
  30. private static final String[] sendTo = {"xxx@gmail.com","xxx@yahoo.com"};
  31.  
  32. private static final String fileAttachment="D:\hai.txt";
  33.  
  34. public static void main(String args[]) throws Exception {
  35.  
  36. Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  37.  
  38. new GoogleTest().sendSSLMessage(sendTo, emailSubjectTxt,
  39. emailMsgTxt, emailFromAddress);
  40. System.out.println("Sucessfully Sent mail to All Users");
  41.  
  42. }
  43.  
  44. public void sendSSLMessage(String recipients[], String subject,
  45. String message, String from) throws MessagingException {
  46. boolean debug = true;
  47.  
  48. Properties props = new Properties();
  49. props.put("mail.smtp.host", SMTP_HOST_NAME);
  50. props.put("mail.smtp.auth", "true");
  51. props.put("mail.debug", "true");
  52. props.put("mail.smtp.port", SMTP_PORT);
  53. props.put("mail.smtp.socketFactory.port", SMTP_PORT);
  54. props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
  55. props.put("mail.smtp.socketFactory.fallback", "false");
  56.  
  57. Session session = Session.getDefaultInstance(props,
  58. new javax.mail.Authenticator() {
  59.  
  60. protected PasswordAuthentication getPasswordAuthentication() {
  61. return new PasswordAuthentication("xxx@gmail.com", "give password of
  62. gmail");
  63. }
  64. });
  65.  
  66.  
  67.  
  68. MimeMessage message1 =
  69. new MimeMessage(session);
  70. message1.setFrom(
  71. new InternetAddress(from));
  72. message1.addRecipient(
  73. Message.RecipientType.TO,
  74. new InternetAddress(recipients[0]));
  75.  
  76. message1.addRecipient(
  77. Message.RecipientType.TO,
  78. new InternetAddress(recipients[1]));
  79.  
  80. message1.setSubject(
  81. "Hello JavaMail Attachment");
  82.  
  83. // create the message part
  84. MimeBodyPart messageBodyPart =
  85. new MimeBodyPart();
  86.  
  87. //fill message
  88. messageBodyPart.setText("Hi");
  89.  
  90. Multipart multipart = new MimeMultipart();
  91. multipart.addBodyPart(messageBodyPart);
  92.  
  93. // Part two is attachment
  94. messageBodyPart = new MimeBodyPart();
  95. DataSource source =
  96. new FileDataSource(fileAttachment);
  97. messageBodyPart.setDataHandler(
  98. new DataHandler(source));
  99. messageBodyPart.setFileName(fileAttachment);
  100. multipart.addBodyPart(messageBodyPart);
  101.  
  102. // Put parts in message
  103. message1.setContent(multipart);
  104.  
  105. // Send the message
  106. Transport.send( message1 );
  107. }
  108. }
Add Comment
Please, Sign In to add comment