首页 / 邮件 / SpringMVC 实现邮件发送功能
SpringMVC 实现邮件发送功能
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了SpringMVC 实现邮件发送功能,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5329字,纯文字阅读大概需要8分钟。
内容图文
配置spring-mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName">
<!-- 实现邮件服务 -->
<bean id="mimeMessage" class="javax.mail.internet.MimeMessage"
factory-bean="javaMailSender" factory-method="createMimeMessage" />
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.163.com" />
<property name="username" value="hongliang_0510@163.com" /> //你的邮箱
<property name="password" value="***********" />//邮箱密码
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.host">smtp.163.com</prop>
<prop key="mail.smtp.timeout">25000</prop>
<prop key="mail.smtp.port">25</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
</props>
</property>
</bean>
<bean id="mailService" class="core.mail.service.impl.MailServiceImpl">
<property name="mailSender" ref="javaMailSender" />
<property name="mimeMessage" ref="mimeMessage" />
</bean></beans>
--邮件Module实体类
public class MailModel {
private String fromAddress;//发送人地址1个
private String toAddresses;//接收人地址,可以为很多个,每个地址之间用";"分隔,比方说450065208@qq.com;lpf@sina.com
private String subject;//邮件主题
private String content;//邮件文本内容
private String[] attachFileNames;//附件
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getToAddresses() {
return toAddresses;
}
public void setToAddresses(String toAddresses) {
this.toAddresses = toAddresses;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public void setAttachFileNames(String[] attachFileNames) {
this.attachFileNames = attachFileNames;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
}
--service 接口
public interface MailService {
public void sendAttachMail(MailModel mail);
}
--service实现类
@Service
public class MailServiceImpl implements MailService{
@Resource
private JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
private MimeMessage mimeMessage;
public void setMimeMessage(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}
private static Logger logger = Logger.getLogger(MailServiceImpl.class);
/**
* 发送html格式的,带附件的邮件
*/
@Override
public void sendAttachMail(MailModel mail) {
try {
MimeMessageHelper mailMessage = new MimeMessageHelper(
this.mimeMessage, true, "UTF-8");
mailMessage.setFrom(mail.getFromAddress());// 设置邮件消息的发送者
mailMessage.setSubject(mail.getSubject());// 设置邮件消息的主题
mailMessage.setSentDate(new Date());// 设置邮件消息发送的时间
mailMessage.setText(mail.getContent(), true); // 设置邮件正文,true表示以html的格式发送
String[] toAddresses = mail.getToAddresses().split(";");// 得到要发送的地址数组
for (int i = 0; i < toAddresses.length; i++) {
mailMessage.setTo(toAddresses[i]);
/* for (String fileName : mail.getAttachFileNames()) {
mailMessage.addAttachment(fileName, new File(fileName));
}*/
// 发送邮件
this.mailSender.send(this.mimeMessage);
logger.info("send mail ok=" + toAddresses[i]);
}
} catch (Exception e) {
logger.error(e);
e.printStackTrace();
}
}
}
--controller中的发邮件的方法
@RequestMapping(value = "/sendEmail.do")
@ResponseBody
public AjaxUtil sendEmail(HttpServletRequest request) {
String title = request.getParameter("title");
String userName= request.getParameter("userName");
String email = request.getParameter("email");
String advice = request.getParameter("advice");
MailModel mail = new MailModel();
mail.setFromAddress("hongliang_0510@163.com");
mail.setToAddresses("71006957@qq.com");
mail.setSubject(title);
StringBuffer sb = new StringBuffer();
sb.append("姓名:"+userName+"<br/>");
sb.append("邮箱:"+email+"<br/>");
sb.append("建议:"+advice+"<br/>");
mail.setContent(sb.toString());
AjaxUtil ajax=new AjaxUtil();
try{
mailService.sendAttachMail(mail);
}catch(Exception e){
ajax = new AjaxUtil(e.getMessage());
}
return ajax;
}
原文:http://www.cnblogs.com/hongliang-0510/p/5437971.html
内容总结
以上是互联网集市为您收集整理的SpringMVC 实现邮件发送功能全部内容,希望文章能够帮你解决SpringMVC 实现邮件发送功能所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。