当前位置:宁波企业邮 » 企业目录 » 重庆 » 文章详细

Java实现网易企业163邮箱发送邮件

前提:

本文介绍两种邮件发送,一个是网易163邮箱,另一个是网易企业163邮箱

项目地址:https://github.com/su1573/mail-send

一、登录邮箱设置

网易163邮箱
1、登录个人163邮箱,如图选中POP3/SMTP/IMAP
在这里插入图片描述


2、开启POP3/SMTP服务,根据提示获取授权码,授权要保存好,一会儿要用到

在这里插入图片描述


网易企业163邮箱
这个我这里是没有上面的POP3/SMTP服务,在代码中用的直接是登录邮箱密码


二、jar包引用

		<!-- javax.mai 核心包 -->
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.2.11.RELEASE</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

三、代码演示

package com.su.mailsend.service;

import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

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

/**
 * @program: program
 * @Date: 2020/12/16 19:26
 * @Author: Mr.SU
 * @Description:
 */
public class MailStmpSend {

    /**
     * @Description: 网易163邮箱,发送邮件
     * @param: "[to, text, title]"
     * @Return: boolean
     * @Author: supenghui
     * @Date: 2020/12/16 16:54
     */
    private void sendMail(String to, String text, String title) throws Exception {

        String from = "xxx@163.com"; // 发件人邮箱地址
        String user = "xxx@163.com"; // 发件人称号,同邮箱地址
        String password = "VHXCLTZDEOFFWNSA"; // 发件人邮箱客户端授权码

        // 一、创建参数配置, 用于连接邮件服务器的参数配置
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", "smtp.163.com"); // 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
        props.put("mail.smtp.host", "smtp.163.com");
        props.put("mail.smtp.auth", "true"); // 需要经过授权,也就是用户名和密码的校验,这样才能通过验证(一定要有这一条)

        // 二、根据配置创建会话对象, 用于和邮件服务器交互
        Session session = Session.getDefaultInstance(props);
        session.setDebug(true); // true 在控制台(console)上看到发送邮件的过程

        // 三、 创建一封复杂邮件(文本+附件)

        try {
            // 3.1. 创建邮件对象
            MimeMessage message = new MimeMessage(session); // 加载发件人地址

            // 3.2. From: 发件人
            message.setFrom(new InternetAddress(from));

            // 3.3. To: 收件人(可以增加多个收件人)
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 加载收件人地址

            // 3.4. To: 收件人(可以增加多个抄送)
            message.addRecipient(Message.RecipientType.CC, new InternetAddress(to)); // 加载抄件人地址

            // 3.5. Subject: 邮件主题
            message.setSubject(title); // 加载标题

            // 3.6. 邮件内容
            MimeMultipart multipart = new MimeMultipart(); // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
            MimeBodyPart contentPart = new MimeBodyPart(); // 设置邮件的文本内容
            contentPart.setContent(text, "text/html;charset=utf-8");
            multipart.addBodyPart(contentPart);

            // 3.7. 邮件附件
            String attPath = "D:\data\ftpUpload\2020\12\10\盖章指令文件.zip";
            MimeBodyPart attachment = new MimeBodyPart();
            DataHandler dh = new DataHandler(new FileDataSource(attPath)); // 读取本地文件
            attachment.setDataHandler(dh); // 将附件数据添加到“节点”
            attachment.setFileName(MimeUtility.encodeText(dh.getName())); // 设置附件的文件名(需要编码)
            multipart.addBodyPart(attachment);

            multipart.setSubType("mixed"); // 混合关系

            // 3.8. 设置整个邮件的关系(将最终的混合“节点”作为邮件的内容添加到邮件对象)
            message.setContent(multipart);

            // 3.9. 设置发件时间
            message.setSentDate(new Date());

            // 3.10. 保存上面的所有设置
            message.saveChanges(); // 保存变化

            // 四、 根据 Session 获取邮件传输对象
            Transport transport = session.getTransport("smtp");

            // 五、 使用 邮箱账号 和 授权码 连接邮件服务器
            transport.connect("smtp.163.com", user, password);

            // 六、 发送邮件,
            transport.sendMessage(message, message.getAllRecipients());

            // 七、 关闭连接
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description: 网易企业163邮箱发送邮件
     * @param: "[]"
     * @Return: void
     * @Author: supenghui
     * @Date: 2020/12/17 13:55
     */
    public void sendHtmlMail() {
        try {

            String from = "职业年金受托";//发件人昵称展示
//            String[] to = {"supenghui@sinosoft.com.cn", "supenghui@sinosoft.com.cn"};//接收邮箱
            String to = "xxx@163.com";//接收邮箱
//            String[] copy = {"supenghui@sinosoft.com.cn", "supenghui@sinosoft.com.cn"};//抄送邮箱
            String copy = "xxx@163.com";//抄送邮箱
            String subject = "测试邮件";//邮件主题
            String text = "你好,这是一封测试邮件,无需回复。";
            String host = "smtphz.qiye.163.com";//163企业邮箱smtp
            String username = "xxx@su-abc.com.cn";//企业邮箱 @后面是域名
            String password = "123456";//企业邮箱密码

            // 一、创建参数配置, 用于连接邮件服务器的参数配置
            Properties prop = new Properties();
            prop.setProperty("mail.smtp.auth", "true"); // 需要经过授权,也就是用户名和密码的校验,这样才能通过验证(一定要有这一条)
            prop.setProperty("mail.smtp.timeout", "994"); // 加密端口(ssl)  可通过 https://qiye.163.com/help/client-profile.html 进行查询

            MailSSLSocketFactory sf = new MailSSLSocketFactory();// SSL加密
            sf.setTrustAllHosts(true); // 设置信任所有的主机
            prop.put("mail.smtp.ssl.enable", "true");
            prop.put("mail.smtp.ssl.socketFactory", sf);

            //二、创建一封复杂邮件(文本+附件)
            JavaMailSenderImpl javaMailSend = new JavaMailSenderImpl();

            // 3.1. 创建邮件对象
            MimeMessage message = javaMailSend.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "utf-8");

            // 3.2. From: 发件人
            String nick = MimeUtility.encodeText(from);//设置昵称
            messageHelper.setFrom(new InternetAddress(nick + " <" + username + ">"));// 邮件发送者

            // 3.3. To: 收件人(可以增加多个收件人)
            messageHelper.setTo(to);  //收件人

            // 3.4. To: 收件人(可以增加多个抄送)
            messageHelper.setCc(copy); //抄送人

            // 3.5. Subject: 邮件主题
            messageHelper.setSubject(subject); //邮件标题

            // 3.6. 邮件内容
            messageHelper.setText(text, true); //文本中,如需换行,要用<br>,不能用
换行

            // 3.7. 邮件附件
            File file = new File("D:\data\ftpUpload\2020\12\10\盖章指令文件.zip");
            messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
            // 3.8. 设置邮件服务器登录信息
            javaMailSend.setHost(host);
            javaMailSend.setUsername(username);
            javaMailSend.setPassword(password);
            javaMailSend.setJavaMailProperties(prop);

            // 六、 发送邮件,
            javaMailSend.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            MailStmpSend ds = new MailStmpSend();
//          ds.sendMail("xxx@qq.com", "你好,这是一封测试邮件,无需回复。", "测试邮件");
//          ds.sendMail("xxxx@163.com", "你好,这是一封测试邮件,无需回复。", "测试邮件");
            ds.sendHtmlMail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187

四、效果展示

网易163邮箱发送邮件:
因篇幅问题,附件在最下面,这里就不截了
在这里插入图片描述


网易企业163邮箱发送邮件:
在这里插入图片描述

<>


阅读:202
日期:2021-01-26

打印 】 【 关闭 】  【 字体: 】 
上一篇: 163企业邮箱登录后怎么导入联系人?
下一篇: 腾讯qq企业邮箱登录入口界面如何设置
  >> 相关文章
 

服务热线

0574-55011290

微信二维码