https://blog.csdn.net/xyang81/article/details/7672745
https://blog.csdn.net/wenwen1538/article/category/2220745
https://www.cnblogs.com/xdp-gacl/p/4216311.html
package gaofeng.socket;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
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 MailClient {
private Session session;
private Transport ts;
public void init() throws MessagingException {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
session = Session.getInstance(props);
session.setDebug(true);
ts = session.getTransport();
ts.connect("smtp.163.com", "ggaofengg", "163第三方邮件客户端授权码");
}
public void sendMail() throws Exception {
Message msg = createJpgMail();//createSimpleMail();
ts.sendMessage(msg, msg.getAllRecipients());
ts.close();
}
public MimeMessage createSimpleMail() throws Exception {
MimeMessage message = new MimeMessage(this.session);
message.setFrom(new InternetAddress("ggaofengg@163.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("21565038@qq.com"));
message.setSubject("mm只包含文本的简单邮件");
message.setContent("<h1>你好啊!</h1>", "text/html;charset=UTF-8");
return message;
}
public MimeMessage createJpgMail() throws Exception {
MimeMessage message = new MimeMessage(this.session);
message.setFrom(new InternetAddress("ggaofengg@163.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("gaofeng609@qq.com"));
message.setSubject("mm只包含文本的简单邮件");
MimeBodyPart text = new MimeBodyPart();
text.setContent("这是一封邮件正文带图片<img src='cid:xxx.jpg'>的邮件", "text/html;charset=UTF-8");
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("G:\\21565038\\gaofengEbook\\数字图像处理java编程与实验源码\\imProcess\\images\\ch15\\jpg\\j_bird2.jpg"));
image.setDataHandler(dh);
image.setContentID("xxx.jpg");
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
message.setContent(mm);
return message;
}
public static void main(String[] args) throws Exception {
MailClient client = new MailClient();
client.init();
client.sendMail();
}
}