【Mail】JavaMail发送带附件的邮件(二) 【Mail】JavaMail介绍及发送邮件(一)

上一篇讲了使用JavaMail发送普通邮件(【Mail】JavaMail介绍及发送邮件(一)),本例讲发送复杂的邮件(带有附件的邮件)

生成一封复杂的邮件

  1.   新建一个JavaWeb的Maven工程,引入javamail.jar包,maven引用如下:
 <!-- javamail -->
 <dependency>
     <groupId>javax.mail</groupId>
     <artifactId>mail</artifactId>
     <version>1.4.7</version>
 </dependency>

     2 .新建邮件的结构图,以及邮件MIME关系图如下
     

     3.新建一个Demo3.java,如下:

package com.hd.javamail;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
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;

import com.sun.mail.util.MimeUtil;

/**
 * 
 * @author H__D
 * @date 2016年12月6日 下午7:38:33
 *
 */
public class Demo3 {
    
    /**
     * 生成一份本地的邮件
     * @param args
     * @throws MessagingException
     * @throws IOException 
     */
    public static void main(String[] args) throws MessagingException, IOException {
        //环境
        Session session = Session.getDefaultInstance(new Properties());
        
        //邮件
        MimeMessage msg = new MimeMessage(session);
        //设置主题
        msg.setSubject("test123456");
        //发件人,注意中文的处理
        msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText( "某某") + "\"<[email protected]>"));
        //设置邮件回复人
        msg.setReplyTo(new Address[]{new InternetAddress("[email protected]")});
        
        //整封邮件的MINE消息体
        MimeMultipart msgMultipart = new MimeMultipart("mixed");//混合的组合关系
        //设置邮件的MINE消息体
        msg.setContent(msgMultipart);
        
        //附件1
        MimeBodyPart attch1 = new MimeBodyPart();
        //附件2
        MimeBodyPart attch2 = new MimeBodyPart();
        //正文内容
        MimeBodyPart content = new MimeBodyPart();
        
        //把内容,附件1,附件2加入到 MINE消息体中
        msgMultipart.addBodyPart(attch1);
        msgMultipart.addBodyPart(attch2);
        msgMultipart.addBodyPart(content);
        
        //把文件,添加到附件1中
        //数据源
        DataSource ds1 = new FileDataSource(new File("C:/Users/H__D/Desktop/1.txt"));
        //数据处理器
        DataHandler dh1 = new DataHandler(ds1 );
        //设置第一个附件的数据
        attch1.setDataHandler(dh1);
        //设置第一个附件的文件名
        attch1.setFileName("file1.jpg");
        
        //把文件,添加到附件2中
        DataSource ds2 = new FileDataSource(new File("C:/Users/H__D/Desktop/2.txt"));
        DataHandler dh2 = new DataHandler(ds2 );
        attch2.setDataHandler(dh2);
        attch2.setFileName(MimeUtility.encodeText( "文件2.jpg"));
        
        //正文(图片和文字部分)
        MimeMultipart bodyMultipart  = new MimeMultipart("related");
        //设置内容为正文
        content.setContent(bodyMultipart);
        
        //html代码部分
        MimeBodyPart htmlPart = new MimeBodyPart();
        //html中嵌套的图片部分
        MimeBodyPart imgPart = new MimeBodyPart();
        
        //正文添加图片和html代码
        bodyMultipart.addBodyPart(htmlPart);
        bodyMultipart.addBodyPart(imgPart);
        
        //把文件,添加到图片中
        DataSource imgds = new FileDataSource(new File("C:/Users/H__D/Desktop/logo.png"));
        DataHandler imgdh = new DataHandler(imgds );
        imgPart.setDataHandler(imgdh);
        //说明html中的img标签的src,引用的是此图片
        imgPart.setHeader("Content-Location", "http://sunteam.cc/logo.jsg");
        
        //html代码
        htmlPart.setContent("<span style='color:red'>中文呵呵</span><img src=\"http://sunteam.cc/logo.jsg\">","text/html;charset=utf-8");
        
        //生成文件邮件
        msg.saveChanges();
        
        //输出
        OutputStream os = new FileOutputStream("C:/Users/H__D/Desktop/demo.eml");
        msg.writeTo(os);
        os.close();
    }
    
}

4.    运行main方法,等到邮件文件。可以使用记事本和Foxmail打开效果如下:

  1. 记事本效果:


    Foxmai效果: 

 发送本地邮件文件

    •   新建一个Demo4.类,如下:
package com.hd.javamail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 
 * @author H__D
 * @date 2016年12月6日 下午7:51:43
 *
 */
public class Demo4 {
    public static void main(String[] args) throws FileNotFoundException, MessagingException {
        // 属性对象
        Properties properties = new Properties();
        // 开启debug调试 ,打印信息
        properties.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        properties.setProperty("mail.smtp.auth", "true");
        // 发送服务器端口,可以不设置,默认是25
        properties.setProperty("mail.smtp.port", "25");
        // 发送邮件协议名称
        properties.setProperty("mail.transport.protocol", "smtp");
        // 设置邮件服务器主机名
        properties.setProperty("mail.host", "smtp.163.com");
        // 环境信息
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 在session中设置账户信息,Transport发送邮件时会使用
                return new PasswordAuthentication( "[email protected]", "xxxxx");
            }
        });
        
        //读取本地邮件
        Message message = new MimeMessage(session, new FileInputStream(new File("C:/Users/H__D/Desktop/demo.eml")));
        
        //发送邮件
        Transport.send(message, InternetAddress.parse("[email protected]") );
    }
}

此文来源于: https://www.cnblogs.com/h--d/p/6138900.html

猜你喜欢

转载自www.cnblogs.com/JonaLin/p/11269558.html