package ccs;
import java.io.FileOutputStream;
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;
/**
/
public class SendEmail_Photo_Attachments {
public static void main(String[] args) throws Exception {
String mailTo = “42306328@qq.com”;
String mailTittle = “邮件带附件、图片”;
String mailText ="邮件内容如下*********";
String [] images = {“D:\相册\1.gif”,“D:\相册\Photos\3111455QZ-2[1].gif”};
String [] attach={“C:\Users\Administrator\Desktop\sql.txt”,“D:\相册\Photos\3111455QZ-2[1].gif”};
sendMessage(“42306328@qq.com”,“cs20190808lh”,mailTittle,mailText,mailTo,images,attach);
}
/**
***
* @param mailFrom 指明邮件的发件人
* @param password_mailFrom 邮件的发件人登陆密码 TODO QQ邮箱需要在邮箱设置里面:开启POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,为安全起见使用生成的授权码
* @param mailTittle 邮件的标题
* @param mailText 邮件的文本内容
* @param mailTo 指明邮件的收件人
* @param images 图片
* @param parameter 附件
* @throws Exception
/
public static void sendMessage(String mailFrom,String password_mailFrom,String mailTittle,String mailText,String mailTo,String []images,String …attach) throws Exception {
String mail_host = “smtp.qq.com”;// TODO 需要修改发送服务器
Properties prop = new Properties();
prop.setProperty(“mail.host”, mail_host);
prop.setProperty(“mail.transport.protocol”, “smtp”);
prop.setProperty(“mail.smtp.auth”, “true”);
//prop.setProperty(“mail.smtp.auth.mechanisms”, “NTLM”);
// 使用JavaMail发送邮件的5个步骤
// 1、创建session
Session session = Session.getInstance(prop);
// 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
// 2、通过session得到transport对象
Transport ts = session.getTransport();
// 3、连上邮件服务器,需要发件人提供邮箱的用户名和密码进行验证
ts.connect(mail_host, mailFrom, password_mailFrom);// 需要修改
// 4、创建邮件
Message message = createMixedMail(session,mailTittle,mailText,mailFrom,mailTo,images,attach);
// 5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}*
/**
* @Method: createMixedMail
* @Description: 生成一封带附件和带图片的邮件
/
public static MimeMessage createMixedMail(Session session,String title,String mailContent, String mailFrom,String mailTo,String []imgs,String …attach) throws Exception {
MimeMessage message = new MimeMessage(session);
// 设置邮件的基本信息
message.setFrom(new InternetAddress(mailFrom)); // 发件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));// 收件人
message.setSubject(title);
// 正文
MimeBodyPart text = new MimeBodyPart();
text.setContent(mailContent+"
", “text/html;charset=UTF-8”);
// 描述关系:正文和图片
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(text);
mp1.setSubType(“related”);
//图片
for (int i = 0; i < imgs.length; i++) {
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource(imgs[i])));
image.setContentID(“aaa.jpg”);//将图片设置在内容中
mp1.addBodyPart(image);
}
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp1);
// 代表正文的bodypart
MimeMultipart mp2 = new MimeMultipart();
mp2.addBodyPart(content);
mp2.setSubType(“mixed”);
// 附件1********************************
// 描述关系:正文和附件
for (int i = 0; i < attach.length; i++) {
MimeBodyPart attachFile = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(attach[i]));
attachFile.setDataHandler(dh);
attachFile.setFileName(dh.getName());
mp2.addBodyPart(attachFile);
}
//************************************
message.setContent(mp2);
message.saveChanges();
message.writeTo(new FileOutputStream(“F:/ImageMail.eml”));//邮件发送记录
// 返回创建好的的邮件
return message;
}
}