1,Emaill类:
package com.learn.jsp.pojo;
/**
* 邮件基本信息
* @author kevin
*
*/
public class Email {
private String emailFrom ;
private String emailTo;
private String emailSubject;
private String emailContent;
public String getEmailContent() {
return emailContent;
}
public String getEmailFrom() {
return emailFrom;
}
public String getEmailSubject() {
return emailSubject;
}
public String getEmailTo() {
return emailTo;
}
public void setEmailContent(String emailContent) {
this.emailContent = emailContent;
}
public void setEmailFrom(String emailFrom) {
this.emailFrom = emailFrom;
}
public void setEmailSubject(String emailSubject) {
this.emailSubject = emailSubject;
}
public void setEmailTo(String emailTo) {
this.emailTo = emailTo;
}
}
2,EmailThread:
package com.learn.jsp.pojo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
public class EmailThread implements Runnable {
private Email email;
private JavaMailSender emailSender;
public EmailThread(Email email, JavaMailSender emailSender) {
super();
this.email = email;
this.emailSender = emailSender;
}
public EmailThread() {
super();
}
public void run() {
//发送条件不足
if (email == null || emailSender == null) {
System.out.println("发送条件不足发送失败~~~");
return;
}
SimpleMailMessage mail = new SimpleMailMessage();
boolean isOk = true;
try {
if (email != null) {
if (StringUtils.isNotEmpty(email.getEmailFrom())) {
mail.setFrom(email.getEmailFrom());// 发送者
} else {
isOk = false;
}
if (StringUtils.isNotEmpty(email.getEmailTo())) {
mail.setTo(email.getEmailTo());// 接受者
} else {
isOk = false;
}
if (StringUtils.isNotEmpty(email.getEmailSubject())) {
mail.setSubject(email.getEmailSubject());// 主题
} else {
isOk = false;
}
if (StringUtils.isNotEmpty(email.getEmailContent())) {
mail.setText(email.getEmailContent());// 邮件内容
} else {
isOk = false;
}
} else {
isOk = false;
}
if (!isOk) {
throw new Exception("内容为空~~-发送失败");
}
// 发送邮件
emailSender.send(mail);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
3基础配置:
email.properties
mail.smtp.host=smtp.qq.com
mail.smtp.auth=true
mail.smtp.timeout=50000
mail.smtp.username=xxxxxxx@qq.com
mail.smtp.password=独立密码
mail.smtp.port=465
mail.smtp.starttls.enable=true
4,配置spring bean
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:email.properties" />
<bean id="emailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}" />
<property name="port" value="${mail.smtp.port}" />
<property name="username" value="${mail.smtp.username}" />
<property name="password" value="${mail.smtp.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
<prop key="mail.smtp.socketFactory.port">${mail.smtp.port}</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
</props>
</property>
</bean>
5,controller中注入发送器:
@Autowired
@Qualifier("emailSender")
private JavaMailSender emailSender;
6,实现邮件发送