package src.reflect;import java.util.Date;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
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 sun.misc.BASE64Encoder;public class Mail { // private static String server = "smtp.163.com";// smtp主机名。
//
// private static String from = "[email protected]";// 发送方邮件地址
//
// private static String pw = "bluebird82";// 发送方邮件密码。
//
// private static String to = "[email protected]"; // 接收方邮件地址 // static String subject = "你好!";
//
// static String mailContent = "你好!\n\n" + "\t邮件正文test\n\n";// 邮件正文
/**
 * 邮件属性DTO
 */
private MailDTO mailDTO; public Mail(MailDTO mailDTO) {
this.mailDTO = mailDTO;
} public Mail() {
mailDTO = new MailDTO();
} public void send() {
Multipart mp = new MimeMultipart();
// String htmlandtext = "text";
String[] filepath = { "e:/员工转正评价表20060217.doc" };
String[] filename = { "员工转正评价表20060217.doc" };
// 附件
try {
int indexstr = 0;
if (filepath[0] != null && !filepath[0].equals("")) {
indexstr = 1;
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", mailDTO.getServer());
props.put("mail.smtp.user", mailDTO.getFrom());
props.put("mail.smtp.password", mailDTO.getPassword());
sendMailSession = Session.getInstance(props,
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() // 邮件安全认证。
{
return new PasswordAuthentication(mailDTO
.getFrom(), mailDTO.getPassword());
}
}); Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(mailDTO.getFrom()));
newMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress(mailDTO.getTo()));
newMessage.setSubject(mailDTO.getSubject());
newMessage.setSentDate(new Date());
String mailContent = mailDTO.getMailContent();
if (mailDTO.getHeadType().equals("text")) {
// 获得文本格式的邮件
newMessage.setSubject(mailDTO.getSubject());
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(mailContent + "\n\t\t\t\t\t\t"
+ new Date().toLocaleString());
// mailContent += new Date().toLocaleString();
mp.addBodyPart(messageBodyPart);
} else if (mailDTO.getHeadType().equals("html")) {
// 设置邮件内容,将邮件body部分转化为HTML格式
newMessage.setSubject(mailDTO.getSubject());
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(mailContent
+ "\n\t\t\t\t\t\t" + new Date().toLocaleString(),
"text/html;charset=gb2312"));
mailContent += new Date().toLocaleString();
mp.addBodyPart(messageBodyPart);
}
if (indexstr > 0) {
for (int i = 0; i < mailDTO.getFileName().length; i++) {
if (mailDTO.getFileName()[i] != null) {
// 创建BodyPart对象以便获得附件
BodyPart messageBodyPart = new MimeBodyPart();
// 邮件附件路径
DataSource source = new FileDataSource(mailDTO
.getFilePath()[i]);
// 添加附件
messageBodyPart.setDataHandler(new DataHandler(
source));
// 邮件附件名称的中文乱问题解决方案
BASE64Encoder enc = new BASE64Encoder();
messageBodyPart.setFileName("=?GBK?B?"
+ enc.encode((source.getName()).getBytes())
+ "?=");
mp.addBodyPart(messageBodyPart);
}
}
}
newMessage.setContent(mp);
newMessage.saveChanges();
transport = sendMailSession.getTransport("smtp");
System.out.println("Sending...");
Transport.send(newMessage, newMessage.getAllRecipients());
System.out.println("send success");
}
} catch (MessagingException ex) {
ex.printStackTrace();
System.out.println("send fail\t\n" + ex.getMessage());
}
} /**
 * @param args
 */
public static void main(String[] args) {
MailDTO dao = new MailDTO();
String[] path = { "e:\\员工转正评价表20060217.doc" };
String[] name = { "员工转正评价表20060217.doc" };
dao.setServer("smtp.163.com");
dao.setFrom("[email protected]");
dao.setPassword("bluebird82");
dao.setTo("[email protected]");
dao.setSubject("你好");
dao.setMailContent("你好:\n\t\t这是我的第一封邮件");
dao.setHeadType("text");
dao.setFilePath(path);
dao.setFileName(name);
new Mail(dao).send();
}}

解决方案 »

  1.   

    package src.reflect;public class MailDTO {
    /**
     * smtp主机名
     */
    private String server; /**
     * 发送方邮件地址
     */
    private String from; /**
     * 发送方邮件密码
     */
    private String password; /**
     * 接收方邮件地址
     */
    private String to; /**
     * 邮件标题
     */
    private String subject; /**
     * 邮件正文
     */
    private String mailContent; /**
     * 邮件格式
     */
    private String headType; /**
     * 附件路径
     */
    private String[] filePath; /**
     * 附件名称
     */
    private String[] fileName; public String getFrom() {
    return from;
    } public void setFrom(String from) {
    this.from = from;
    } public String getMailContent() {
    return mailContent;
    } public void setMailContent(String mailContent) {
    this.mailContent = mailContent;
    } public String getPassword() {
    return password;
    } public void setPassword(String password) {
    this.password = password;
    } public String getServer() {
    return server;
    } public void setServer(String server) {
    this.server = server;
    } public String getSubject() {
    return subject;
    } public void setSubject(String subject) {
    this.subject = subject;
    } public String getTo() {
    return to;
    } public void setTo(String to) {
    this.to = to;
    } public String getHeadType() {
    return headType;
    } public void setHeadType(String headType) {
    this.headType = headType;
    } public String[] getFileName() {
    return fileName;
    } public void setFileName(String[] fileName) {
    this.fileName = fileName;
    } public String[] getFilePath() {
    return filePath;
    } public void setFilePath(String[] filePath) {
    this.filePath = filePath;
    } public void MailDTO(String server, String from, String password, String to,
    String subject, String mailContent, String headType,
    String[] filePath, String[] fileName) {
    this.server = server;
    this.from = from;
    this.password = password;
    this.to = to;
    this.subject = subject;
    this.mailContent = mailContent;
    this.headType = headType;
    this.filePath = filePath;
    this.fileName = fileName;
    }
    }