这个只是一个Demo,可以用来封装为像多人批量发送:
废话少说,直接上代码:
(代码较多,直接复制之过,不过请大家耐心查看,可以根据我的分类复制代码到本地,在组合测试)
/****************************************************************************/
主文件代码:
package com.cmcc.mail;
public class TestMail {
public static void main(String[] args){
//这个类主要是设置邮件
MailSenderInfo mailInfo = new MailSenderInfo(); //此类封装参见代码2(下面)
mailInfo.setMailServerHost("smtp.163.com"); //您要使用的邮箱服务器地址
mailInfo.setMailServerPort("25"); //SMTP使用的TCP端口固定是25
mailInfo.setValidate(true);
mailInfo.setUserName("这里您自己填入"); //您的邮箱地址
mailInfo.setPassword("这里您自己填入"); //您的邮箱密码
mailInfo.setFromAddress("hxlistiancai@163.com"); //您邮件的来源地址,必须与您的邮箱地址相同
mailInfo.setToAddress("869407449@qq.com"); //目的邮箱地址
mailInfo.setSubject("计算中心北调课通知单"); //邮件主题
String content = "";
String header = "尊敬的"+"aa 老师:\n"+
"您好!\n"+
"这是一封计算中心(北)智能排课平台自动给您发送的机房课程通知单,请您按照下表的相应的时间通知学生到相应的机房上课\n"+
"谢谢您的支持,您的满意是我们计算中心最大的快乐!\n"+
"如果您对课表有任何疑问,请您拨打0315-2792027\n";
content += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"+
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
"<head>\n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n" +
// "<!--[if IE]><link href='/css/fkie.css' rel='stylesheet' type='text/css' media='all' /><![endif]-->\n" +
"<style type=\"text/css\">\n" +
"<!--\n"+
"html{border:0;height:100%;border:0;}\n" +
"body{margin:0;padding:0;height:100%;font:12px/120% Arial, Helvetica, sans-serif; text-align:left;}\n" +
"#main{ padding:15px 25px 15px 10px;}\n" +
".tables{ background:#b2bac5; width:100%; margin:1px 0;}\n"+
".tables caption{background:#e5e8ed; padding:1px 0 0 0; _border-bottom:solid 3px #fff; height:26px;}\n"+
".tables th{text-align:center;background:#eef7e2; color:#016bb7; font-weight: normal; line-height:22px;}\n"+
".tables tr{ background:#fff;}\n"+
".tables tr td{line-height:22px;}\n"+
".area-contrl{background:#e5e8ed; padding:1px 0 0 0; _border-bottom:solid 3px #fff; height:26px;}\n" +
"-->\n"+
"</style>\n" +
"</head>\n" +
"<body>\n" +
"<div id=\"main\">\n" +
"<div class=\"area-contrl\">\n" +
"<table class='tables' border='0' cellpadding='3' cellspacing='1'><tr><th width='35'>周次</th><th width='35'>星期</th><th width='35'>讲次</th><th width='180'>机房</th><th width='50'>人数</th><th width='100'>任课教师</th><th width='200'>班级</th><th width='300'>课程名称</th></tr>\n"
+"<tr><td align='center'>aaa</td><td align='center'>bbb</td><td align='center'>ccc</td><td align='center'>ddd</td><td align='center'>ccc</td><td align='center'>ddd</td><td align='center'>eee</td><td align='center'>fff</td></tr>\n"
+"<tr><td align='center'>aaa</td><td align='center'>bbb</td><td align='center'>ccc</td><td align='center'>ddd</td><td align='center'>ccc</td><td align='center'>ddd</td><td align='center'>eee</td><td align='center'>fff</td></tr>\n"
+"<tr><td align='center'>aaa</td><td align='center'>bbb</td><td align='center'>ccc</td><td align='center'>ddd</td><td align='center'>ccc</td><td align='center'>ddd</td><td align='center'>eee</td><td align='center'>fff</td></tr>\n"
+"<tr><td align='center'>aaa</td><td align='center'>bbb</td><td align='center'>ccc</td><td align='center'>ddd</td><td align='center'>ccc</td><td align='center'>ddd</td><td align='center'>eee</td><td align='center'>fff</td></tr>";
content += "</table></div></div></body></html>";
System.out.println(content);
mailInfo.setContent(content);
//这个类主要来发送邮件
SimpleMailSender sms = new SimpleMailSender(); //此类代码参见代码3(下面)
// sms.sendTextMail(mailInfo);//发送文体格式
sms.sendHtmlMail(mailInfo);//发送html格式
}
}
/*******************************************************************************/
代码2:
MailSenderInfo 类的封装
package com.cmcc.mail;
/**
* 发送邮件需要使用的基本信息
*/
import java.util.Properties;
public class MailSenderInfo {
// 发送邮件的服务器的IP和端口
private String mailServerHost;
private String mailServerPort = "25";
// 邮件发送者的地址
private String fromAddress;
// 邮件接收者的地址
private String toAddress;
// 登陆邮件发送服务器的用户名和密码
private String userName;
private String password;
// 是否需要身份验证
private boolean validate = false;
// 邮件主题
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名
private String[] attachFileNames;
/**
* 获得邮件会话属性
*/
public Properties getProperties(){
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
}
/****************************************************************************/
代码3:
SimpleMailSender 类的封装:
package com.cmcc.mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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 SimpleMailSender {
/**
* 以文本格式发送邮件
* @param mailInfo 待发送的邮件的信息
*/
public boolean sendTextMail(MailSenderInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 以HTML格式发送邮件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendHtmlMail(MailSenderInfo mailInfo){
// 判断是否需要身份认证
MyAuthenticator authenticator = null; //此类参见代码4(下面)
Properties pro = mailInfo.getProperties();
//如果需要身份认证,则创建一个密码验证器
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
}
/********************************************************************************/
代码4:
MyAuthenticator 类的封装:
package com.cmcc.mail;
import javax.mail.*;
public class MyAuthenticator extends Authenticator{
String userName=null;
String password=null;
public MyAuthenticator(){
}
public MyAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName, password);
}
}
posted on 2011-02-26 11:09
Soap MacTavish 阅读(400)
评论(0) 编辑 收藏