java邮件传输图片

java邮件传输图片

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>javaweb-02-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>9</maven.compiler.source>
        <maven.compiler.target>9</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.0</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0-b01</version>
        </dependency>
    </dependencies>

</project>

java代码

package com.li;
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.security.GeneralSecurityException;
import java.util.Properties;

public class easyEmail {
    public static void main(String[] args) throws GeneralSecurityException, MessagingException {
        //可进行获取相应的资源
        Properties properties =new Properties();
        properties.setProperty("mail.host","smtp.qq.com");//设置qq邮件的服务器
        properties.setProperty("mail.transport.protocol","smtp");//邮件的发送协议
        properties.setProperty("mail.smtp.auth","true");//需要验证用户的密码


        //惯有QQ邮箱,还需要设置SSL加密,加上一下代码即可
        MailSSLSocketFactory sf= new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable","true");//表示采用ssl连接
        properties.put("mail.smtp.ssl.socketFactory",sf);//表示自己的工厂


        //使用JavaMail发送邮件的5哥步骤
        /*
        * 1、创建定义整个应用程序所需要的环境信息的session对象
        * 2、通过session得到transport对象
        * 3、连接服务器
        * 4、创建邮件
        * 5、发送邮件
        * */


        //1、创建定义整个应用程序所需要的环境信息的session对象
        //注: 只有在qq邮箱之中才会存在
        Session session=Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("1320155788@qq.com","fuejvoautrrsfieg");
            }
        });
        //查看邮件传输的状态
        session.setDebug(true);


        //2、通过session得到transport对象
        Transport transport=session.getTransport();//进行邮件服务器的连接


        //3、连接服务器
        transport.connect("smtp.qq.com","1320155788@qq.com","fuejvoautrrsfieg");


        //4、创建邮件===============================================================
        //注意:需要传递Session
        MimeMessage message=new MimeMessage(session);
        //指明邮件的发送人
        message.setFrom(new InternetAddress("1320155788@qq.com"));
        //指明邮件的收件人,
        message.setRecipient(Message.RecipientType.TO,new InternetAddress("1320155788@qq.com"));
        //邮件的标题
        message.setSubject("世界欢迎你");

        //=======================================================================================================
        //准备图片的数据
        MimeBodyPart image=new MimeBodyPart();
        //图片需要数据处理  DataHandler进行图片的处理
        DataHandler dataHandler=new DataHandler(new FileDataSource("D:\\IDEA\\javaweb-02-maven\\src\\main\\resources\\1.jpg"));
        //在我们的body主题中放入我们处理的数据
        image.setDataHandler(dataHandler);
        //给图片设置ID
        image.setContentID("nh.jpg");


        //准备正文的数据
        MimeBodyPart text=new MimeBodyPart();
        text.setContent("这是一封邮件正文带图片<img src='cid:nh.jpg'>","text/html;charset=UTF-8");

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



        //设置到消息中,保存修改
        message.setContent(mm);
        //保存修改
        message.saveChanges();

        //=======================================================================================================


        // 5、发送邮件
        transport.sendMessage(message,message.getAllRecipients());
        //关闭连接
        transport.close();

    }
}

上一篇:Ajax axios get请求 时钟案例


下一篇:JavaWeb详解(第一篇)之Servlet基础简介