利用Jmail发带附件的邮件
2008-02-23 09:31:28来源:互联网 阅读 ()
package com.sidiw.util.jmail;
import Java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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;
import javax.mail.internet.MimeMessage.RecipientType;
import sun.misc.BASE64Encoder;
public class SendMail {
/** 文本编码 */
private String encode;
/** 文本还是html内容 */
private boolean HTML;
private Session session;
private Transport transport;
private BASE64Encoder enc = new sun.misc.BASE64Encoder();
public SendMail() {
this.encode = "GBK";
this.HTML = true;
}
public SendMail(String encode, boolean html) {
this.encode = encode;
this.HTML = html;
}
public boolean connect(String smtpHost, String uid, String pwd) {
boolean result = false;
ConnectMail connect = new ConnectMail();
result = connect.connectSmtp(smtpHost, uid, pwd);
this.session = connect.getSession();
this.transport = connect.getTransport();
return result;
}
public void close() {
if (this.transport != null) {
try {
this.transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void send(Map outerMailInfo) {
try {
/** 文件附件编码 */
//setFileName(MimeUtility.encodeText(fileName, "GBK"));
/** 文件主题编码 */
//msg.setSubject("=?GB2312?B?" enc.encode(subject.getBytes()) "?=");
String title = "=?"
this.encode
"?B?"
enc.encode(((String) outerMailInfo.get("strTitle"))
.getBytes()) "?=";
MimeMessage mimeMessage = createMimeMessage(((String) outerMailInfo
.get("strFromName")), ((String) outerMailInfo
.get("strFrom")), ((String) outerMailInfo.get("strTo")),
((String) outerMailInfo.get("strCc")),
((String) outerMailInfo.get("strBcc")), title,
((String) outerMailInfo.get("strContent")),
((String[]) outerMailInfo.get("strFileNameList"))//outerMailInfo.getStrFileNameList()
);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
} catch (MessagingException e) {
e.printStackTrace();
}
}
private MimeMessage createMimeMessage(String fromName, String from,
String to, String cc, String bcc, String subject, String text,
String[] filename) throws MessagingException {
/** 加入调试信息 */
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
InternetAddress fromAddress = new InternetAddress(from);
try {
if (fromName != null)
fromAddress.setPersonal(fromName);
} catch (Exception e) {
}
message.setFrom(fromAddress);
InternetAddress[] toAddress = InternetAddress.parse(to);
InternetAddress[] ccAddress = InternetAddress.parse(cc);
InternetAddress[] bccAddress = InternetAddress.parse(bcc);
message.setRecipients(RecipientType.TO, toAddress);
message.setRecipients(RecipientType.CC, ccAddress);
message.setRecipients(RecipientType.BCC, bccAddress);
message.setSentDate(new Date());
message.setSubject(subject, this.encode);
/** 处理附件 */
if (filename != null && filename.length > 0) {
Multipart multipart = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
if (HTML) {
mimeBodyPart.setContent(text, "text/html;charset="
this.encode);
} else {
mimeBodyPart.setText(text, this.encode);
}
multipart.addBodyPart(mimeBodyPart);
for (int i = 0; i < filename.length; i ) {
try {
MimeBodyPart fileBodyPart = new MimeBodyPart();
DataSource datasource = new FileDataSource(filename[i]);
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash