Guest User

Untitled

a guest
Sep 17th, 2018
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. send mail from yahoo id to other email ids using Javamail API
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. import java.util.Properties;
  5.  
  6. public class MailExample {
  7. private static final String SMTP_HOST_NAME = "smtp.mail.yahoo.com";
  8. private static final int SMTP_HOST_PORT = 587;//465,587,25
  9. private static final String SMTP_AUTH_USER = "dummyrls@yahoo.com";
  10. private static final String SMTP_AUTH_PWD = "my password";
  11.  
  12. public static void main(String[] args) throws Exception{
  13. new MailExample().test();
  14. }
  15.  
  16. public void test() throws Exception{
  17. Properties props = new Properties();
  18.  
  19. props.put("mail.transport.protocol", "smtp");
  20. props.put("mail.smtp.host", SMTP_HOST_NAME);
  21. props.put("mail.smtp.auth", "true");
  22. // props.put("mail.smtps.quitwait", "false");
  23.  
  24. Session mailSession = Session.getDefaultInstance(props);
  25. mailSession.setDebug(true);
  26. Transport transport = mailSession.getTransport();
  27.  
  28. MimeMessage message = new MimeMessage(mailSession);
  29. message.setSubject("Testing SMTP-SSL");
  30. message.setContent("This is a test", "text/plain");
  31.  
  32. message.addRecipient(Message.RecipientType.TO,
  33. new InternetAddress("rlss@abc.com"));
  34.  
  35. transport.connect
  36. (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
  37.  
  38. transport.sendMessage(message,
  39. message.getRecipients(Message.RecipientType.TO));
  40. transport.close();
  41. }
  42. }
  43.  
  44. DEBUG: setDebug: JavaMail version 1.4.1 DEBUG: getProvider()
  45. returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,
  46. Sun Microsystems, Inc.,1.4.1] DEBUG SMTP: useEhlo true,
  47. useAuth true
  48. DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.com", port 587,
  49. isSSL false Exception in thread "main"
  50. javax.mail.MessagingException: Could not connect to SMTP
  51. host: smtp.mail.yahoo.com, port: 587; nested exception is:
  52. java.net.ConnectException: Connection timed out: connect
  53. at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
  54. at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
  55. at javax.mail.Service.connect(Service.java:288)
  56. at com.sample.mailexample.MailExample.test(MailExample.java:313)
  57. at com.sample.mailexample.MailExample.main(MailExample.java:291) Caused by:
  58. java.net.ConnectException: Connection timed out: connect
  59. at java.net.PlainSocketImpl.socketConnect(Native Method)
  60. at java.net.PlainSocketImpl.doConnect(Unknown Source)
  61. at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
  62. at java.net.PlainSocketImpl.connect(Unknown Source)
  63. at java.net.SocksSocketImpl.connect(Unknown Source)
  64. at java.net.Socket.connect(Unknown Source)
  65. at java.net.Socket.connect(Unknown Source)
  66. at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
  67. at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
  68. at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
  69. ... 4 more
  70.  
  71. public class SendMail {
  72.  
  73. String host, port, emailid,username, password;
  74. Properties props = System.getProperties();
  75. Session l_session = null;
  76.  
  77. public BSendMail() {
  78. host = "smtp.mail.yahoo.com";
  79. port = "587";
  80. emailid = "a@yahoo.com";
  81. username = "a";
  82. password = "pwd";
  83.  
  84. emailSettings();
  85. createSession();
  86. sendMessage("a@yahoo.com", "rahul@gmail.com","Test","test Mail");
  87. }
  88.  
  89. public void emailSettings() {
  90. props.put("mail.smtp.host", host);
  91. props.put("mail.smtp.auth", "true");
  92. props.put("mail.debug", "false");
  93. props.put("mail.smtp.port", port);
  94. // props.put("mail.smtp.socketFactory.port", port);
  95. // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  96. // props.put("mail.smtp.socketFactory.fallback", "false");
  97.  
  98. }
  99.  
  100. public void createSession() {
  101.  
  102. l_session = Session.getInstance(props,
  103. new javax.mail.Authenticator() {
  104. protected PasswordAuthentication getPasswordAuthentication() {
  105. return new PasswordAuthentication(username, password);
  106. }
  107. });
  108.  
  109. l_session.setDebug(true); // Enable the debug mode
  110.  
  111. }
  112.  
  113. public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) {
  114. //System.out.println("Inside sendMessage 2 :: >> ");
  115. try {
  116. //System.out.println("Sending Message *********************************** ");
  117. MimeMessage message = new MimeMessage(l_session);
  118. emailid = emailFromUser;
  119. //System.out.println("mail id in property ============= >>>>>>>>>>>>>> " + emailid);
  120. //message.setFrom(new InternetAddress(emailid));
  121. message.setFrom(new InternetAddress(this.emailid));
  122.  
  123. message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
  124. message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail));
  125. message.setSubject(subject);
  126. message.setContent(msg, "text/html");
  127.  
  128. //message.setText(msg);
  129. Transport.send(message);
  130. System.out.println("Message Sent");
  131. } catch (MessagingException mex) {
  132. mex.printStackTrace();
  133. } catch (Exception e) {
  134. e.printStackTrace();
  135. }//end catch block
  136. return true;
  137. }
  138.  
  139. }
Add Comment
Please, Sign In to add comment