java 发送附件1是文件,附件2是图片,内容含有图片的邮件

package com.common;

import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
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 SendImgaeAndAttachMail {

    // 发件人地址
    public static String senderAddress = "[email protected]";
    // 收件人地址
    public static String recipientAddress = "[email protected]";
    // 发件人账户名
    public static String senderAccount = "MrZou";
    // 发件人账户密码
    public static String senderPassword = "shenzhen";

    public static void main(String[] args) throws Exception {

        // 1、连接邮件服务器的参数配置
        Properties props = new Properties();
        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 发送服务器端口,可以不设置,默认是25
        props.setProperty("mail.smtp.port", "25");
        // 设置发送邮件协议名
        props.setProperty("mail.transport.protocol", "smtp");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.163.com");

        // 2、创建定义整个应用程序所需的环境信息的 Session 对象
        Session session = Session.getInstance(props, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 在session中设置账户信息,Transport发送邮件时会使用
                return new PasswordAuthentication(senderAccount, senderPassword);
            }
        });

        // 3、 创建一封邮件的实例对象
        MimeMessage msg = new MimeMessage(session);
        // 设置发件人地址
        msg.setFrom(new InternetAddress(senderAddress));
        // 设置邮件回复人
        msg.setReplyTo(new Address[] { new InternetAddress(senderAddress) });
        // 设置收件人地址
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientAddress));
        // 设置主题
        msg.setSubject("发送HTML+内嵌图片+附件的测试邮件", "utf-8");

        // 4、整封邮件的MINE消息体
        MimeMultipart msgMultipart = new MimeMultipart("mixed");
        // 设置邮件的MINE消息体
        msg.setContent(msgMultipart);

        // 5、 创建附件1"节点"
        MimeBodyPart attachment1 = new MimeBodyPart();
        // 读取本地文件
        String attch1Path = "D:\\test\\淘淘商城的第一天笔记.doc";
        DataSource ds1 = new FileDataSource(new File(attch1Path.replace("////", "/")));
        DataHandler dh1 = new DataHandler(ds1);
        // 将附件1数据添加到"节点"
        attachment1.setDataHandler(dh1);
        // 设置附件1的文件名
        attachment1.setFileName(MimeUtility.encodeText(dh1.getName()));
        // 把附件1加入到 MINE消息体中
        msgMultipart.addBodyPart(attachment1);

        // 6、创建附件2"节点"
        MimeBodyPart attachment2 = new MimeBodyPart();
        // 读取本地文件
        String attch2Path = "D:\\test\\hello.jpg";
        DataSource ds2 = new FileDataSource(new File(attch2Path.replace("////", "/")));
        DataHandler dh2 = new DataHandler(ds2);
        // 将附件2数据添加到"节点"
        attachment2.setDataHandler(dh2);
        // 设置附件2的文件名
        attachment2.setFileName(MimeUtility.encodeText(dh2.getName()));
        // 附件2加入到 MINE消息体中
        msgMultipart.addBodyPart(attachment2);

        // 7、创建正文节点,用于保存最终正文部分
        MimeBodyPart content = new MimeBodyPart();
        // 把正文加入到 MINE消息体中
        msgMultipart.addBodyPart(content);

        // 8、设置邮件正文
        // 一个Multipart对象包含一个或多个bodypart对象,组成邮件正文
        MimeMultipart bodyMultipart = new MimeMultipart("related");
        // 将上面"related"型的 MimeMultipart 对象作为邮件的正文
        content.setContent(bodyMultipart);

        // 读取本地图片,将图片数据添加到"节点"
        MimeBodyPart imagePart = new MimeBodyPart();
        String imgPath = "D:\\test\\mailTest.jpg";
        DataSource imgds = new FileDataSource(new File(imgPath.replace("////", "/")));
        DataHandler imgdh = new DataHandler(imgds);
        imagePart.setDataHandler(imgdh);
        imagePart.setContentID("mailTest");

        // 创建文本节点
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent("这是一张图片<br/><img src='cid:mailTest'/>", "text/html;charset=utf-8");

        // 将文本和图片添加到bodyMultipart
        bodyMultipart.addBodyPart(imagePart);
        bodyMultipart.addBodyPart(htmlPart);

        // 9、根据session对象获取邮件传输对象Transport
        Transport transport = session.getTransport();
        transport.connect();
        // 发送邮件,并发送到所有收件人地址
        transport.sendMessage(msg, msg.getAllRecipients());

        // 10、关闭邮件连接
        transport.close();
    }
}
 

猜你喜欢

转载自blog.csdn.net/qingcyb/article/details/83957824