Guest User

Untitled

a guest
Jan 5th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.Properties;
  3.  
  4. import javax.mail.Message;
  5. import javax.mail.MessagingException;
  6. import javax.mail.PasswordAuthentication;
  7. import javax.mail.Transport;
  8. import javax.mail.internet.AddressException;
  9. import javax.mail.internet.InternetAddress;
  10. import javax.mail.internet.MimeMessage;
  11.  
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.core.io.ClassPathResource;
  14. import org.springframework.core.io.Resource;
  15. import org.springframework.core.io.support.PropertiesLoaderUtils;
  16. import org.springframework.mail.MailSender;
  17. import org.springframework.stereotype.Service;
  18. @Service("mailSender")
  19. public class ApplicationMailer {
  20.  
  21. private static javax.mail.Session session=null;
  22. private static Resource resource = new ClassPathResou
  23.  
  24. rce("resources/mail.properties");
  25.  
  26. static {
  27. try {
  28. final Properties props = PropertiesLoaderUtils.loadProperties(resource);
  29. if (props!=null)
  30. {
  31. session = javax.mail.Session
  32. .getInstance(props, new javax.mail.Authenticator() {
  33. protected PasswordAuthentication getPasswordAuthentication() {
  34. return new PasswordAuthentication(props.getProperty("email.username"),
  35. props.getProperty("email.password"));
  36. }
  37.  
  38. });
  39. }
  40. } catch (IOException e) {
  41. session=null;
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46.  
  47. @Autowired
  48. private MailSender mailSender;
  49.  
  50.  
  51. public MailSender getMailSender() {
  52. return mailSender;
  53. }
  54.  
  55. public void setMailSender(MailSender mailSender) {
  56. this.mailSender = mailSender;
  57. }
  58.  
  59. /**
  60. * This method will send compose and send the message
  61. * @throws IOException
  62. * @throws MessagingException
  63. * @throws AddressException
  64. * */
  65. public void sendMail(String to, String subject, String body) throws IOException, AddressException, MessagingException{
  66. Message mail = new MimeMessage(session);
  67. final Properties props = PropertiesLoaderUtils.loadProperties(resource);
  68. mail.setFrom(new InternetAddress(props.getProperty("mail.from")));
  69. mail.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  70. mail.setSubject(subject);
  71. mail.setContent(body, "text/html");
  72. Transport.send(mail);
  73.  
  74. }
  75. }
  76.  
  77. mail.debug=true
  78. mail.from=*****
  79. email.username=******
  80. email.password=******
  81.  
  82. mail.smtp.host=smtps.aruba.it
  83. mail.smtp.port=465
  84. mail.smtp.localhost=localhost
  85. mail.mime.charset=UTF-8
  86. mail.smtp.auth=true
  87.  
  88. mail.smtp.ssl.enable=true
  89. mail.transport.protocol=smtps
  90.  
  91. <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  92. <property name="host" value="smtps.aruba.it" />
  93. <property name="port" value="465" />
  94. <property name="protocol" value="smtps" />
  95. <property name="username" value="*****" />
  96. <property name="password" value="*****" />
  97. <property name="javaMailProperties">
  98. <props>
  99. <prop key="mail.transport.protocol">smtps</prop>
  100. <prop key="mail.smtp.auth">true</prop>
  101. <prop key="mail.smtp.starttls.enable">true</prop>
  102. <prop key="mail.debug">true</prop>
  103. <prop key="mail.smtp.localhost">localhost</prop>
  104. <prop key="mail.mime.charset">UTF-8</prop>
  105. <!-- <prop key="mail.smtps.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> -->
  106. <prop key="mail.smtps.ssl.enable">false</prop>
  107. </props>
  108. </property>
  109. </bean>
Add Comment
Please, Sign In to add comment