spring mail发送邮件相关配置与实现
1.spring相关配置
<bean id="mailSender"
??value="smtp.qq.com"></property>
??<property name="javaMailProperties">
???<props>
????<prop key="mail.smtp.auth">true</prop>
????<prop key="mail.smtp.timeout">20000</prop>
???</props>
??</property>
??<property name="username" value="91517511@qq.com"></property>
??<property name="password" value="123456987"></property>
?</bean>
2.代码实现
2.1发送简单文本邮件
BeanFactory bf = new ClassPathXmlApplicationContext(
????"applicationContext-mail.xml");
??MailSender ms = (MailSender) bf.getBean("mailSender");
??SimpleMailMessage smm = new SimpleMailMessage();
??smm.setTo(toEmail);
??smm.setSubject(title);
??try {
???smm.setText(text);
??} catch (Exception e) {
???e.printStackTrace();
??}
??ms.send(smm);
2.2发送html邮件
BeanFactory bf = new ClassPathXmlApplicationContext(
????"applicationContext-mail.xml");
??JavaMailSender jms = (JavaMailSender) bf.getBean("mailSender");
??MimeMessage mailMessage = jms.createMimeMessage();
??try {
???MimeMessageHelper messageHelper = new MimeMessageHelper(
?????mailMessage, true, "utf-8");
???messageHelper.setTo(toEmail);
???messageHelper.setSubject(title);
???messageHelper.setText(html文本, true);
???jms.send(mailMessage);
??} catch (MailException e) {
???e.printStackTrace();
??} catch (SecurityException e) {
???e.printStackTrace();
??} catch (javax.mail.MessagingException e) {
???e.printStackTrace();
??}
?