Guest User

Untitled

a guest
Sep 28th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. package sendmail;
  2.  
  3. import javax.mail.*;
  4. import javax.mail.internet.InternetAddress;
  5. import javax.mail.internet.MimeMessage;
  6. import java.util.Properties;
  7.  
  8. /**
  9. * Created by caesar-84 on 9/28/16.
  10. */
  11. public class SslGmailSender
  12. {
  13. private String username;
  14. private String password;
  15. private Properties props;
  16.  
  17. public SslGmailSender(String username, String password)
  18. {
  19. this.username = username;
  20. this.password = password;
  21.  
  22. props = new Properties();
  23. props.put("mail.smtp.host", "smtp.gmail.com");
  24. props.put("mail.smtp.socketFactory.port", "465");
  25. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  26. props.put("mail.smtp.auth", "true");
  27. props.put("mail.smtp.port", "465");
  28. }
  29.  
  30. public void send(String subject, String text, String from, String to)
  31. {
  32. Session thisSession = Session.getInstance(props, new Authenticator()
  33. {
  34. public PasswordAuthentication getPasswordAuthentification()
  35. {
  36. return new PasswordAuthentication(SslGmailSender.this.username, SslGmailSender.this.password);
  37. }
  38. });
  39. try
  40. {
  41. MimeMessage message = new MimeMessage(thisSession);
  42. message.setFrom(new InternetAddress(from));
  43. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  44. message.setSubject(subject);
  45. message.setText(text);
  46.  
  47. Transport.send(message);
  48. System.out.println("Yor e-mail has been sent.");
  49. }
  50. catch (MessagingException ex)
  51. {
  52. ex.printStackTrace();
  53. }
  54. }
  55.  
  56. public SslGmailSender(String username, String password)
  57. {
  58. this.username = username;
  59. this.password = password;
  60.  
  61. props = new Properties();
  62. props.put("mail.smtp.host", "smtp.gmail.com");
  63. props.put("mail.smtp.socketFactory.port", "465");
  64. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  65. props.put("mail.smtp.auth", "true");
  66. props.put("mail.smtp.port", "465");
  67. }
  68.  
  69. public void send(String subject, String text, String from, String to)
  70. {
  71. Session thisSession = Session.getInstance(props, new Authenticator()
  72. {
  73. public PasswordAuthentication getPasswordAuthentification()
  74. {
  75. return new PasswordAuthentication(SslGmailSender.this.username, SslGmailSender.this.password);
  76. }
  77. });
  78. try
  79. {
  80. MimeMessage message = new MimeMessage(thisSession);
  81. message.setFrom(new InternetAddress(from));
  82. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  83. message.setSubject(subject);
  84. message.setText(text);
  85.  
  86. Transport.send(message);
  87. System.out.println("Yor e-mail has been sent.");
  88. }
  89. catch (MessagingException ex)
  90. {
  91. ex.printStackTrace();
  92. }
  93. }
  94.  
  95. package sendmail;
  96.  
  97. import java.io.BufferedReader;
  98. import java.io.IOException;
  99. import java.io.InputStreamReader;
  100.  
  101. /**
  102. * Created by caesar-84 on 9/28/16.
  103. */
  104. public class Main
  105. {
  106. public static void main(String[] args)
  107. {
  108. String username = "";
  109. String password = "";
  110. String subject = "";
  111. String text = "";
  112. String to = "";
  113. try(BufferedReader console = new BufferedReader(new InputStreamReader(System.in)))
  114. {
  115. System.out.print("Your e-mail: ");
  116. username = console.readLine();
  117. System.out.print("Password: ");
  118. password = console.readLine();
  119. System.out.print("Recipient: ");
  120. to = console.readLine();
  121. System.out.print("Message subject: ");
  122. subject = console.readLine();
  123. System.out.println("Text to send (type "-END" to finish):");
  124. String typedText = "";
  125. StringBuilder sb = new StringBuilder();
  126. while (!typedText.contains("-END"))
  127. {
  128. typedText = console.readLine();
  129. sb.append(typedText).append("n");
  130. }
  131. sb.delete(sb.length() - 4, sb.length());
  132. text = sb.toString();
  133.  
  134. }
  135. catch (IOException ex){ex.getStackTrace();}
  136.  
  137. String from = username;
  138. SslGmailSender thisSender = new SslGmailSender(username, password);
  139. thisSender.send(subject, text, from, to);
  140. }
  141.  
  142. }
  143. catch (IOException ex){ex.getStackTrace();}
  144.  
  145. String from = username;
  146. SslGmailSender thisSender = new SslGmailSender(username, password);
  147. thisSender.send(subject, text, from, to);
  148. }
Add Comment
Please, Sign In to add comment