在大多数的系统中都会有发送邮件的功能。所以整理了个发送邮件的工具类。以备查阅。
配置文件
mail.propertis
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.host=smtp.qq.com
mail.smtp.port=465
username=
password=
java相关代码
package com.bobohe.mail ;
import java.util.Date ;
import java.util.Locale ;
import java.util.Properties ;
import java.util.ResourceBundle ;
import javax.mail.Authenticator ;
import javax.mail.Message ;
import javax.mail.Multipart ;
import javax.mail.PasswordAuthentication ;
import javax.mail.Session ;
import javax.mail.Transport ;
import javax.mail.internet.InternetAddress ;
import javax.mail.internet.MimeBodyPart ;
import javax.mail.internet.MimeMessage ;
import javax.mail.internet.MimeMultipart ;
public class SendEmailUtil {
private static String username;
private static String password;
public static boolean sendEmail( ) throws Exception {
//读取邮件的配置文件
ResourceBundle rb = ResourceBundle .getBundle ( "mail" ,Locale .ENGLISH ) ;
//获取配置参数
String host = rb.getString ( "mail.smtp.host" ) ;
String port = rb.getString ( "mail.smtp.port" ) ;
username = rb.getString ( "username" ) ;
password = rb.getString ( "password" ) ;
//设置参数
Properties props = new Properties ( ) ;
props.setProperty ( "mail.smtp.auth" , rb.getString ( "mail.smtp.auth" ) ) ;
props.setProperty ( "mail.smtp.starttls.enable" ,rb.getString ( "mail.smtp.starttls.enable" ) ) ;
props.setProperty ( "mail.smtp.host" , host) ;
props.setProperty ( "mail.smtp.port" , port) ;
props.setProperty ( "mail.smtp.ssl.enable" , "true" ) ;
props.setProperty ( "mail.smtp.ssl.trust" , host) ;
props.setProperty ( "mail.smtp.ssl.checkserveridentity" ,"false" ) ;
//获取session对象
Session session = Session.getInstance ( props,
new Authenticator ( ) {
protected PasswordAuthentication getPasswordAuthentication( ) {
return new PasswordAuthentication ( username, password) ;
}
} ) ;
//打印debug信息
session.setDebug ( true ) ;
//邮件内容
Message msg = new MimeMessage( session) ;
msg.setFrom ( new InternetAddress( username) ) ;
msg.setRecipient ( Message.RecipientType .TO , new InternetAddress( "solo.he@mpos.net" ) ) ;
msg.setSentDate ( new Date ( ) ) ;
msg.setSubject ( "hava a test" ) ;
MimeBodyPart mbp = new MimeBodyPart( ) ;
mbp.setContent ( "</br>hava a test</br>hava a test</br>" , "text/html;charset=UTF-8" ) ;
Multipart mulp = new MimeMultipart( ) ;
mulp.addBodyPart ( mbp) ;
msg.setContent ( mulp) ;
//获取Service对象
Transport tran = session.getTransport ( "smtp" ) ;
//连接服务器
tran.connect ( host, username, password) ;
//发送邮件
tran.sendMessage ( msg, msg.getAllRecipients ( ) ) ;
return true ;
}
public static void main( String [ ] args) throws Exception {
sendEmail( ) ;
}
}
正确的填写配置文件即可发送邮件。JavaMail version 为 1.4.3。由于使用props.setProperty("mail.smtp.ssl.trust", host)添加信任的服务器地址。所以不需要导入证书。如果没添加这个配置,怎需要导入证书到JDK的Java\jre6\lib\security目录下。
生成证书相关代码
package com.bobohe.mail ;
import java.util.Date ;
import java.util.Locale ;
import java.util.Properties ;
import java.util.ResourceBundle ;
import javax.mail.Authenticator ;
import javax.mail.Message ;
import javax.mail.Multipart ;
import javax.mail.PasswordAuthentication ;
import javax.mail.Session ;
import javax.mail.Transport ;
import javax.mail.internet.InternetAddress ;
import javax.mail.internet.MimeBodyPart ;
import javax.mail.internet.MimeMessage ;
import javax.mail.internet.MimeMultipart ;
public class SendEmailUtil {
private static String username;
private static String password;
public static boolean sendEmail( ) throws Exception {
//读取邮件的配置文件
ResourceBundle rb = ResourceBundle .getBundle ( "mail" ,Locale .ENGLISH ) ;
//获取配置参数
String host = rb.getString ( "mail.smtp.host" ) ;
String port = rb.getString ( "mail.smtp.port" ) ;
username = rb.getString ( "username" ) ;
password = rb.getString ( "password" ) ;
//设置参数
Properties props = new Properties ( ) ;
props.setProperty ( "mail.smtp.auth" , rb.getString ( "mail.smtp.auth" ) ) ;
props.setProperty ( "mail.smtp.starttls.enable" ,rb.getString ( "mail.smtp.starttls.enable" ) ) ;
props.setProperty ( "mail.smtp.host" , host) ;
props.setProperty ( "mail.smtp.port" , port) ;
props.setProperty ( "mail.smtp.ssl.enable" , "true" ) ;
// props.setProperty("mail.smtp.ssl.trust", host);
props.setProperty ( "mail.smtp.ssl.checkserveridentity" ,"false" ) ;
//获取session对象
Session session = Session.getInstance ( props,
new Authenticator ( ) {
protected PasswordAuthentication getPasswordAuthentication( ) {
return new PasswordAuthentication ( username, password) ;
}
} ) ;
//打印debug信息
session.setDebug ( true ) ;
//邮件内容
Message msg = new MimeMessage( session) ;
msg.setFrom ( new InternetAddress( username) ) ;
msg.setRecipient ( Message.RecipientType .TO , new InternetAddress( "solo.he@mpos.net" ) ) ;
msg.setSentDate ( new Date ( ) ) ;
msg.setSubject ( "hava a test" ) ;
MimeBodyPart mbp = new MimeBodyPart( ) ;
mbp.setContent ( "</br>hava a test</br>hava a test</br>" , "text/html;charset=UTF-8" ) ;
Multipart mulp = new MimeMultipart( ) ;
mulp.addBodyPart ( mbp) ;
msg.setContent ( mulp) ;
//获取Service对象
Transport tran = session.getTransport ( "smtp" ) ;
//连接服务器
tran.connect ( host, username, password) ;
//发送邮件
tran.sendMessage ( msg, msg.getAllRecipients ( ) ) ;
return true ;
}
public static void main( String [ ] args) throws Exception {
sendEmail( ) ;
}
}
代码执行一次就会在JDK的Java\jre6\lib\security目录下生成证书。输入参数为邮件服务器host和port。用:号隔开。