Guest User

Untitled

a guest
Jul 28th, 2018
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. How to verify an email address really exists by sending a mail in java
  2. package csv;
  3. import javax.mail.PasswordAuthentication;
  4. import java.util.Properties;
  5. import javax.activation.DataHandler;
  6. import javax.activation.DataSource;
  7. import javax.activation.FileDataSource;
  8. import javax.mail.BodyPart;
  9. import javax.mail.Message;
  10. import javax.mail.MessagingException;
  11. import javax.mail.Multipart;
  12. import javax.mail.Session;
  13. import javax.mail.Transport;
  14. import javax.mail.internet.AddressException;
  15. import javax.mail.internet.InternetAddress;
  16. import javax.mail.internet.MimeBodyPart;
  17. import javax.mail.internet.MimeMessage;
  18. import javax.mail.internet.MimeMultipart;
  19.  
  20. public class email {
  21.  
  22. public void send(String recipeintEmail,
  23. String subject,
  24. String messageText,String[] attachments)
  25. throws MessagingException, AddressException {
  26. /*
  27. It is a good practice to put this in a java.util.Properties
  28. file and encrypt password. Scroll down
  29. to comments below to see
  30. how to use java.util.Properties in JSF context.
  31. */
  32. String senderEmail = "our email address";
  33. String senderMailPassword = "password";
  34. String gmail = "smtp.gmail.com";
  35.  
  36. Properties props = System.getProperties();
  37.  
  38. props.put("mail.smtp.user", senderEmail);
  39. props.put("mail.smtp.host", "smtp.gmail.com");
  40. props.put("mail.smtp.port", "465");
  41. props.put("mail.smtp.starttls.enable", "true");
  42. props.put("mail.smtp.debug", "true");
  43. props.put("mail.smtp.auth", "true");
  44. props.put("mail.smtp.socketFactory.port", "465");
  45. props.put("mail.smtp.socketFactory.class",
  46. "javax.net.ssl.SSLSocketFactory");
  47. props.put("mail.smtp.socketFactory.fallback", "false");
  48.  
  49. // Required to avoid security exception.
  50. email.MyAuthenticator authentication =
  51. new email.MyAuthenticator(senderEmail,senderMailPassword);
  52. Session session =
  53. Session.getDefaultInstance(props,authentication);
  54. session.setDebug(true);
  55.  
  56. MimeMessage message = new MimeMessage(session);
  57.  
  58. BodyPart messageBodyPart = new MimeBodyPart();
  59. messageBodyPart.setText(messageText);
  60.  
  61. // Add message text
  62. Multipart multipart = new MimeMultipart();
  63. multipart.addBodyPart(messageBodyPart);
  64.  
  65. // Attachments should reside in your server.
  66. // Example "c:file.txt" or "/home/user/photo.jpg"
  67.  
  68. for (int i=0; i < attachments.length; i++) {
  69.  
  70. messageBodyPart = new MimeBodyPart();
  71. DataSource source = new FileDataSource(attachments[i]);
  72. messageBodyPart.setDataHandler(new DataHandler(source));
  73. messageBodyPart.setFileName(attachments [i]);
  74. multipart.addBodyPart(messageBodyPart) ;
  75. }
  76.  
  77.  
  78.  
  79. message.setContent(multipart);
  80. message.setSubject(subject);
  81. message.setFrom(new InternetAddress(senderEmail));
  82. message.addRecipient(Message.RecipientType.TO,
  83. new InternetAddress(recipeintEmail));
  84.  
  85. Transport transport = session.getTransport("smtps");
  86. transport.connect(gmail,465, senderEmail, senderMailPassword);
  87. transport.sendMessage(message, message.getAllRecipients());
  88.  
  89. transport.close();
  90.  
  91. }
  92.  
  93. private class MyAuthenticator extends javax.mail.Authenticator {
  94. String User;
  95. String Password;
  96. public MyAuthenticator (String user, String password) {
  97. User = user;
  98. Password = password;
  99. }
  100.  
  101. @Override
  102. public PasswordAuthentication getPasswordAuthentication() {
  103. return new javax.mail.PasswordAuthentication(User, Password);
  104. }
  105. }
  106.  
  107.  
  108. public static void main(String args[]) throws MessagingException
  109. {
  110. // email e=new email();
  111. // String at[]={"c:/COPYRIGHT.txt"};
  112. // e.send("xyz@gmail.com", "hello","test" )");
  113. }
  114.  
  115. }
Add Comment
Please, Sign In to add comment