Guest User

Untitled

a guest
Jul 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. package at.stadtverwaltung.server;
  2.  
  3. import java.io.File;
  4. import java.util.Properties;
  5.  
  6. import javax.mail.Message;
  7. import javax.mail.MessagingException;
  8. import javax.mail.Multipart;
  9. import javax.mail.Session;
  10. import javax.mail.Transport;
  11. import javax.mail.internet.InternetAddress;
  12. import javax.mail.internet.MimeBodyPart;
  13. import javax.mail.internet.MimeMessage;
  14. import javax.mail.internet.MimeMultipart;
  15.  
  16. import at.stadtverwaltung.client.Text;
  17. import at.stadtverwaltung.client.presenter.rpc.EmailService;
  18.  
  19. import com.extjs.gxt.ui.client.widget.MessageBox;
  20.  
  21. public class EmailServiceImpl extends DatabaseServiceImpl implements EmailService {
  22.  
  23. private static final long serialVersionUID = 1L;
  24.  
  25. @Override
  26. public boolean sendMail(String from, String[] to, String[] cc, String[] bcc, String subject, String text) {
  27. // TODO Auto-generated method stub
  28. return false;
  29. }
  30.  
  31. private static void sendMailByJavaMail(String smtpHost, String from, String[] to, String[] cc, String[] bcc, String subject, String text, File[] attachments) {
  32.  
  33. /**
  34. * Set up the mail
  35. */
  36.  
  37. int nrOfAttachments = attachments.length;
  38.  
  39. String smtpUser = "";
  40. String smtpPW = "";
  41. boolean useSmtpAuthentication = false;
  42.  
  43. try {
  44. // Get system properties and session
  45. Properties props = System.getProperties();
  46. Session session = Session.getInstance(props);
  47.  
  48. // Define message and it's parts
  49. MimeMessage message = new MimeMessage(session);
  50. MimeBodyPart textPart = new MimeBodyPart();
  51. MimeBodyPart[] attachmentPart = new MimeBodyPart[nrOfAttachments];
  52. for (int i = 0; i < nrOfAttachments; i++)
  53. attachmentPart[i] = new MimeBodyPart();
  54.  
  55. // Build multipart message
  56. Multipart multipart = new MimeMultipart();
  57. multipart.addBodyPart(textPart);
  58. for (int i = 0; i < nrOfAttachments; i++)
  59. multipart.addBodyPart(attachmentPart[i]);
  60. message.setContent(multipart);
  61.  
  62. /**
  63. * Fill the message properties
  64. */
  65.  
  66. // Set the SMTP Host
  67. props.put("mail.smtp.host", smtpHost);
  68.  
  69. // We want to close the connection immediately after sending
  70. props.put("mail.smtp.quitwait", "false");
  71.  
  72. // We want to use encryption if needed
  73. props.put("mail.smtp.starttls.enable", "true");
  74. props.put("mail.smtp.ssl.protocols", "SSLv3 TLSv1");
  75.  
  76. // If authentication is needed we set it to true
  77. if (useSmtpAuthentication) props.put("mail.smtp.auth", "true");
  78. else props.put("mail.smtp.auth", "false");
  79.  
  80. // Set all recipients of any kind (TO, CC, BCC)
  81. message.setFrom(new InternetAddress(from));
  82. for (int i = 0; i < to.length; i++) {
  83. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
  84. }
  85. for (int i = 0; i < cc.length; i++) {
  86. message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
  87. }
  88. for (int i = 0; i < bcc.length; i++) {
  89. message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
  90. }
  91.  
  92. // Set subject, text and attachment
  93. message.setSubject(subject);
  94. textPart.setText(text);
  95.  
  96. for (int i = 0; i < nrOfAttachments; i++) {
  97. attachmentPart[i++].attachFile(attachments[i]);
  98. }
  99.  
  100. /**
  101. * Send message (if no authentication is used, we use the short variant to send a mail
  102. */
  103.  
  104. if (useSmtpAuthentication) {
  105. Transport transport = session.getTransport("smtp");
  106. try {
  107. transport.connect(smtpHost, smtpUser, smtpPW);
  108. transport.sendMessage(message, message.getAllRecipients());
  109. }
  110. finally {
  111. transport.close();
  112. }
  113. }
  114. else { // No SMTP Authentication
  115. Transport.send(message);
  116. }
  117.  
  118. }
  119.  
  120. catch (Exception e) {
  121. e.printStackTrace();
  122. if (e instanceof MessagingException) { // SMTP Error
  123. MessageBox.alert(Text.ERROR, Text.ERROR_MAIL, null);
  124. e.printStackTrace();
  125. }
  126. else { // Other Error
  127. MessageBox.alert(Text.ERROR, Text.ERROR_MAIL, null);
  128. e.printStackTrace();
  129. }
  130. }
  131. }
  132.  
  133. }
Add Comment
Please, Sign In to add comment