Au printemps, cela devrait être fait de cette façon :
Votre classe de courriel :
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class HTMLMail
{
private JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void sendMail(String from, String to, String subject, String msg) {
try {
MimeMessage message = mailSender.createMimeMessage();
message.setSubject(subject);
MimeMessageHelper helper;
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setText(msg, true);
mailSender.send(message);
} catch (MessagingException ex) {
Logger.getLogger(HTMLMail.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
haricots : (Spring-Mail.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="youremail@gmail.com" />
<property name="password" value="yourpassword" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
<bean id="htmlMail" class="com.mohi.common.HTMLMail">
<property name="mailSender" ref="mailSender" />
</bean>
</beans>
Utilisation :
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Mail.xml");
HTMLMail mm = (HTMLMail) context.getBean("htmlMail");
String html="<p>Hi!</p><a href=\"google.com\">Link text</a>";
mm.sendMail("sender@gmail.com",
"receiver@gmail.com",
"test html email",
html);
Exemple complet aquí .