使用Maven构建项目,在pom.xml中添加依赖包,

1
2
3
4
5
6
7
8
<dependencies>
...
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>

参考User guide,基于QQ邮箱发送文本邮件,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main(String[] args) {
Email email = new SimpleEmail();
// 连接参数配置
email.setHostName("smtp.qq.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("QQ No", "QQ Password");
email.setSSLOnConnect(true);
// 邮件相关内容
try {
email.setFrom("[email protected]");
email.setMsg("Hello, world!");
email.addTo("[email protected]");
email.send();
} catch (EmailException e) {
}
}

与使用JDBC连接数据库一样,前面的代码包含大量的模板操作,所以结合Spring,与JdbcTemplate类似,构建MailTemplate。在applicationContext-mail.xml中声明Bean,

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
<beans...>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mail.properties</value>
</list>
</property>
</bean>
<bean id="simpleMail" class="org.apache.commons.mail.SimpleEmail">
<property name="hostName" value="${me.mail.hostName}" />
<property name="smtpPort" value="${me.mail.smtpPort}" />
<property name="from" value="${me.mail.from}" />
<property name="authenticator">
<bean class="org.apache.commons.mail.DefaultAuthenticator">
<constructor-arg value="${me.mail.authenticator.userName}" />
<constructor-arg value="${me.mail.authenticator.password}" />
</bean>
</property>
</bean>
<bean id="mailTemplate" class="me.voler.jechat.core.MailTemplate">
<property name="simpleMail" ref="simpleMail" />
</bean>
</beans>

将连接参数添加到mail.properties,me.voler.jechat.core.MailTemplate即构建的MailTemplate,

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
package me.voler.jechat.core;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
public class MailTemplate {
private SimpleEmail simpleMail;
public void send(String msg, String to) {
this.send("【邮件】", msg, to);
}
/**
*
* @param subject 邮件主题
* @param msg 邮件正文
* @param toList 收件人列表
*/
public void send(String subject, String msg, String... toList) {
try {
simpleMail.setSubject(subject);
simpleMail.setMsg(msg);
simpleMail.addTo(toList);
simpleMail.setSSLOnConnect(true);
simpleMail.send();
} catch (EmailException e) {
e.printStackTrace();
}
}
public SimpleEmail getSimpleMail() {
return simpleMail;
}
public void setSimpleMail(SimpleEmail simpleMail) {
this.simpleMail = simpleMail;
}
}

使用构建的MailTemplate发送文本邮件,

1
2
3
4
5
6
7
@Autowired
@Qualifier("mailTemplate")
private MailTemplate mailTemplate;
public void sendEmptyMail() {
mailTemplate.send("中文测试,English Test.", "[email protected]");
}

SimpleEmail的父类的属性sslOnConnect的set方法名为setSSLOnConnect,如果直接在applicationContext-mail.xml的simpleMailBean下配置<property name="sslOnConnect" value="${me.mail.sslOnConnect}" />会提示

Bean property is not writable or has an invalid setter method