SpringMVC 實現郵件發送功能


配置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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM