Java发邮件的几种方式
- JavaMail
- Commons Email
- Spring Mail
JavaMail是Java提供的发邮件API
Commons Email是Apache Commons下的发邮件API
Spring Mail 是Spring提供的发邮件API
所需jar包
JavaMail
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.5</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
Commons Email
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
Spring Mail
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
以Spring Mail发邮件为例
Spring配置文件如下
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 加载mail.properties配置文件 -->
<context:property-placeholder location="classpath:mail.properties"
file-encoding="utf-8" ignore-unresolvable="true"/>
<!-- mailSender配置 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}" />
<property name="username" value="${mail.smtp.username}" />
<property name="password" value="${mail.smtp.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
</props>
</property>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
mail.properties配置文件如下
#------------------------------------------------------------
# 邮件服务的配置
#------------------------------------------------------------
#服务器
mail.smtp.host=smtp.xxx.com
#是否需要验证密码
mail.smtp.auth=true
#超时时间
mail.smtp.timeout=50000
#------------------------------------------------------------
# 对用户名和密码都进行了加密
#------------------------------------------------------------
#登陆用户名
mail.smtp.username=qBFXEAFr500QuCYz1D9/nw==
#密码
mail.smtp.password=oqfyI44bbowwVRY0dSKX6yuDKF3s6J9K
#发件人
mail.smtp.from=abcd@xxx.com
#收件人
mail.smtp.to=ghi@xxx.com,jkl@xxx.com
#抄送
#mail.smtp.cc=abc@xxx.com,def@xxx.com
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
发邮件类
package com.test.mail;
public interface MailSender {
public void sendMail();
}
- 1
- 2
- 3
- 4
- 5
实现类
package com.test.mail.impl;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import com.test.mail.MailSender;
import com.test.util.PropertiesUtil;
/**
* 采用Spring的JavaMailSender发邮件
*
* @author gaowei.cheng 2015年12月23日
*/
@Component
public class SpringMail implements MailSender {
private static Logger log = Logger.getLogger(SpringMail.class);
//注入一个mailSender
@Autowired
JavaMailSenderImpl mailSender;
public void sendMail() {
//读取配置文件中的收件人
PropertiesUtil.readProperties("mail.properties");
String[] to = PropertiesUtil.getProperty("mail.smtp.to").split(",");// 收件人
String[] cc = PropertiesUtil.getProperty("mail.smtp.cc").split(",");// 收件人
MimeMessage mailMessage = mailSender.createMimeMessage();
log.info("发送邮件给" + PropertiesUtil.getProperty("mail.smtp.to"));
try {
MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "utf-8");
helper.setFrom(propUtil.getProperty("mail.smtp.from"));// 设置发件人
helper.setTo(to);// 设置收件人
helper.setCc(cc);// 设置抄送
helper.setSubject("SpringMail测试");// 设置主题
helper.setText("这是一封来自SpringMail的测试邮件");// 邮件体
mailSender.send(mailMessage);// 发送邮件
log.info("邮件发送成功...");
} catch (Exception e) {
log.error("邮件发送发生异常");
log.error(e.getMessage());
log.info("进行重发");
try {
Thread.sleep(1000 * 1000);
this.sendMail();
} catch (InterruptedException e1) {
log.info("重发邮件发生异常");
log.error(e.getMessage());
e1.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
如果是自己公司内部的邮件服务器,这个程序是没有任何问题的。
但当我们使用QQ邮箱尽心登陆,然后发邮箱时会报错,错误信息如下
503 Error
Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28
- 1
- 2
这是因为腾讯需要认证,才用平时的QQ邮箱用户名和密码已经不能登陆了。
解决办法
进入报错时,腾讯给的网址http://service.mail.qq.com/cgi-bin/help?id=28
就会进入客户端设置页面