Konfiguracja Springowego “wysyłacza maili”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com" /> <property name="username" value="xxx@gmail.com" /> <property name="password" value="xxx" /> <property name="port" value="587" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.from">xxx@gmail.com</prop> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.starttls.enable">true</prop> <prop key="mail.debug">true</prop> </props> </property> </bean> <bean class="services.MailService" id="mailService" scope="singleton" > <property name="mailSender" ref="mailSender" /> </bean> |
Klasa usługi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package services; import java.io.Serializable; import org.apache.log4j.Logger; import org.springframework.mail.MailException; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; public class MailService implements Serializable { private static final long serialVersionUID = 1L; private Logger log = Logger.getLogger(getClass()); /** * springowy wysylacz maili */ private MailSender mailSender; public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } public boolean sendEmail(String email, String message, String subject) { SimpleMailMessage msg = new SimpleMailMessage(); msg.setTo(email); msg.setSubject(subject); msg.setText(message); try { this.mailSender.send(msg); return true; } catch (MailException e) { log.error(e.getMessage(), e); } return false; } } |