Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import javax.mail.Message;
  2. import javax.mail.Session;
  3. import javax.mail.Transport;
  4. import javax.mail.internet.InternetAddress;
  5. import javax.mail.internet.MimeMessage;
  6.  
  7. private static void sendMail() throws Exception {
  8. String smtpServer = "hogehoge.smtp.server.com";
  9. int port = "587";
  10. String userid = "from@gmail.com";
  11. String password = "password";
  12. String contentType = "text/plain";
  13. String subject = "Test Subject";
  14. String from = "from@gmail.com";
  15. String to = "to@gmail.com";
  16. String bounceAddr = "from@gmail.com";
  17. String body = "send mail";
  18.  
  19. Properties props = new Properties();
  20.  
  21. props.put("mail.transport.protocol", "smtp");
  22. props.put("mail.smtp.auth", "true");
  23. props.put("mail.smtp.starttls.enable","true");
  24. props.put("mail.smtp.host", smtpServer);
  25. props.put("mail.smtp.from", bounceAddr);
  26.  
  27. Session mailSession = Session.getInstance(props);
  28. mailSession.setDebug(true);
  29.  
  30. MimeMessage message = new MimeMessage(mailSession);
  31. message.addFrom(InternetAddress.parse(from));
  32. message.setRecipients(Message.RecipientType.TO, to);
  33. message.setSubject(subject);
  34. message.setContent(body, contentType);
  35.  
  36. Transport transport = mailSession.getTransport();
  37. try{
  38. System.out.println("Sending ....");
  39. transport.connect(smtpServer, port, userid, password);
  40. transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
  41. System.out.println("Sending done ...");
  42. }
  43. catch(Exception e) {
  44. System.err.println("Error Sending: ");
  45. e.printStackTrace();
  46. }
  47. transport.close();
  48. }
  49.  
  50. $ printf "%s%s%s" from@gmail.com from@gmail.com password | openssl base64 -e | tr -d 'n'; echo
  51. ZnJvbUBnbWFpbC5jb20AZnJvbUBnbWFpbC5jb20AcGFzc3dvcmQ=
  52.  
  53. $ telnet hogehoge.smtp.server.com 587
  54. Trying xxx.xxx.xxx.xxx...
  55. Connected to hogehoge.smtp.server.com
  56. Escape character is '^]'.
  57. 220 smtp.server.com ESMTP
  58. EHLO localhost // my type
  59.  
  60. 250-smtp.server.com
  61. ...
  62. 250 AUTH LOGIN PLAIN CRAM-MD5
  63. AUTH PLAIN ZnJvbUBnbWFpbC5jb20AZnJvbUBnbWFpbC5jb20AcGFzc3dvcmQ= // my type
  64. 235 ok, go ahead (#2.0.0)
  65.  
  66. MAIL FROM:<from@gmail.com> // my type
  67. 250 ok
  68.  
  69. RCPT TO:<to@gmail.com> // my type
  70. 250 ok
  71.  
  72. DATA // my type
  73. 354 go ahead
  74.  
  75. subject:Test Mail // my type
  76. from:User Mail <spoof@gmail.com> // my type. set envelope from mail address.
  77. to:to@gmail.com // my type
  78. // my type
  79. send mail. // my type
  80. . // my type
  81. 250 ok 1481706367 qp 12070
  82.  
  83. quit // my type
  84. 221 smtp.server.com
  85. Connection closed by foreign host.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement