--sunfruit
没有什么说的,源代码如下
public void sendMail(int id) {
//数据初始化
Transport transport; //发邮件时使用
String part = GetResource.getXMLValue("mail_port"); //端口
String mailhost = GetResource.getXMLValue("mail_server"); //服务器
String user = GetResource.getXMLValue("mail_user"); //用户名
String password = GetResource.getXMLValue("mail_password"); //密码
String from = GetResource.getXMLValue("mail_from"); //发件mail
String strname = GetResource.getXMLValue("mail_strname"); //在地址栏上显示的名字
String to = GetResource.getXMLValue("mail_to"); //收件人
String cc = ""; //抄送人
String bcc = ""; //密送人
String title = GetResource.getXMLValue("mail_title"); //标题
byte[] bytes = null;
try {
bytes = FileIO.readFile(GetResource.getXMLValue("mail_contentpath")); //正文
} catch (IOException ex) {
log.error("",ex);
}
String fromat = GetResource.getXMLValue("mail_fromat");
//获得下载需要的key
byte[] bkeydes=null;
try {
bkeydes=FileIO.readFile(GetResource.getXMLValue("附件地址"));
} catch (IOException ex) {
log.error("",ex);
}
try {
Properties props = System.getProperties(); //获得系统属性
props.put("mail.smtp.host", mailhost); //设置SMTP主机
props.put("mail.smtp.auth", "true"); //设置身份验证为真,若须身份验证则必须设为真
//获得邮件会话对象
Session session = null;
//需要验证
session = Session.getDefaultInstance(props,
new SmtpAuthenticator(user,
password));
//创建MIME邮件对象
MimeMessage mimeMsg = new MimeMessage(session);
//设置发信人
if (strname != null && !strname.equals("")) {
mimeMsg.setFrom(new InternetAddress(from, strname));
} else {
mimeMsg.setFrom(new InternetAddress(from));
}
//设置收信人
if (!to.equals("")) {
mimeMsg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
}
//设置抄送人
if (!cc.equals("")) {
mimeMsg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc));
}
//设置暗送人
if (!bcc.equals("")) {
mimeMsg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(bcc));
}
//设置邮件主题
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
// mimeMsg.setSubject("=?GB2312?B?" + enc.encode(title.getBytes()) +
// "?=");
mimeMsg.setSubject(title, "GBK");
//设置邮件内容
//mimeMsg.setText(content, "gb2312");
//发送日期
mimeMsg.setSentDate(new Date());
/*******************附件 begin××××××××××××××××××××××××*/
BodyPart mbp = null; //正文
//MimeBodyPart mbp1 = null; //附件
mbp = new MimeBodyPart();
//设置邮件正文格式(html/text)
if (fromat.equals("0") || fromat.equals("")) {
//普通格式
mbp.setText(new String(bytes,"GBK"));
} else {
//网页格式
mbp.setDataHandler(new DataHandler(new String(bytes,"GBK"),
"text/html;charset=gb2312"));
}
//!!!!注意必须为new MimeMultipart("related"); 这样的实例化才能发送html正文显示图片的邮件 "related"
//new MimeMultipart(); 这样实例化则不能发送html正文显示图片的邮件 只可以发送其他的邮件
Multipart mp = new MimeMultipart("related");
if (mbp != null)
mp.addBodyPart(mbp); //正文
//附件key.des
BodyPart messageBodyPart = new MimeBodyPart();
ByteArrayDataSource fileds = new ByteArrayDataSource(bkeydes,"application/octet-stream");
messageBodyPart.setDataHandler(new DataHandler(fileds));
//解决附件中文问题
//mbp1.setFileName(fileds.getName(),"gb2312");
messageBodyPart.setFileName(MimeUtility.encodeWord("key.des", "GB2312", null));
mp.addBodyPart(messageBodyPart);
//附件 图标
messageBodyPart = new MimeBodyPart();
bytes=FileIO.readFile(GetResource.getXMLValue("img_logo"));
fileds = new ByteArrayDataSource(bytes,"application/octet-stream");
messageBodyPart.setDataHandler(new DataHandler(fileds));
//解决附件中文问题
// messageBodyPart.setFileName("fruitrsource_touming.jpg");
// messageBodyPart.setHeader("Content-ID", "fruitrsource_touming");
messageBodyPart.setFileName("1.jpg");
//!!!!注意这里是"<IMG1>" 带有尖括号 而在正文的html里面则是src="cid:IMG1"
messageBodyPart.setHeader("Content-ID", "<IMG1>");
mp.addBodyPart(messageBodyPart);
// 添加 Multipart到Message中
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
transport = session.getTransport("smtp");
transport.connect(mailhost, Integer.parseInt(part), user, password);
//发送邮件
//transport.send(mimeMsg, mimeMsg.getAllRecipients());
//transport.send(mimeMsg);
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
//System.out.println("mail send!");
transport.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
class SmtpAuthenticator extends Authenticator {
//SMTP身份验证
public SmtpAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
}
String username = null;
String password = null;
}