利用Jmail发带附件的邮件

2008-02-23 09:31:28来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Thinking in Java第三版读书笔记-第七章:多态性

下一篇:java: isn't it ironic?