使用commons email发送邮件
在项目中,一直来是使用spring对java mail的封装,来发邮件的。虽然spring的封装已经大大简化了发送邮件的复杂度,但是今天要介绍下apache commons email组件,对java mail进行了更好的封装,对于客户端的调用,已经是相当地方便了。commons-email maven库地址:
<dependency>
<groupId>commons-email</groupId>
<artifactId>commons-email</artifactId>
<version>1.1</version>
</dependency>
commons-email实现是相当的简单,下面介绍下几个重要的类实现
1)Email
一个抽象类,对java mail进行了封装,提供主要接口如下
setHostName(String):设置smtp服务器地址
setSmtpPort(int):设置smtp服务器端
setAuthenticator(Authenticator):设置授权帐号和密码
setSSL(boolean ssl):是否采用ssl方式连接服务
setMailSession(Session):设置与服务器连接session
setCharset(String):设置邮件主体编码
setFrom(String):设置发件人地址
addTo(String):添加收件人地址
addCc(String):添加抄送人地址
addBcc(String):添加密送人地址
addReplyTo(String):添加回复人地址
setSubject(String):设置邮件标题
setMsg(String):设置邮件主体
send():发送邮件
2)SimpleEmail
继承Email,实现了setMsg(String msg)方法,发送文本格式邮件,调用非常简单
SimpleEmail email
=
new
SimpleEmail();
email.setSSL( true );
email.setSmtpPort(SMTP_PORT);
email.setHostName(SMTP_SERVER);
email.setAuthentication(USER_NAME, PASSWORD);
email.addTo( " **@**.com " );
email.setFrom( " **@**.com " );
email.setSubject( " subject " );
email.setCharset( " GBK " );
email.setMsg( " message " );
email.send();
email.setSSL( true );
email.setSmtpPort(SMTP_PORT);
email.setHostName(SMTP_SERVER);
email.setAuthentication(USER_NAME, PASSWORD);
email.addTo( " **@**.com " );
email.setFrom( " **@**.com " );
email.setSubject( " subject " );
email.setCharset( " GBK " );
email.setMsg( " message " );
email.send();
3)MultiPartEmail
继承Email,多了一个添加附件的方法:
attach(URL, String, String)
attach(URL, String, String, String)
attach(DataSource, String, String)
attach(DataSource, String, String, String)
attach(EmailAttachment)
调用方式如下:
MultiPartEmail email
=
new
MultiPartEmail();
email.setSSL( true );
email.setSmtpPort(SMTP_PORT);
email.setHostName(SMTP_SERVER);
email.setAuthentication(USER_NAME, PASSWORD);
email.addTo( " **@**.com " );
email.setFrom( " **@**.com " );
email.setSubject( " subject " );
email.setCharset( " GBK " );
email.setMsg( " message " );
email.attach( " file:///***.pom.xml " , " pom.xml " , " pom " );
email.send();
email.setSSL( true );
email.setSmtpPort(SMTP_PORT);
email.setHostName(SMTP_SERVER);
email.setAuthentication(USER_NAME, PASSWORD);
email.addTo( " **@**.com " );
email.setFrom( " **@**.com " );
email.setSubject( " subject " );
email.setCharset( " GBK " );
email.setMsg( " message " );
email.attach( " file:///***.pom.xml " , " pom.xml " , " pom " );
email.send();
4)HtmlEmail
继承MultiPartEmail,与SimpleEmail相比,多了一个setHtmlMsg(String msg)方法,用户设置html格式内容,调用方式同SimpleEmail几乎一致
HtmlEmail email
=
new
HtmlEmail();
email.setSSL( true );
email.setSmtpPort(SMTP_PORT);
email.setHostName(SMTP_SERVER);
email.setAuthentication(USER_NAME, PASSWORD);
email.addTo( " **@**.com " );
email.setFrom( " **@**.com " );
email.setSubject( " subject " );
email.setCharset( " GBK " );
email.setHtmlMsg( " <html><body><b>this is email message!</body></html> " );
email.send();
email.setSSL( true );
email.setSmtpPort(SMTP_PORT);
email.setHostName(SMTP_SERVER);
email.setAuthentication(USER_NAME, PASSWORD);
email.addTo( " **@**.com " );
email.setFrom( " **@**.com " );
email.setSubject( " subject " );
email.setCharset( " GBK " );
email.setHtmlMsg( " <html><body><b>this is email message!</body></html> " );
email.send();
当然,commons email只提供了对smtp协议的封装,不包括对pop协议的封装,所以只能发送邮件,不能接受邮件。