java实现发送一封带有附件的邮件

java实现一封带有附件的邮件

继上一篇javamail发邮件的博客接着写,如何实现发送一封带有附件的邮件

直接上代码

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
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;
import javax.mail.internet.MimeUtility;

public class MailFileSendTest {

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

        File affix1 = new File("d:/ceshi.xlsx");
        List<File> tempList = new ArrayList<File>();
        tempList.add(affix1);
        //发送邮件
        String fileName1 = "测试附件" ; //新文件名字
        String sendContent = "您好,你的订单统计表已经给您发送,详见附件!";//正文
        String mainTitle = "测试附件T-2";//标题
        sendFileMail(mainTitle,  sendContent, fileName1, tempList, "1********[email protected]");

    }

    /**
     * 发送带有附件的邮件
     *
     * @param mailTitle
     * @param sendContent
     * @param attachFileList
     * @Description:
     * @author
     * @date 2019年9月25日
     */
    public static void sendFileMail(String mailTitle, String sendContent, String fileName, List<File> attachFileList,String tomail) {

        Properties prop = new Properties();

        prop.put("mail.transport.protocol", "smtp");
        prop.put("mail.smtp.host", "smtp.163.com");
        prop.put("mail.smtp.auth", "true");
        final String smtpPort = "465";
        prop.setProperty("mail.smtp.port", smtpPort);
        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        prop.setProperty("mail.smtp.socketFactory.port", smtpPort);
        Session session = Session.getInstance(prop);
        try {
            // 发送邮件
            send(session, tomail, mailTitle, sendContent, attachFileList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 发送带附件的邮件
     *
     * @param session    发件人信息
     * @param subject    邮件标题
     * @param content    邮件内容
     * @param recipients 收件人地址
     */
    public static void send(Session session, String recipients, String subject, String content, List<File> attachments) {

        MimeMessage message = new MimeMessage(session);
        try {
            System.getProperties().setProperty("mail.mime.splitlongparameters", "false");

            String EmailAccount = "*******@163.com";// 发件人的邮箱
            String EmailPassword = "*********************";//客户端授权码***

            // 创建多重消息
            Multipart multipart = new MimeMultipart();

            message.setFrom(new InternetAddress(EmailAccount, "qc测试", "utf-8"));

            // 邮件标题
            message.setSubject(subject);
            // 添加邮件正文
            BodyPart contentPart = new MimeBodyPart();
            contentPart.setContent(content, "text/html;charset=UTF-8");
            multipart.addBodyPart(contentPart);

            message.setSentDate(new Date());
            // 创建邮件的接收者地址,并设置到邮件消息中
            Address to = new InternetAddress(recipients);
            // Message.RecipientType.TO属性表示接收者的类型为TO
            message.setRecipient(Message.RecipientType.TO, to);

            // 添加附件的内容
            if (attachments != null) {
                for (int i = 0; i < attachments.size(); i++) {
                    BodyPart attachmentBodyPart = new MimeBodyPart();
                    DataSource source = new FileDataSource(attachments.get(i));
                    attachmentBodyPart.setDataHandler(new DataHandler(source));
                    // MimeUtility.encodeWord可以避免文件名乱码
                    attachmentBodyPart.setFileName(MimeUtility.encodeText(attachments.get(i).getName()));
                    multipart.addBodyPart(attachmentBodyPart);
                }
            }

            System.out.println("发件人邮箱:" + EmailAccount);
            System.out.println("发件人的密码:" + EmailPassword);

            // 发送完整消息
            message.setContent(multipart);
            Transport transport = session.getTransport();
            transport.connect(EmailAccount, EmailPassword);// 敲门开门
            transport.sendMessage(message, message.getAllRecipients());// 送到对方手里

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

效果如下
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_49462255/article/details/109351241