JavaEmailツールクラス

package com.ceh.utils;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

/**
 * 发送邮件的工具类
 */
public class JavaEmailUtils {
    
    

     username  password  meEmail toemail tatil content
    public static void sendemail(String username,String password,String meEmail,String toEmail,String tatil,String content) throws Exception {
    
    
        //创建properties文件,并赋予参数
        Properties prop = new Properties();
        //设置发件人邮箱服务器地址(可在邮箱官网查看:qq:smtp.qq.com 端口号465/587)
        prop.setProperty("mail.host","smtp.qq.com");
        //邮件发送的协议
        prop.setProperty("mail.transport.protocol","smtp");
        //是否需要验证用户名和密码
        prop.setProperty("mail.smtp.auth","true");


        //ssl验证:qq邮箱是启用了ssl验证了,如果出现错误与ssl有关,就添加下面的代码
        // QQ邮箱设置SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);


        //创建发送邮件有关的session环境
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
    
    
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
    
    
                //传入发件人的姓名和授权码
                return new PasswordAuthentication(username,password);
            }
        });

        //开启debug模式
        session.setDebug(true);

        //2、通过session获取transport对象
        Transport transport = session.getTransport();
        //3、通过transport对象邮箱用户名和授权码连接邮箱服务器
        transport.connect("smtp.qq.com",meEmail,password);



        创建一个邮件
        MimeMessage mimeMessage = new MimeMessage(session);
        //发件人
        mimeMessage.setFrom(new InternetAddress(meEmail));
        //收件人
        mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(toEmail));
        //邮件标题
        mimeMessage.setSubject(tatil);

        //======================================================================================
        //邮件内容
        //准备图片数据
        MimeBodyPart image = new MimeBodyPart();
        DataHandler handler = new DataHandler(new FileDataSource("E:\\image\\hippyBirthday.jpeg"));
        image.setDataHandler(handler);
        image.setContentID("hippyBirthday.jpeg"); //设置图片id


        //准备文本  这是显示在正文中的内容,可以把图片引入过来
        MimeBodyPart text = new MimeBodyPart();
        text.setContent("<img src='cid:hippyBirthday.jpeg'><br>"+content,"text/html;charset=utf-8");

        //拼装邮件正文 -->  把图片和文本封装在一起,文本才能正常的引入图片
        MimeMultipart mimeMultipart = new MimeMultipart();
        mimeMultipart.addBodyPart(image);
        mimeMultipart.addBodyPart(text);
        mimeMultipart.setSubType("related");//文本和图片内嵌成功

        //将拼装好的正文内容设置为主体
        MimeBodyPart contentText = new MimeBodyPart();
        contentText.setContent(mimeMultipart);
        //=============================================================================

        //附件 --->在邮箱中下载的文件
        /*MimeBodyPart appendix = new MimeBodyPart();
        appendix.setDataHandler(new DataHandler(new FileDataSource("E:\\image\\haha.txt")));
        appendix.setFileName("test.txt");*/

        //拼接附件和正文到住体
        MimeMultipart allFile = new MimeMultipart();
        /*allFile.addBodyPart(appendix);*///附件
        allFile.addBodyPart(contentText);//正文
        allFile.setSubType("mixed"); //正文和附件都存在邮件中,所有类型设置为mixed

        //把所有的内容放到邮件中
        mimeMessage.setContent(allFile);
        mimeMessage.saveChanges();//保存修改

        //5、发送邮件
        transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());

        //释放资源
        transport.close();
    }

}

パラメータ

文字列ユーザー名:送信者の名前
文字列パスワード:送信者の認証コード
文字列meEmail:送信者のメールアドレス
文字列toEmail:受信者のメールアドレス
文字列tatil:タイトル
文字列コンテンツ:住宅のテキストコンテンツ
(写真や添付ファイルも抽出できます)

おすすめ

転載: blog.csdn.net/weixin_43431123/article/details/111999959