Advertisement
Guest User

My email class

a guest
Mar 23rd, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. package net.mcviral.dev.plugins.servermanager.util;
  2.  
  3. import java.util.Properties;
  4. import javax.mail.Authenticator;
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.PasswordAuthentication;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12. //import javax.mail.internet.MimeMessage.RecipientType;
  13.  
  14. public class Email {
  15.  
  16. private String smtpServer;
  17. private String username;
  18. private String password;
  19. private String to;
  20. private String subject;
  21. private String body;
  22.  
  23. public Email withSMTPServer(String smtpServer)
  24. {
  25. this.smtpServer = smtpServer;
  26. return this;
  27. }
  28.  
  29. public Email withUsername(String username)
  30. {
  31. this.username = username;
  32. return this;
  33. }
  34.  
  35. public Email withPassword(String password)
  36. {
  37. this.password = password;
  38. return this;
  39. }
  40.  
  41. public Email withTo(String to)
  42. {
  43. this.to = to;
  44. return this;
  45. }
  46.  
  47. public Email withSubject(String subject)
  48. {
  49. this.subject = subject;
  50. return this;
  51. }
  52.  
  53. public Email withBody(String body)
  54. {
  55. this.body = body;
  56. return this;
  57. }
  58.  
  59. public void send()
  60. {
  61. new Thread(new Runnable()
  62. {
  63. public void run()
  64. {
  65. try{
  66. Properties props = new Properties();
  67. props.put("mail.smtp.host", Email.this.smtpServer);
  68. //props.put("mail.smtp.socketFactory.port", "465");
  69. props.put("mail.smtp.socketFactory.port", "587");
  70. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  71. props.put("mail.smtp.auth", "true");
  72. //props.put("mail.smtp.port", "465");
  73. props.put("mail.smtp.port", "587");
  74. Session session = Session.getInstance(
  75. props,
  76. new Authenticator()
  77. {
  78. protected PasswordAuthentication getPasswordAuthentication()
  79. {
  80. return new PasswordAuthentication(Email.this.username, Email.this.password);
  81. }
  82. });
  83.  
  84. Message message = new MimeMessage(session);
  85. message.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse(Email.this.to));
  86. message.setSubject(Email.this.subject);
  87. message.setText(Email.this.body);
  88. Transport.send(message);
  89. }
  90. catch (MessagingException e)
  91. {
  92. e.printStackTrace();
  93. }catch(Exception e){
  94. e.printStackTrace();
  95. }
  96. }
  97. }).start();
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement