首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

spring mail发送邮件有关配置与实现

2012-10-19 
spring mail发送邮件相关配置与实现1.spring相关配置bean idmailSender??valuesmtp.qq.com/prope

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();
??}

?

热点排行