栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

邮件发送功能(Java语言实现)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

邮件发送功能(Java语言实现)

 

引入依赖:

            javax.mail
            mail
            1.4.7
        
        
            javax.activation
            activation
            1.1.1
        

首先在qq邮箱中开启邮件收发功能

发送简单邮件(纯文本)
package com.fdw.mail;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class MailTest {
    //发送简单邮件
    public static void main(String[] args) throws Exception {
        //要发送协议,需要获得协议和支持,开启服务POP3/SMTP
        //授权码:oqudlrdmigwydigj
        final String passCode = "授权码";
        final String fromAddress = "发件邮箱";
         String toAddress = "收件邮箱";
         String subject = "天天开心";
        //邮件内容本质是html
         String content = "

你好啊"; String type = "text/html;charset=utf-8"; Properties prop = new Properties(); //设置QQ邮件服务器 prop.setProperty("mail.host","smtp.qq.com"); //邮件发送协议 prop.setProperty("mail.transport.protocol","smtp"); //需要验证用户名密码 prop.setProperty("mail.smtp.auth","true"); //关于QQ邮箱,还要设置SSL加密,加上一下代码即可 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable","true"); prop.put("mail.smtp.ssl.socketFactory",sf); //使用javamail发送邮件的5个步骤 //1.创建定义整个应用程序所需的环境信息的session对象 //QQ才有 Session session = Session.getDefaultInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication( ) { return new PasswordAuthentication(fromAddress,passCode); } }); //session.setDebug(true); //2.通过session得到transport对象 Transport ts = session.getTransport(); //3.使用邮箱的用户名和授权码连上邮件服务器 ts.connect("smtp.qq.com",fromAddress,passCode); //4.写邮件,注意需要传递session MimeMessage message = new MimeMessage(session); //发件人 message.setFrom(new InternetAddress(fromAddress)); //收件人 message.setRecipient(Message.RecipientType.TO,new InternetAddress(toAddress)); //邮件主题 message.setSubject(subject); //正文 message.setContent(content,type); //5.发送邮件 ts.sendMessage(message,message.getAllRecipients()); System.out.println("邮件发送成功"); //6.关闭连接 ts.close(); } }

 效果:

发送复杂邮件(带附件和图片)

 

代码
package com.fdw.mail;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class MailTest2 {
    //发送复杂邮件
    public static void main(String[] args) throws Exception {
        //要发送协议,需要获得协议和支持,开启服务POP3/SMTP
        //授权码:oqudlrdmigwydigj
        final String passCode = "oqudlrdmigwydigj";
        final String fromAddress = "2530155337@qq.com";
        String toAddress = "2043154068@qq.com";
        String subject = "天天开心";
        String content = "这是一封邮件带图片的邮件";
        String type = "text/html;charset=utf-8";
        String imagePath = "E:\IDEA项目\JavaWebStudy\Servlet-01\src\main\resources\1.png";
        String filePath = "E:\IDEA项目\JavaWebStudy\Servlet-01\src\main\resources\请假申请表.docx";
        String imageID = "bz.jpg";
        String fileName = "请假申请表.docx";



        Properties prop = new Properties();
        //设置QQ邮件服务器
        prop.setProperty("mail.host","smtp.qq.com");
        //邮件发送协议
        prop.setProperty("mail.transport.protocol","smtp");
        //需要验证用户名密码
        prop.setProperty("mail.smtp.auth","true");
        prop.setProperty("mail.mime.splitlongparameters","false");
        prop.setProperty("mail.mime.charset","utf-8");

        //关于QQ邮箱,还要设置SSL加密,加上一下代码即可
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable","true");
        prop.put("mail.smtp.ssl.socketFactory",sf);

        //使用javamail发送邮件的5个步骤

        //1.创建定义整个应用程序所需的环境信息的session对象
        //QQ才有
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication( ) {
                return new PasswordAuthentication(fromAddress,passCode);
            }
        });
        //session.setDebug(true);
        //2.通过session得到transport对象
        Transport ts = session.getTransport();

        //3.使用邮箱的用户名和授权码连上邮件服务器
        ts.connect("smtp.qq.com",fromAddress,passCode);
        //4.写邮件,注意需要传递session
        MimeMessage message = new MimeMessage(session);

        //发件人
        message.setFrom(new InternetAddress(fromAddress));
        //收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(toAddress));

        //邮件主题
        message.setSubject(subject);

        //文本
        MimeBodyPart text = new MimeBodyPart();
        text.setContent(content,type);

        //图片
        MimeBodyPart image = new MimeBodyPart();
        //图片需要经过数据处理
        FileDataSource imageDataSource = new FileDataSource(imagePath);
        DataHandler dh = new DataHandler(imageDataSource);
        image.setDataHandler(dh);
        //给图片设置一个ID,方便调用,cid:imageID
        image.setContentID(imageID);

        //附件
        MimeBodyPart file = new MimeBodyPart();
        FileDataSource fileDataSource = new FileDataSource(filePath);
        DataHandler dh2 = new DataHandler(fileDataSource);
        file.setDataHandler(dh2);
        //给附件设置文件名,并处理中文乱码
        file.setFileName(MimeUtility.encodeText(fileName));


        //描述数据关系
        MimeMultipart mm = new MimeMultipart();
        mm.addBodyPart(text);
        mm.addBodyPart(image);
        mm.setSubType("related");

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


        //拼接附件
        MimeMultipart allFile = new MimeMultipart();
        allFile.addBodyPart(file);
        allFile.addBodyPart(contentText);
        allFile.setSubType("mixed");


        //设置到消息中,保存修改
        message.setContent(allFile);
        message.saveChanges();
        //5.发送邮件
        ts.sendMessage(message,message.getAllRecipients());
        System.out.println("邮件发送成功");
        //6.关闭连接
        ts.close();

    }
}
效果

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/919158.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号