package com.icsse.email;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
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.MimeUtility;
/**
* 实现图片附件的发送
* @author yuanfy
*
*/
public class SendEmail {
public static void main(String[] args) throws Exception{
sendEmail("oiva123@sina.com","******","oiva123@sina.com","1","1","E:/12.jpg","E:/a.zip");
}
public static void sendEmail(String sendUserEmail, String sendUserEamilPwd, String takeUserEmail, String subject,
String emailContent,String imgUrl,String attachmentUrl) throws Exception{
imgUrl = change(imgUrl);
attachmentUrl = change(attachmentUrl);
try {
String serverUrl = sendUserEmail.substring(sendUserEmail.lastIndexOf("@")+1);
String sendUserEmailName = sendUserEmail.substring(0,sendUserEmail.lastIndexOf("@"));
Properties props = new Properties();
props.setProperty("mail.host", "smtp."+serverUrl);
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
session.setDebug(true);//发送过程打到控制台
//1、得到发送邮件的对象
Transport ts;
ts = session.getTransport();
//2、连接服务器
ts.connect("smtp."+serverUrl, sendUserEmailName, sendUserEamilPwd);
//3、创建邮件
MimeMessage message = new MimeMessage(session);//创建了一封邮件
message.setFrom(new InternetAddress(sendUserEmail));//设置发件人
message.setRecipients(Message.RecipientType.TO,takeUserEmail);//设置收件人的邮箱
message.setSubject(subject);//设置主题
//创建正文:文本
MimeBodyPart text = new MimeBodyPart();
text.setContent(emailContent, "text/html;charset=UTF-8");
/**
* 注意图片路径跟附件路径如果有 就一定要正确 不然找不到 会报错的
*/
//图片
MimeBodyPart image = null;
MimeBodyPart attachment = null;
if(imgUrl.length()>0){
image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(imgUrl));
image.setDataHandler(dh);
//其中还要为图片设置Id
image.setContentID("aaa");
}
//附件
if(attachmentUrl.length()>0){
attachment = new MimeBodyPart();
DataHandler dh1 = new DataHandler(new FileDataSource(attachmentUrl));
attachment.setDataHandler(dh1);
attachment.setFileName(MimeUtility.encodeText(dh1.getName()));//特殊注意的地方 得自己设置名字 不然会随机分配的
}
System.out.println("-----------------------------------image="+image+"\nattachment="+attachment);
//把图片和正文结合成新的BodyPart //设置关系
MimeMultipart mm1 = new MimeMultipart();
mm1.addBodyPart(text);
if(image!=null){
mm1.addBodyPart(image);
mm1.setSubType("related");
}
//改成Bodypart
MimeBodyPart content = new MimeBodyPart();
content.setContent(mm1);
//把新的multipart和附件结合成更新的multipart
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(content);
if(attachment!=null){
mm.addBodyPart(attachment);
mm.setSubType("mixed");
//加载message中
}
message.setContent(mm);
Date d = new Date();
message.setSentDate(d);
message.saveChanges();
//4、发送邮件
ts.sendMessage(message, message.getAllRecipients());
//5、关闭邮件发送对象
ts.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String change(String temp){
if(temp!=null){
if(temp.trim().length()<1){
temp = "";
}
}else{
temp = "";
}
return temp;
}
}