JavaMail 发送一封含有图片 附件的邮件

学习资源:b站 哈哈哈哈哈哈哈哈
下面的代码都是跟着老师敲的

单节点 && 复合节点

单节点 MimeBodyPart :
图片节点
文字节点
附件节点

复合节点 MimeMultipart :
就是两两结合 或是三者结合

直接上代码:

package com.testjavamail;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
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;

public class TestMail02 {
	public static void main(String[] args) throws Exception{
		Properties props = createProperty();
		Session session = Session.getInstance(props);// Session类的构造器私有
		//创建邮件
		MimeMessage msg = createMimeMessage(session, "[email protected]", "[email protected]")
		// 发送邮件 首先  创建连接对象
		Transport transport = session.getTransport();
		//创建连接    其中密码以 “授权码”的形式体现  设置 账户 POP3/SMTP服务
		transport.connect("[email protected]", "ncufvtsyfdnbehgh");
		//发送邮件
		transport.sendMessage(msg, msg.getAllRecipients());
		//关闭连接
		transport.close();
	}
	public static MimeMessage createMimeMessage(Session session, String send, String recipient) throws Exception{
		MimeMessage msg = new MimeMessage(session);
		//邮件里面包含的内容
		Address address1 = new InternetAddress(send, "发件人", "UTF-8");
		Address address2 = new InternetAddress(recipient, "收件人", "UTF-8");
		msg.setFrom(address1);
		msg.setSubject("邮件的标题", "UTF-8");
		msg.setRecipient(MimeMessage.RecipientType.TO, address2);
		//msg.setContent("邮件的主体", "text/html;charset=utf-8");
//------------------------------------------------------------------------------		
		// 创建图片节点
		MimeBodyPart imagePart = new MimeBodyPart();
		DataHandler imageDataHandler = new DataHandler(new FileDataSource("src/images/1.jpg"));
		imagePart.setDataHandler(imageDataHandler);
		imagePart.setContentID("img01");//为图片节点添加id
		
		//创建文本节点: 是为了加载图片节点
		MimeBodyPart textPart = new MimeBodyPart();
		textPart.setContent("image:<img src='cid:img01' />", "text/html;charset=utf-8");
		//textPart.setContent("image:<image src='cid:img01' />", "text/html;charset=utf-8");
		//<img src="" /> 这才是标准的写法!!!                                                                                                                                                     
		//将图片节点和文本节点 组装成复合节点       
		MimeMultipart img_text = new MimeMultipart();
		img_text.addBodyPart(imagePart);
		img_text.addBodyPart(textPart);
		img_text.setSubType("related"); //设置与正文的关系 : 关联关系
		
		//将复合节点变成单节点   因为在正文中只能出现单节点  不能出现复合节点
		//这里不明白  在使用setContent()方法的时候 里面的参数只有MimeMultupart 并没有MimeBodyPart 
		MimeBodyPart imgtext = new MimeBodyPart();
		imgtext.setContent(img_text);
		
		//附件:     附加节点不是在正文中  
		MimeBodyPart attachment = new MimeBodyPart();
		DataHandler fileDataHandler = new DataHandler(new FileDataSource("src/images/1.jpg"));
		attachment.setDataHandler(fileDataHandler);
		//设置一个名字
		attachment.setFileName(MimeUtility.encodeText(fileDataHandler.getName()));
		
		//将刚处理好的“文本+图片”节点 与 附件设置成一个新的节点 此时的附件节点与正文的关系是 混合关系
		MimeMultipart mm = new MimeMultipart();
		mm.addBodyPart(imgtext);
		mm.addBodyPart(attachment);
		mm.setSubType("mixed");
		
		msg.setContent(mm, "text/html;charset=utf-8");
//------------------------------------------------------------------------------		
		msg.setSentDate(new Date());  //设置发送时间
		msg.saveChanges(); // 保存邮件
		return msg;
	}
	
	
	public static Properties createProperty(){
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp"); //协议
		props.setProperty("mail.smtp.host", "smtp.qq.com"); //地址
		props.setProperty("mail.smtp.port", "465"); //端口
		props.setProperty("mail.smtp", "true"); //验证
		//如果是163邮箱的话 上面的就已经可以了  但是qq邮箱还需要进行SSL验证
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.socketFactory.port", "465");
		return props;
	}
}

猜你喜欢

转载自blog.csdn.net/zf2015800505/article/details/84715607