一个Web 系统通常会少不了邮件服务的,比如用于注册,密码找回,订单提醒等应用场景。Spring 封装了一个简单易用的关于邮件发送的工具类JavaMailSenderImpl 。
系统要提供邮件服务,那得需要一个邮件服务器,用于发送和回复邮件。如果有条件专门弄一个邮件服务器那固然是最好的,但是也可以简单的使用163或者qq提供的邮件服务。
例如注册了一个example@163.com的邮箱账号,在 设置 勾选 POP3/SMTP服务,然后保存。点击左侧导航栏中的 客户端授权密码 ,开启客户端授权码,重置授权码,你会收到一个授权码的短信,这个授权码就是用来第三方客户端登录的密码,这个待会会使用到。
1.核心工具类MailUtil.java
import java.util.Date;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.Resource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
public class MailUtil {
public static JavaMailSenderImpl mailSender = createMailSender(ConfigInfo.mail_host,ConfigInfo.mail_port,ConfigInfo.mail_username,
ConfigInfo.mail_password,ConfigInfo.mail_smtp_timeout);
public static String mailFrom = ConfigInfo.mail_from;
private static JavaMailSenderImpl createMailSender(String host,int port,String username,String password,int timeout){
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(host);
sender.setPort(port);
sender.setUsername(username);
sender.setPassword(password);
sender.setDefaultEncoding("Utf-8");
Properties p = new Properties();
p.setProperty("mail.smtp.timeout",timeout+"");
p.setProperty("mail.smtp.auth","true");
sender.setJavaMailProperties(p);
return sender;
}
//发送测试的邮件
public static void sendMailForTest(String host,int port,String username,String password,String from,
String to){
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom(from);
mail.setTo(to);
mail.setSubject("这是测试邮件,请勿回复!");
mail.setSentDate(new Date());// 邮件发送时间
mail.setText("这是一封测试邮件。如果您已收到此邮件,说明您的邮件服务器已设置成功。请勿回复,请勿回复,请勿回复,重要的事说三遍!");
JavaMailSenderImpl sender = createMailSender(host,port,username,password,25000);
sender.send(mail);
}
public static void sendTextMail(String to,String subject,String text){
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom(mailFrom);
mail.setTo(to);
mail.setSubject(subject);
mail.setSentDate(new Date());// 邮件发送时间
mail.setText(text);
mailSender.send(mail);
}
public static void sendHtmlMail(String to,String subject,String html) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
// 设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
messageHelper.setFrom(mailFrom);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(html, true);
mailSender.send(mimeMessage);
}
public static void sendFileMail(String to,String subject,String html,String contentId,Resource resource) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
// 设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
messageHelper.setFrom(mailFrom);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(html, true);
//FileSystemResource img = new FileSystemResource(new File("c:/350.jpg"));
messageHelper.addInline(contentId, resource);
// 发送
mailSender.send(mimeMessage);
}
}
为了使用方便,采用静态方法的实现方式,其中的JavaMailSenderImpl 实例是通过代码的方式创建的,脱离了Spring容器的管理。当然也可以使用Spring注入的方式:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config.properties" />
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.163.com" />
<property name="port" value="25" />
<property name="username" value="${mail_username}"
<property name="password" value="${mail_password}" />
<property name="defaultEncoding" value="UTF-8"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>
在代码中直接这样注入:
//1)首先类需要用 @Component 注解类,并要配置扫描此包,让Spring识别到。
//2)然后JavaMailSender实例通过@Autowired来自动装配,其他不变
//3)需要在Spring容器中配置属性文件PropertyPlaceholderConfigurer
public static JavaMailSender mailSender;
@Autowired
public void setMailSender(JavaMailSender mailSen