zoukankan      html  css  js  c++  java
  • java实现邮件发送

    使用到的jar包:

    Java实现纯文本邮件发送

     1 package org.westos.email;
     2 
     3 import com.sun.mail.util.MailSSLSocketFactory;
     4 
     5 import javax.mail.*;
     6 import javax.mail.internet.InternetAddress;
     7 import javax.mail.internet.MimeMessage;
     8 import java.security.GeneralSecurityException;
     9 import java.util.Properties;
    10 
    11 public class SendEamil {
    12     public static void main(String[] args) throws MessagingException, GeneralSecurityException {
    13         //创建一个配置文件并保存
    14         Properties properties = new Properties();
    15 
    16         properties.setProperty("mail.host","smtp.qq.com");
    17 
    18         properties.setProperty("mail.transport.protocol","smtp");
    19 
    20         properties.setProperty("mail.smtp.auth","true");
    21 
    22 
    23         //QQ存在一个特性设置SSL加密
    24         MailSSLSocketFactory sf = new MailSSLSocketFactory();
    25         sf.setTrustAllHosts(true);
    26         properties.put("mail.smtp.ssl.enable", "true");
    27         properties.put("mail.smtp.ssl.socketFactory", sf);
    28 
    29         //创建一个session对象
    30         Session session = Session.getDefaultInstance(properties, new Authenticator() {
    31             @Override
    32             protected PasswordAuthentication getPasswordAuthentication() {
    33                 return new PasswordAuthentication("619046217@qq.com","16位授权码");
    34             }
    35         });
    36 
    37         //开启debug模式
    38         session.setDebug(true);
    39 
    40         //获取连接对象
    41         Transport transport = session.getTransport();
    42 
    43         //连接服务器
    44         transport.connect("smtp.qq.com","619046217@qq.com","16位授权码");
    45 
    46         //创建邮件对象
    47         MimeMessage mimeMessage = new MimeMessage(session);
    48 
    49         //邮件发送人
    50         mimeMessage.setFrom(new InternetAddress("619046217@qq.com"));
    51 
    52         //邮件接收人
    53         mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("875203654@qq.com"));
    54 
    55         //邮件标题
    56         mimeMessage.setSubject("Hello Mail");
    57 
    58         //邮件内容
    59         mimeMessage.setContent("我的想法是把代码放进一个循环里","text/html;charset=UTF-8");
    60 
    61         //发送邮件
    62         transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
    63 
    64         //关闭连接
    65         transport.close();
    66     }
    67 }

    Java实现文本图片附件复杂的邮件发送
    MIME(多用途互联网邮件扩展类型)

    MimeBodyPart类

    javax.mail.internet.MimeBodyPart类 表示的是一个MIME消息,它和MimeMessage类一样都是从Part接口继承过来。

    MimeMultipart类

    javax.mail.internet.MimeMultipart是抽象类 Multipart的实现子类,它用来组合多个MIME消息。一个MimeMultipart对象可以包含多个代表MIME消息的MimeBodyPart对象

      1 package org.westos.email;
      2 
      3 import com.sun.mail.util.MailSSLSocketFactory;
      4 
      5 import javax.activation.DataHandler;
      6 import javax.activation.FileDataSource;
      7 import javax.mail.*;
      8 import javax.mail.internet.*;
      9 import java.security.GeneralSecurityException;
     10 import java.util.Properties;
     11 
     12 public class SendComplexEmail {
     13     public static void main(String[] args) throws GeneralSecurityException, MessagingException {
     14         Properties prop = new Properties();
     15         prop.setProperty("mail.host", "smtp.qq.com");  设置QQ邮件服务器
     16         prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议
     17         prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码
     18 
     19         // QQ邮箱设置SSL加密
     20         MailSSLSocketFactory sf = new MailSSLSocketFactory();
     21         sf.setTrustAllHosts(true);
     22         prop.put("mail.smtp.ssl.enable", "true");
     23         prop.put("mail.smtp.ssl.socketFactory", sf);
     24 
     25         //1、创建定义整个应用程序所需的环境信息的 Session 对象
     26         Session session = Session.getDefaultInstance(prop, new Authenticator() {
     27             @Override
     28             protected PasswordAuthentication getPasswordAuthentication() {
     29                 //传入发件人的姓名和授权码
     30                 return new PasswordAuthentication("619046217@qq.com","16位授权码");
     31             }
     32         });
     33 
     34         //2、通过session获取transport对象
     35         Transport transport = session.getTransport();
     36 
     37         //3、通过transport对象邮箱用户名和授权码连接邮箱服务器
     38         transport.connect("smtp.qq.com","619046217@qq.com","16位授权码");
     39 
     40         //4、创建邮件,传入session对象
     41         MimeMessage mimeMessage = complexEmail(session);
     42 
     43         //5、发送邮件
     44         transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
     45 
     46         //6、关闭连接
     47         transport.close();
     48 
     49 
     50     }
     51 
     52     public static MimeMessage complexEmail(Session session) throws MessagingException {
     53         //消息的固定信息
     54         MimeMessage mimeMessage = new MimeMessage(session);
     55 
     56         //发件人
     57         mimeMessage.setFrom(new InternetAddress("619046217@qq.com"));
     58         //收件人
     59         mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("619046217@qq.com"));
     60         //邮件标题
     61         mimeMessage.setSubject("带图片和附件的邮件");
     62 
     63         //邮件内容
     64         //准备图片数据
     65         MimeBodyPart image = new MimeBodyPart();
     66         DataHandler handler = new DataHandler(new FileDataSource("E:\IdeaProjects\WebEmail\resources\测试图片.png"));
     67         image.setDataHandler(handler);
     68         image.setContentID("test.png"); //设置图片id
     69 
     70         //准备文本
     71         MimeBodyPart text = new MimeBodyPart();
     72         text.setContent("这是一段文本<img src='cid:test.png'>","text/html;charset=utf-8");
     73 
     74         //附件
     75         MimeBodyPart appendix = new MimeBodyPart();
     76         appendix.setDataHandler(new DataHandler(new FileDataSource("E:\IdeaProjects\WebEmail\resources\测试文件.txt")));
     77         appendix.setFileName("test.txt");
     78 
     79         //拼装邮件正文
     80         MimeMultipart mimeMultipart = new MimeMultipart();
     81         mimeMultipart.addBodyPart(image);
     82         mimeMultipart.addBodyPart(text);
     83         mimeMultipart.setSubType("related");//文本和图片内嵌成功
     84 
     85         //将拼装好的正文内容设置为主体
     86         MimeBodyPart contentText = new MimeBodyPart();
     87         contentText.setContent(mimeMultipart);
     88 
     89         //拼接附件
     90         MimeMultipart allFile = new MimeMultipart();
     91         allFile.addBodyPart(appendix);//附件
     92         allFile.addBodyPart(contentText);//正文
     93         allFile.setSubType("mixed"); //正文和附件都存在邮件中,所有类型设置为mixed
     94 
     95 
     96         //放到Message消息中
     97         mimeMessage.setContent(allFile);
     98         mimeMessage.saveChanges();//保存修改
     99 
    100         return mimeMessage;
    101     }
    102 }

    QQ邮箱授权码:

    QQ邮箱需获取相应的权限:

    QQ邮箱–>邮箱设置–>账户–>POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 开启POP3/SMTP服务,然后获取16位授权码(注意不要将授权码泄露,一个账户可以拥有多个授权码)

  • 相关阅读:
    Java集合学习(8):LinkedList
    Java集合学习(7):ArrayList
    Java集合学习(6):LinkedHashSet
    数据结构与算法19—排序
    ZooKeeper基本介绍
    Java集合学习(5):LinkedHashMap
    Java集合学习(4):HashTable
    Java集合学习(3):HashSet
    java集合学习(2):Map和HashMap
    HTML DOM的创建,删除及替换
  • 原文地址:https://www.cnblogs.com/smartisn/p/14090706.html
Copyright © 2011-2022 走看看