最近在练习邮件发送的一些操作(这里用的qq邮箱),开始的时候没有用 Spring 来写,就是用最基础的方法:
Properties配置一些属性信息,用session初始化环境,设置Message属性,最后用transport发送邮件
这时候一切还是正常,我的mail.smtp.port设置的是465(587不行),运行一切正常。
然后用基础的Spring写邮件,这里操作与上面差不多,细节还是有点区别,总之该有的都写了,但是写完后一直报Could not connect to SMTP host这个错,毕竟第一次写也没什么经验,去百度查了查有几点注意
- 邮箱开启POP3/SMTP服务、IMAP/SMTP服务
- 开启SSL安全协议
- 端口换成587(465不行。就是这里,我用465试了好久,用各种方法惭愧惭愧)
- 用户名、密码的填写正确,尤其是密码要写成授权码
- 一定要设置debug,之前就是因为没开这个浪费我很长时间找问题。
就是以上几点,以下是我写的代码:
正常写的
public static void sendMail(String title, String value) throws MessagingException {
Properties properties = new Properties();
// 先确定邮件的传输协议
properties.put("mail.transport.protocol", "smtp");
// 在设置这个传输协议的一些值,如:服务器地址、服务器端口号、需要用户认证
properties.put("mail.smtp.host", "smtp.qq.com");
properties.put("mail.smtp.port", "465");
// 开启安全验证
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.debug", "true");
// 这里一般是静态获取的,所以我类写成static了
Session session = Session.getInstance(properties);
// 设置邮件
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("填你自己的QQ邮箱"));
// 这个是设置邮件接收者
//TO是接收人、CC是抄送人,BCC是秘密抄送人
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("填接收人的QQ邮箱"));
// 这是抄送人
message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("填抄送人QQ邮箱"));
// 秘密抄送人
message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress("填秘密抄送人的QQ邮箱"));
message.setSentDate(new Date());
message.setSubject(title);
message.setText(value);
// 发送邮件
Transport transport = session.getTransport();
transport.connect("填你自己的QQ邮箱", "授权码");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
本人比较喜欢JUnti方式测试代码
@Test
public void mailSend() throws MessagingException {
try {
SendMessages.sendMail("主题", "内容");
} catch (Exception e) {
// 获取错误信息及错误位置
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
e.printStackTrace(pw);
pw.flush();
sw.flush();
// 调用方法 传入参数 发送邮件
SendMessages.sendMail("error", sw.toString());
}
}
用Spring写的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.xingrenhaofa.spring_mail"/>
<!-- 配置邮件发送器 -->
<bean id="JavaMailSenderImpl"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.qq.com" />
<property name="port" value="587" />
<property name="defaultEncoding" value="utf8"/>
<property name="username" value="填自己邮箱" />
<property name="password" value="授权码" />
<property name="JavaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<!--因为之前一直包超时错误,这里我就注释了-->
<!-- <prop key="mail.smtp.timeout">20000</prop> -->
<prop key="mail.debug">true</prop>
</props>
</property>
<!-- <property name="mail.smtp.auth" value="true" />
<property name="mail.smtp.starttls.enable" value="true" />
<property name="mail.smtp.timeout" value="2000" /> -->
</bean>
</beans>
@Component("mailUtils")
public class MaliUtils {
@Autowired
private JavaMailSenderImpl mailSender;
public void sendMail(String from,String to,String title,String content) {
SimpleMailMessage message=new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(title);
message.setText(content);
mailSender.send(message);
}
}
@Test
public void sendMail()throws MessagingException {
String xml="com/xingrenhaofa/spring_mail/mailApplication4.xml";
ApplicationContext context =new ClassPathXmlApplicationContext(xml);
MaliUtils mailUtils=(MaliUtils)context.getBean("mailUtils");
mailUtils.sendMail("自己邮箱", "收件人邮箱", "spring_mail测试", "一次成功吧");
}