Advertisement
Guest User

Untitled

a guest
Oct 30th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1.  
  2. package mailMessage;
  3. import javax.mail.Message;
  4. import javax.mail.MessagingException;
  5. import javax.mail.PasswordAuthentication;
  6. import javax.mail.Session;
  7. import javax.mail.Transport;
  8. import javax.mail.internet.InternetAddress;
  9. import javax.mail.internet.MimeMessage;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12. import java.util.*;
  13.  
  14.  
  15. public class EmailMessage {
  16. private String from;
  17. //private LinkedList<String> to = new LinkedList<String>();
  18. private String to;
  19. private String subject;
  20. private String content;
  21. private String mimeType;
  22. private LinkedList<String> cc;
  23. private LinkedList<String> bcc;
  24.  
  25.  
  26.  
  27. //private at least, in order to prevent running constructor from out of class
  28. private EmailMessage(){};
  29.  
  30. private EmailMessage(Builder bilder){
  31. if (bilder == null)
  32. return;
  33. else{
  34. from = bilder.from;
  35. to = bilder.to;
  36. subject = bilder.subject;
  37. content = bilder.content;
  38. mimeType = bilder.mimeType;
  39. cc = bilder.cc;
  40. bcc = bilder.bcc;
  41. }
  42. }
  43.  
  44. public void sendMail(EmailMessage ms){
  45. final String username = ms.getFrom();
  46. final String password = "bigheart01";
  47.  
  48. Properties props = new Properties();
  49. props.put("mail.smtp.auth", "true");
  50. props.put("mail.smtp.starttls.enable", "true");
  51. props.put("mail.smtp.host", "smtp.gmail.com");
  52. props.put("mail.smtp.port", "587");
  53.  
  54. Session session = Session.getInstance(props,
  55. new javax.mail.Authenticator() {
  56. protected PasswordAuthentication getPasswordAuthentication() {
  57. return new PasswordAuthentication(username, password);
  58. }
  59. });
  60. try{
  61. MimeMessage message = new MimeMessage(session);
  62. message.setFrom(new InternetAddress(ms.getFrom()));
  63. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(ms.getTo()));
  64. message.setSubject(ms.getSubject());
  65. message.setText(ms.getContent());
  66.  
  67. if (ms.getFrom() == "Invalid email address" || ms.getTo() == "Invalid email address")
  68. System.out.println("Could not send. Check email address.");
  69. else{
  70. Transport.send(message);
  71. System.out.println("Sent succesfully.");
  72. }
  73. }catch (MessagingException mex) {mex.printStackTrace();}
  74. }
  75.  
  76. public String getFrom(){
  77. return from;
  78. }
  79. public String getTo(){
  80. return to;
  81. }
  82. public String getSubject(){
  83. return subject;
  84. }
  85. public String getContent(){
  86. return content;
  87. }
  88. public String getmimeType(){
  89. return mimeType;
  90. }
  91. public LinkedList<String> getCC(){
  92. return cc;
  93. }
  94. public LinkedList<String> getBCC(){
  95. return bcc;
  96. }
  97.  
  98. public static class Builder{
  99. private String from;
  100. private String to;
  101. private String subject;
  102. private String content;
  103. private String mimeType;
  104. private LinkedList<String> cc;
  105. private LinkedList<String> bcc;
  106.  
  107. protected Builder(){}
  108.  
  109. public Builder fromWho(String from){
  110. Pattern p = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
  111. + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
  112. Matcher matcher = p.matcher(from);
  113. if (!matcher.matches()){
  114. System.out.println("Invalid email address");
  115. this.from = "Invalid email address";
  116. return this;
  117. }else{
  118. this.from = from;
  119. return this;
  120. }
  121. }
  122. public Builder toWho(String who){
  123. // for (String address : who){
  124. // to.add(address);
  125. // }
  126. Pattern p = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
  127. + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
  128. Matcher matcher = p.matcher(who);
  129. if (!matcher.matches()){
  130. System.out.println("Invalid email address");
  131. this.to = "Invalid email address";
  132. return this;
  133. }else{
  134. this.to = who;
  135. return this;
  136. }
  137. }
  138. public Builder whatSubject(String subject){
  139. this.subject = subject;
  140. return this;
  141. }
  142. public Builder whatContent(String content){
  143. this.content = content;
  144. return this;
  145. }
  146. public Builder whatMimeType(String mimeType){
  147. this.mimeType = mimeType;
  148. return this;
  149. }
  150. public Builder WhatCC(LinkedList<String> cc){
  151. this.cc = cc;
  152. return this;
  153. }
  154. public Builder WhatBCC(LinkedList<String> bcc){
  155. this.bcc = bcc;
  156. return this;
  157. }
  158. public EmailMessage build(){
  159. return new EmailMessage(this);
  160. }
  161. }
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. //second file
  173. public class MainMessage {
  174. public static void main(String[] argv){
  175. EmailMessage mail = new EmailMessage.Builder()
  176. .fromWho("belabot01@gmail.com")
  177. .toWho("belabot01@gmail.com")
  178. .whatSubject("heehe działa")
  179. .whatContent("pozdrawiam")
  180. .build();
  181. mail.sendMail(mail);
  182.  
  183. // System.out.println(" MAIL:");
  184. // System.out.println("FROM: " + mail.getFrom());
  185. // System.out.println("TO: " + mail.getTo());
  186. // System.out.println("SUBJECT: " + mail.getSubject());
  187. // System.out.println("CONTENT: " + mail.getContent());
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement