Guest User

Untitled

a guest
Aug 27th, 2018
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. TLS issue when sending to gmail through JavaMail
  2. Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. l10sm302158wfk.21
  3. at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
  4. at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
  5. at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
  6. at SendEmail.main(SendEmail.java:47)
  7.  
  8. Properties props = new Properties();
  9. props.put("mail.transport.protocol", "smtp");
  10. props.put("mail.host", "smtp.gmail.com");
  11. props.put("mail.user", "blahblah@gmail.com");
  12. props.put("mail.password", "blah");
  13. props.put("mail.port", "587");
  14.  
  15. Session mailSession = Session.getDefaultInstance(props, null);
  16. Transport transport = mailSession.getTransport();
  17.  
  18. MimeMessage message = new MimeMessage(mailSession);
  19. message.setSubject("This is a test");
  20. message.setContent("This is a test", "text/plain");
  21. message.addRecipient(Message.RecipientType.TO, new InternetAddress("blahblah2@gmail.com"));
  22.  
  23. transport.connect();
  24. transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
  25. transport.close();
  26.  
  27. props.put("mail.smtp.starttls.enable", "true");
  28.  
  29. import java.util.*;
  30. import javax.mail.*;
  31. import javax.mail.internet.*;
  32. import javax.activation.*;
  33.  
  34. public class CopyOfSendMail {
  35.  
  36. private static String SMPT_HOSTNAME = "my smtp port no";
  37. private static String USERNAME = "root";
  38. private static String PASSWORD = "root";
  39.  
  40. public static void main(String[] args) {
  41. Properties props = new Properties();
  42. props.put("mail.smtp.host", SMPT_HOSTNAME);
  43. props.put("mail.from","aaa@gmail.com");
  44. props.put("mail.smtp.starttls.enable", "true");
  45. props.put("mail.smtp.auth", "true");
  46. props.put("mail.debug", "true");
  47.  
  48. Session session = Session.getInstance(props, new Authenticator() {
  49. @Override
  50. protected PasswordAuthentication getPasswordAuthentication() {
  51. return new PasswordAuthentication(USERNAME, PASSWORD);
  52. }
  53. });
  54. try {
  55. MimeMessage msg = new MimeMessage(session);
  56. msg.setFrom();
  57. msg.setRecipients(Message.RecipientType.TO,
  58. "bbb@gmail.com");
  59. msg.setSubject("JavaMail hello world example");
  60. msg.setSentDate(new Date());
  61. msg.setText("Hello, world!n");
  62. Transport.send(msg);
  63. } catch (MessagingException mex) {
  64. System.out.println("send failed, exception: " + mex);
  65. }
  66. }
  67. }
Add Comment
Please, Sign In to add comment