package org.mail; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import org.entity.MailModel; /** * 发送一封邮件 * @说明 注意这里并没有实际发送而是保存在了C盘临时文件中,真是发送的话,请参考我的博客 * @author fei.teng * @version 1.0 * @since */ public class SendMail { public static void sendMail(MailModel mail) throws Exception { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props); Message message = new MimeMessage(session); InternetAddress from = new InternetAddress(); from.setPersonal(MimeUtility.encodeText("风中落叶<cuisuqiang@163.com>")); message.setFrom(from); InternetAddress to = new InternetAddress(mail.getTo()); message.setRecipient(Message.RecipientType.TO, to); // 是否抄送 if (null != mail.getCopy() && !"".equals(mail.getCopy())) { InternetAddress copy = new InternetAddress(mail.getCopy()); message.setRecipient(Message.RecipientType.CC, copy); } message.setSubject(MimeUtility.encodeText(mail.getTitle())); message.setSentDate(new Date()); // 指定为混合关系 MimeMultipart msgMultipart = new MimeMultipart("mixed"); message.setContent(msgMultipart); MimeBodyPart content = new MimeBodyPart(); msgMultipart.addBodyPart(content); // 依赖关系 MimeMultipart bodyMultipart = new MimeMultipart("related"); content.setContent(bodyMultipart); MimeBodyPart htmlPart = new MimeBodyPart(); // 组装的顺序非常重要 bodyMultipart.addBodyPart(htmlPart); MimeBodyPart in_bg = new MimeBodyPart(); bodyMultipart.addBodyPart(in_bg); DataSource bgsou = new FileDataSource(mail.getBgPath()); DataHandler bghd = new DataHandler(bgsou); in_bg.setDataHandler(bghd); in_bg.setHeader("Content-Location", "bg.jpg"); // 是否使用了背景音乐 if (null == mail.getMusicPath() || "".equals(mail.getMusicPath())) { String start = "<html><body background='bg.jpg'>"; String end = "</body></html>"; htmlPart.setContent(start + mail.getContext() + end,"text/html;charset=UTF-8"); } else { MimeBodyPart in_Part = new MimeBodyPart(); bodyMultipart.addBodyPart(in_Part); DataSource gifds = new FileDataSource(mail.getMusicPath()); DataHandler gifdh = new DataHandler(gifds); in_Part.setDataHandler(gifdh); in_Part.setHeader("Content-Location", "bg.mp3"); String start = "<html><head><bgsound src='bg.mp3' loop='-1'></head><body background='bg.jpg'>"; String end = "</body></html>"; htmlPart.setContent(start + mail.getContext() + end,"text/html;charset=UTF-8"); } // 组装附件 if (null != mail.getFilePath() && !"".equals(mail.getFilePath())) { MimeBodyPart file = new MimeBodyPart(); FileDataSource file_datasource = new FileDataSource(mail .getFilePath()); DataHandler dh = new DataHandler(file_datasource); file.setDataHandler(dh); file.setFileName(MimeUtility.encodeText(dh.getName())); msgMultipart.addBodyPart(file); } message.saveChanges(); // 保存邮件 OutputStream ips = new FileOutputStream("C:\\tmp\\test.eml"); message.writeTo(ips); ips.close(); System.out.println("------------发送完毕------------"); // 删除临时文件 if (null != mail.getMusicPath() && !"".equals(mail.getMusicPath())) { File file = new File(mail.getMusicPath()); file.delete(); } if (null != mail.getFilePath() && !"".equals(mail.getFilePath())) { File file = new File(mail.getFilePath()); file.delete(); } } }
最近下载更多