zoukankan      html  css  js  c++  java
  • 自己动手写一个邮件工具

    邮件服务是项目中常用的,看到别人写了一个,手痒了,自己也整一个。

    一:邮箱的基本概念

     POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议。它是因特网电子邮件的第一个离线协议标准,POP3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的。

    SMTP 的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。 
    SMTP 认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录 SMTP 服务器,这就使得那些垃圾邮件的散播者无可乘之机。 增加 SMTP 认证的目的是为了使用户避免受到垃圾邮件的侵扰。

    IMAP全称是Internet Mail Access Protocol,即交互式邮件存取协议,它是跟POP3类似邮件访问标准协议之一。不同的是,开启了IMAP后,您在电子邮件客户端收取的邮件仍然保留在服务器上,同时在客户端上的操作都会反馈到服务器上,如:删除邮件,标记已读等,服务器上的邮件也会做相应的动作。所以无论从浏览器登录邮箱或者客户端软件登录邮箱,看到的邮件以及状态都是一致的。

    二:新浪邮箱开启服务

    新浪邮箱开启客户端认证:

    接受完验证码填写之后,记录下验证码,同时开启POP、SMTP和IMAP、SMTP

    在记录下新浪微博的POP3、SMTP服务器地址:

     

    二:163邮箱开启服务

     163邮箱打开设置点击POP3服务和SMTP服务:

    开启POP3服务和SMTP服务

    在记录下163邮箱的POP3、SMTP服务器地址:

    三:QQ邮箱开启服务

    QQ邮箱开启:

    设置--->账户

    后续相大概同步骤不再重复叙述

    四:上代码

    先加载一个jar包

    链接: https://pan.baidu.com/s/1YDF1ZKu0ldInx0cU7SX4Bw&shfl=shareset 提取码: istf 

     注:163邮箱必须由一年的使用时间才可以正常的发送,接收好像也不行。

     注:QQ邮箱发送的密码是授权码,不是自己的密码。

    不带附件的格式:

    import java.util.Properties;

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

    public class SendEmail {

      public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.sina.com");
        props.setProperty("mail.smtp.auth", "true");

        Session session = Session.getInstance(props,);
        // 这里设置可以查看打印日志
        session.setDebug(true);

        Message message = new MimeMessage(session);
        // 新浪发送邮件必须要有主题哦,否则永远无法发送成功!
        message.setSubject("标题");
        message.setText("内容");

        // 现在这里发送人必须与认证人一致,否则报错
        message.setFrom(new InternetAddress("发件人邮箱"));

        Transport transport = session.getTransport();
        transport.connect("smtp.sina.com", 25, "发件人邮箱", "发件人密码");
        transport.sendMessage(message, new Address[]{new InternetAddress("收件人邮箱")});
      }
    }

    带附件的代码

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Address;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;

    public class SendEmail {

      @SuppressWarnings("restriction")
      public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.sina.com");
        props.setProperty("mail.smtp.auth", "true");

        Session session = Session.getInstance(props,
          new Authenticator(){
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
            //返回发件人账号与密码信息
              return new PasswordAuthentication("发件人","授权码");
            }
          }
        );
        // 这里设置可以查看打印日志
        session.setDebug(true);

        Message message = new MimeMessage(session);
        // 新浪发送邮件必须要有主题哦,否则永远无法发送成功!
        message.setSubject("被退信的通知:");
        message.setFrom(new InternetAddress("发件人"));

        MimeMultipart mixed = new MimeMultipart("mixed");// 构建一个总的邮件块
        message.setContent(mixed);

        DataSource leftDataSource = new FileDataSource(new File("D:\JAVA\data\1.txt"));
        MimeBodyPart left = new MimeBodyPart();// 附件
        left.setDataHandler(new DataHandler(leftDataSource));
        left.setFileName("1.txt");
        mixed.addBodyPart(left);
      
        DataSource rightDataSource = new FileDataSource(new File("D:\JAVA\data\2.txt"));
        MimeBodyPart right = new MimeBodyPart();// 附件
        right.setDataHandler(new DataHandler(rightDataSource));
        right.setFileName("2.txt");
        mixed.addBodyPart(right);


        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
        new FileInputStream("D:\JAVA\data\3.txt"), "utf-8"));
        String lineTxt = null;
        StringBuilder stringBuilder = new StringBuilder();
        while ((lineTxt = bufferedReader.readLine()) != null) {
          stringBuilder.append(lineTxt).append(" ");
        }
        MimeBodyPart neirong = new MimeBodyPart();// 附件
        neirong.setContent(stringBuilder.toString(), "text/html;charset=UTF-8");
        mixed.addBodyPart(neirong);


        Transport transport = session.getTransport();
        transport.connect("smtp.sina.com", 25, "发件人", "授权码");
        transport.sendMessage(message, new Address[]{new InternetAddress("收件人")});
      }
    }

    参考链接:

    https://blog.csdn.net/lovecuidong/article/details/92658140

  • 相关阅读:
    CodeForces 510C Fox And Names (拓扑排序)
    Codeforces 1153D Serval and Rooted Tree (简单树形DP)
    HDU 6437 Problem L.Videos (最大费用)【费用流】
    Luogu P3381 (模板题) 最小费用最大流
    Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
    Codeforces 1144F Graph Without Long Directed Paths (DFS染色+构造)
    HDU 2204 Eddy's 爱好 (容斥原理)
    Codeforces 939E Maximize! (三分 || 尺取)
    Codeforces 938D. Buy a Ticket (最短路+建图)
    CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)
  • 原文地址:https://www.cnblogs.com/wuxiaolong4/p/11711170.html
Copyright © 2011-2022 走看看