• Java 腾讯邮箱发送邮件工具类(单人+多人)


    邮箱实体类

    public class EmailMailInfo {
        //邮箱服务器 如smtp.163.com
        private String host ;
        //用户邮箱 如**@163
        private String formName ;
        //用户授权码 不是用户名密码 可以自行查看相关邮件服务器怎么查看
        private String formPassword ;
        //消息回复邮箱
        private String replayAddress ;
        //发送地址
        private String toAddress ;
        //发送主题
        private String subject ;
        //发送内容
        private String content ;
        //端口号
        private String port;
        //多人邮件
        private InternetAddress[] emailMany;
    
        public String getPort() {
            return port;
        }
    
        public void setPort(String port) {
            this.port = port;
        }
    
        public String getHost() {
            return host;
        }
        public void setHost(String host) {
            this.host = host;
        }
        public String getFormName() {
            return formName;
        }
        public void setFormName(String formName) {
            this.formName = formName;
        }
        public String getFormPassword() {
            return formPassword;
        }
        public void setFormPassword(String formPassword) {
            this.formPassword = formPassword;
        }
        public String getReplayAddress() {
            return replayAddress;
        }
        public void setReplayAddress(String replayAddress) {
            this.replayAddress = replayAddress;
        }
        public String getToAddress() {
            return toAddress;
        }
        public void setToAddress(String toAddress) {
            this.toAddress = toAddress;
        }
        public String getSubject() {
            return subject;
        }
        public void setSubject(String subject) {
            this.subject = subject;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
    
        public InternetAddress[] getEmailMany() {
            return emailMany;
        }
    
        public void setEmailMany(InternetAddress[] emailMany) {
            this.emailMany = emailMany;
        }
    }
    
    • 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

    具体业务实现

    	/**
         * 多个账号邮箱的公共方法
         * @param emailList
         * @return
         */
        private static InternetAddress[] addressMails(List emailList) throws AddressException {
            List list = new ArrayList();//不能使用string类型的类型,这样只能发送一个收件人
            //多个接收账号
            for (int i = 0; i < emailList.size(); i++) {
                list.add(new InternetAddress(emailList.get(i).toString()));
            }
            return (InternetAddress[]) list.toArray(new InternetAddress[list.size()]);
        }
    
    	/**
         * 发送多人邮件
         * @throws Exception
         */
        private void SendMoreEmail() throws Exception {
            List<String> noticeList = new ArrayList<>();
            noticeList.add("163@163.com");
            noticeList.add("164@163.com");
            EmailMailInfo info = new EmailMailInfo();
            info.setSubject("主题");
            info.setContent("这里是邮件内容啊啊啊啊啊啊啊啊啊啊啊啊啊");
            info.setEmailMany(addressMails(noticeList));//多人邮件
            EmailSendUtil.sendHtmlMail(info);
        }
    
    	/**
    	 *  单人邮箱
    	 */
    	public static void main(String[] args) {
    			String email = "1578855@qq.com";
    			String title = "用户";
    			String content = "您好:我是邮箱";
    			EmailMailInfo info = new EmailMailInfo();
    			info.setToAddress(email);
    			info.setSubject(title);
    			info.setContent(content);
    			try {
    				EmailSendUtil.sendHtmlMail(info);
    			} catch (Exception e) {
    				System.out.print("'" + title + "'的邮件发送失败!");
    				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

    发送邮件工具类

    import java.security.GeneralSecurityException;
    import java.util.Date;
    import java.util.Properties;
    
    import javax.mail.Authenticator;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.Multipart;
    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;
    import com.sun.mail.util.MailSSLSocketFactory;
    
    
    public class EmailSendUtil {
        private final static String host = "smtp.exmail.qq.com"; //腾讯企业邮的服务器
        private final static String formName = "邮箱";//发送人邮箱
        private final static String password = "密码"; //授权码
    
        private final static String replayAddress = "邮箱"; //发送人邮箱
        private final static String port = "465"; //端口号
        //private final static String port = "587"; //你的邮箱
    
    
        public static void sendHtmlMail(EmailMailInfo info)throws Exception{
            info.setHost(host);
            info.setFormName(formName);
            info.setFormPassword(password);   //网易邮箱的授权码~不一定是密码
            info.setReplayAddress(replayAddress);
            info.setPort(port);
            Message message = getMessage(info);
            // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
            Multipart mainPart = new MimeMultipart();
            // 创建一个包含HTML内容的MimeBodyPart
            BodyPart html = new MimeBodyPart();
            // 设置HTML内容
            html.setContent(info.getContent(), "text/html; charset=utf-8");
            mainPart.addBodyPart(html);
            // 将MiniMultipart对象设置为邮件内容
            message.setContent(mainPart);
            Transport.send(message);
        }
    
        public static void sendTextMail(EmailMailInfo info) throws Exception {
    
            info.setHost(host);
            info.setFormName(formName);
            info.setFormPassword(password);   //网易邮箱的授权码~不一定是密码
            info.setReplayAddress(replayAddress);
            Message message = getMessage(info);
            //消息发送的内容
            message.setText(info.getContent());
    
            Transport.send(message);
        }
    
        private static Message getMessage(EmailMailInfo info) throws Exception{
            final Properties p = System.getProperties() ;
            p.setProperty("mail.smtp.host", info.getHost());
            p.setProperty("mail.smtp.auth", "true");
            p.setProperty("mail.smtp.user", info.getFormName());
            p.setProperty("mail.smtp.pass", info.getFormPassword());
            p.setProperty("mail.smtp.port", info.getPort());//设置端口
            p.setProperty("mail.transport.protocol", "smtps");//
    
            //开启安全协议
            MailSSLSocketFactory sf = null;
            try {
                sf = new MailSSLSocketFactory();
                sf.setTrustAllHosts(true);
                p.put("mail.smtp.ssl.enable", "true");
                p.put("mail.smtp.ssl.socketFactory", sf);
            } catch (GeneralSecurityException e1) {
                e1.printStackTrace();
            }
    
            // 根据邮件会话属性和密码验证器构造一个发送邮件的session
            Session session = Session.getDefaultInstance(p, new Authenticator(){
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
                }
            });
            session.setDebug(false);
            Message message = new MimeMessage(session);
            //消息发送的主题
            message.setSubject(info.getSubject());
            //接受消息的人
            //message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
            //消息的发送者
            message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"系统"));
            // 创建邮件的接收者地址,并设置到邮件消息中
            //单人接收邮件
            //message.setRecipient(Message.RecipientType.TO,new InternetAddress(info.getToAddress()));
            //多人接收邮件
            message.setRecipients(Message.RecipientType.TO, info.getEmailMany());
            // 消息发送的时间
            message.setSentDate(new Date());
            return message ;
        }
    }
    
    
    
    
    • 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
  • 相关阅读:
    Docker Ubuntu php nginx mysql redis 开发环境部署教程
    【react】报错记录
    美摄AR人像美颜,全新视觉体验
    30天Python入门(第十九天:深入了解Python中的文件处理)
    某ERP系统存在RCE漏洞
    数据结构——栈的讲解(超详细)
    Go 常用命令介绍
    安装 docker 可视化工具 portainer
    创建一个简单的贪吃蛇游戏:HTML、CSS和JavaScript教程
    【Raspberry Pi】搭建NAS流媒体播放器 + ARIA2 + YAAW + 迅雷下载系统
  • 原文地址:https://blog.csdn.net/weixin_39093006/article/details/126707941