Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. static Properties properties = new Properties();
  2. {
  3. properties.put("mail.smtp.host", getText("smtp.host"));
  4. properties.put("mail.smtp.socketFactory.port", getText("socketFactory.port"));
  5. properties.put("mail.smtp.socketFactory.class", getText("socketFactory.class"));
  6. properties.put("mail.smtp.auth", getText("smtp.auth"));
  7. properties.put("mail.smtp.port", getText("smtp.port"));
  8.  
  9. properties.put("mail.smtp.starttls.enable", "true");
  10. }
  11.  
  12. private String from = getText("from");
  13. private String smtppw = getText("pw");
  14.  
  15. private void initMS() {
  16. if (smtppw != null) {
  17. mailsession = Session.getDefaultInstance(properties, new Authenticator() {
  18.  
  19. protected PasswordAuthentication getPasswordAuthentication() {
  20. return new PasswordAuthentication(from, smtppw);
  21. }
  22. });
  23. }
  24. else {
  25. mailsession = Session.getDefaultInstance(properties, new Authenticator() {
  26. protected PasswordAuthentication getPasswordAuthentication() {
  27. return new PasswordAuthentication(from, null);
  28. }
  29. });
  30. }
  31. }
  32.  
  33.  
  34.  
  35. String attachpath = ServletActionContext.getServletContext().getRealPath("/");
  36.  
  37. public String sendNote(String type, SendingParamsDTO sendingParams, String filename) throws /*AddressException,*/ MessagingException {
  38.  
  39. String res = SUCCESS;
  40. //try {
  41.  
  42. if (mailsession == null) initMS();
  43.  
  44. Message message = new MimeMessage(mailsession);
  45.  
  46. InternetAddress pobox = new InternetAddress(from);
  47. InternetAddress to[] = InternetAddress.parse(sendingParams.getMailto());
  48.  
  49. message.setFrom(pobox);
  50. message.setRecipients( Message.RecipientType.TO, to);
  51. message.addHeader("Disposition-Notification-To", from);
  52. message.setSubject("[AN-V.C.O.] - "+sendingParams.getSubject() );
  53. message.setSentDate( sendingParams.getDate() );
  54.  
  55. /**/
  56. logger.info(" ## FROM: "+from);
  57. logger.info(" ## TO: "+to);
  58. logger.info(" ## SUBJECT: "+sendingParams.getSubject());
  59. logger.info(" ## MESSAGE: "+sendingParams.getMessage());
  60. if (filename != null)
  61. logger.info(" ## ATTACH: "+attachpath + filename);
  62. /**/
  63.  
  64.  
  65. MimeMultipart multipart = new MimeMultipart("related");
  66. //Multipart multipart = new MimeMultipart();
  67.  
  68. BodyPart messageBodyPart1 = new MimeBodyPart();
  69.  
  70. if (type.equals("text"))
  71. messageBodyPart1.setText(sendingParams.getMessage());
  72. else if (type.equals("html"))
  73. messageBodyPart1.setContent(sendingParams.getMessage(), "text/html");
  74.  
  75.  
  76. BodyPart messageBodyPart2 = null;
  77. if (filename != null){
  78. DataSource source = new FileDataSource(attachpath);
  79. messageBodyPart2 = new MimeBodyPart();
  80. messageBodyPart2.setDataHandler( new DataHandler(source) );
  81. messageBodyPart2.setFileName( filename );
  82. }
  83.  
  84.  
  85. multipart.addBodyPart( messageBodyPart1 );
  86. if (filename != null){
  87. multipart.addBodyPart( messageBodyPart2 );
  88. }
  89.  
  90.  
  91. message.setContent(multipart);
  92.  
  93.  
  94. Transport.send(message, message.getRecipients(Message.RecipientType.TO));
  95. //Transport transport = mailsession.getTransport();
  96. //transport.connect();
  97. //transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
  98. //transport.close();
  99.  
  100. //}
  101. //catch(Exception e) {
  102. // res = ERROR;
  103. // e.printStackTrace();
  104. //}
  105.  
  106. return res;
  107. }
  108.  
  109. ## generic using GMAIL
  110. smtp.host=smtp.gmail.com
  111. socketFactory.port=465
  112. socketFactory.class=javax.net.ssl.SSLSocketFactory
  113. smtp.auth=true
  114. smtp.port=465
  115.  
  116. ## real using myprovider
  117. smtp.host=smtp.myprovider.com
  118. #socketFactory.port=465
  119. #socketFactory.class=javax.net.ssl.SSLSocketFactory
  120. smtp.auth=true
  121. smtp.port=465
  122.  
  123. static Properties properties = new Properties();
  124. {
  125. properties.put("mail.smtp.host", getText("smtp.host"));
  126. //properties.put("mail.smtp.socketFactory.port", getText("socketFactory.port"));
  127. //properties.put("mail.smtp.socketFactory.class", getText("socketFactory.class"));
  128. properties.put("mail.smtp.auth", getText("smtp.auth"));
  129. properties.put("mail.smtp.port", getText("smtp.port"));
  130.  
  131. //properties.put("mail.smtp.starttls.enable", "true");
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement