How to configure spring to send SMTP emails via annotations without using XML?

I need to send SMTP messages, but I would like to avoid using XML to configure spring services and use annotations.

How to configure the entire SMTP sender and use it only with code and annotations?

thank

+3
source share
2 answers

Using org.springframework.mail.javamail.MimeMessageHelper, for example:

JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");

MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo("test@host.com");
helper.setText("Thank you for ordering!");

sender.send(message);

Is this what you were looking for? Adapted from http://static.springsource.org/spring/docs/3.0.x/reference/mail.html , please look there to see some more advanced samples. (Attachments, etc.)

+5
source

Use Autowiring

@Autowire
private JavaMailSender mailSender;

and in XML add the following lines:

   <context:annotation-config/> 
<context:component-scan base-package="org.springframework.mail.javamail"/>

.

+2

All Articles