1、UtilTools.sendMail(to, to, subject, body.toString());
参数说明:
第一个to:收件人 “yongzhian@huawei.com;”主送多个人,to用";"隔开
第二个to:收件人 “yongzhian@huawei.com;”抄送多个人, 用";"隔开
subject:主题
body:内容 即html
2、实现类
EmailBase.getInstance().SendEmailsTCB(to, "","",subject, body);
EmailBase采用单例,私有属性,
private String mail_smtp_host;
private String username;
private String password;
private String from;
private String fromname;
private String to;
private String cc;
private String bcc;
private String toname;
private String subject;
private String body;
private String authenticator;
private Properties prp;
private static EmailBase emailBase;
初始化“
init(){
prp = new Properties();
ResourceBundle config = ResourceBundle.getBundle("email", Locale.SIMPLIFIED_CHINESE);
mail_smtp_host = config.getString("mail.smtp.host");
username = config.getString("username");
password = config.getString("password");
from = config.getString("from");
fromname = config.getString("fromname");
if (Constants.NEED_COMPOSE_ENCODE)
fromname = UtilTools.encodeString(fromname);
authenticator = config.getString("mail.smtp.auth");
prp.setProperty("mail.smtp.host", mail_smtp_host);
prp.setProperty("username", username);
prp.setProperty("password", password);
prp.setProperty("mail.smtp.auth", authenticator); }
private boolean SendEmailsTCB() {
try {
EmailBaseAuthenticator sa = new EmailBaseAuthenticator(username,password);
Session session = Session.getInstance(prp, sa);
//发邮件
MimeMessage sendmessage = new MimeMessage(session);
sendmessage.setFrom(new InternetAddress(from, fromname, "GBK"));
// 分拆收件人
StringTokenizer stk_to = new StringTokenizer(to, ";");
int count_to = stk_to.countTokens();
int i = 0;
InternetAddress[] address_to = new InternetAddress[count_to];
while (stk_to.hasMoreTokens()) {
String email = stk_to.nextToken();
address_to[i] = new InternetAddress(email, "", "GBK");
i++;
}
sendmessage.addRecipients(Message.RecipientType.TO, address_to);
// 分拆抄送人
StringTokenizer stk_cc = new StringTokenizer(cc, ";");
int count_cc = stk_cc.countTokens();
int j = 0;
InternetAddress[] address_cc = new InternetAddress[count_cc];
while (stk_cc.hasMoreTokens()) {
String email = stk_cc.nextToken();
address_cc[j] = new InternetAddress(email, "", "GBK");
j++;
}
sendmessage.addRecipients(Message.RecipientType.CC, address_cc);
// 分拆密送人
StringTokenizer stk_bcc = new StringTokenizer(bcc, ";");
int count_bcc = stk_bcc.countTokens();
int k = 0;
InternetAddress[] address_bcc = new InternetAddress[count_bcc];
while (stk_bcc.hasMoreTokens()) {
String email = stk_bcc.nextToken();
address_bcc[k] = new InternetAddress(email, "", "GBK");
k++;
}
sendmessage.addRecipients(Message.RecipientType.BCC, address_bcc);
if (Constants.NEED_COMPOSE_ENCODE)
sendmessage.setSubject(UtilTools.encodeString(subject), "GBK");
sendmessage.setSubject(subject, "GBK");
sendmessage.setSentDate(new Date());
sendmessage.setContent(body, "text/html; charset=GBK");
Transport.send(sendmessage);
} catch (AuthenticationFailedException afe) {
afe.printStackTrace();
return false;
} catch (AddressException ae) {
ae.printStackTrace();
return false;
} catch (javax.mail.MessagingException e) {
e.printStackTrace();
return false;
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
3、需要调的实现类
public class EmailBaseAuthenticator extends Authenticator {
private String username;
private String password;
public EmailBaseAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
}
}
4、使用mail.jar和commons-email-1.3.3-bin.zip轻松发送邮件
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
public class Test {
public static void main(String[] args) throws Exception {
// SimpleEmail - This class is used to send basic text based emails.
// MultiPartEmail - This class is used to send multipart messages. This allows a text message with attachments either inline or attached.
// HtmlEmail - This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images.
// ImageHtmlEmail - This class is used to send HTML formatted emails with inline images. It has all of the capabilities as HtmlEmail but transform all image references to inline images.
// EmailAttachment - This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.qq.com");
email.setSmtpPort(25);
email.setAuthenticator(new DefaultAuthenticator("1*********", "a************"));
email.setSSLOnConnect(true);
email.setFrom("1************@qq.com");
email.setSubject("TestMail测试邮箱");
email.setMsg("This is a test mail ... :用纸-)");
email.addTo("y*****@163.com");
EmailAttachment emailAttachment = new EmailAttachment();
emailAttachment.setPath("mail.jar");
emailAttachment.setName("java邮件架包");
email.attach(emailAttachment);
email.send();
}
}
所需架包下载地址 http://download.youkuaiyun.com/detail/yongzhian/7653175