具体代码::
/**
*
*/
package com.javamail;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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.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;
/**
* @author 86171
*
*/
public class Demo2 {
public static String myEmailAccount = "123456@qq.com";
public static String myEmailPassword = "授权码";
public static String myEmailSMTPHost = "smtp.qq.com";
public static String receiveMailAccount = "xxxx@163.com";
/**
* @param args
* @throws MessagingException
*/
public static void main(String[] args) throws MessagingException {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", myEmailSMTPHost);
properties.setProperty("mail.smtp.auth", "true");
final String smtpPort = "465";
properties.setProperty("mail.smtp.port", smtpPort);
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.socketFactory.port", smtpPort);
Session session = Session.getInstance(properties);
session.setDebug(true);
//1、创建邮件对象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(myEmailAccount));
message.setSubject("测试带附件的复杂邮件");
message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiveMailAccount));
message.setSentDate(new Date());
//message.setText("你好---------------hahah");
//2、创建MimeMultipart对象,该对象用来包含附件及正文信息。
Multipart multipart = new MimeMultipart("mixed");
message.setContent(multipart);//邮件内容
//正文
MimeBodyPart content = new MimeBodyPart();
//附件
MimeBodyPart fujian = new MimeBodyPart();
multipart.addBodyPart(fujian);
multipart.addBodyPart(content);
//3、构建附件附件
String filePath = new String("E:\\eclipse_work\\Test\\src\\com\\fororaclefun\\Area.java");
DataSource ds = new FileDataSource(new File(filePath));
DataHandler dh = new DataHandler(ds);
fujian.setDataHandler(dh);
fujian.setFileName("Area.java");
//4、构建邮件正文内容
MimeMultipart contentMimeMultiPart = new MimeMultipart("related");
content.setContent(contentMimeMultiPart);
MimeBodyPart emailHtml = new MimeBodyPart();
MimeBodyPart emailImg = new MimeBodyPart();
contentMimeMultiPart.addBodyPart(emailImg);
contentMimeMultiPart.addBodyPart(emailHtml);
String imgPath ="C:\\Users\\86171\\Documents\\WeChat Files\\anzimatlab\\Video\\3183909b9593eb5eb5d8729c65f687a7.jpg";
emailImg.setDataHandler(new DataHandler(new FileDataSource(imgPath)));
emailImg.setHeader("Content-location", "http://dog/3183909b9593eb5eb5d8729c65f687a7.jpg");
emailHtml.setContent("你好呀,这是我用代码发送给你的一封电子邮件!!!<br/><img src='http://dog/3183909b9593eb5eb5d8729c65f687a7.jpg'>", "text/html;charset=utf8");
message.saveChanges();
try(FileOutputStream fo = new FileOutputStream(new File("F://dd.eml"))) {
message.writeTo(fo);
} catch (IOException e) {
e.printStackTrace();
}
//发送电子邮件
Transport transport = session.getTransport();
transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
}
}