zj
2024-06-03 287ac389edd047696d956afafdb855a93830bc0c
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
package com.nq.utils.email;
 
import com.nq.utils.PropertiesUtil;
 
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
 
 
public class MailSender {
    private final Properties prop = new Properties();
    private final Session session;
    private final Message msg;
    private final Transport transport;
 
    public static class Builder {
        private final String mailContent;
        private final String toAddress;
        private String debug = "false";
        private String auth = "true";
        private String host = "smtp.163.com";
        private String protocol = "smtp";
 
        private String subject = PropertiesUtil.getProperty("admin.auth.email.subject");
 
        private String fromAddress = PropertiesUtil.getProperty("admin.auth.email");
 
        private String fromCount = PropertiesUtil.getProperty("admin.auth.email");
 
        private String fromPassword = PropertiesUtil.getProperty("admin.auth.email.pwd");
 
        public Builder Debug(String debug) {
            this.debug = debug;
            return this;
        }
 
        public Builder Subject(String subject) {
            this.subject = subject;
            return this;
        }
 
        public Builder Auth(String auth) {
            this.auth = auth;
            return this;
        }
 
        public Builder Host(String host) {
            this.host = host;
            return this;
        }
 
        public Builder FromCount(String fromCount) {
            this.fromCount = fromCount;
            return this;
        }
 
        public Builder FromAddress(String fromAddress) {
            this.fromAddress = fromAddress;
            return this;
        }
 
        public Builder FromPassword(String fromPassword) {
            this.fromPassword = fromPassword;
            return this;
        }
 
        public Builder(String mailContent, String toAddress) {
            this.mailContent = mailContent;
            this.toAddress = toAddress;
        }
 
        public Builder Protocol(String protocol) {
            this.protocol = protocol;
            return this;
        }
 
        public MailSender send() throws Exception {
            return new MailSender(this);
        }
    }
 
    private MailSender(Builder builder) throws Exception {
        this.prop.setProperty("mail.debug", builder.debug);
        this.prop.setProperty("mail.smtp.auth", builder.auth);
        this.prop.setProperty("mail.host", builder.host);
        this.prop.setProperty("mail.transport.protocol", builder.protocol);
        this.prop.put("mail.smtp.socketFactory.port", "465");
        this.prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        this.prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        this.prop.setProperty("mail.smtp.socketFactory.port", "465");
        this.session = Session.getInstance(this.prop);
        this.msg = (Message) new MimeMessage(this.session);
 
        this.transport = this.session.getTransport();
        this.msg.setSubject(builder.subject);
        this.msg.setFrom((Address) new InternetAddress(builder.fromAddress, "通知郵件"));
        this.transport.connect(builder.fromCount, builder.fromPassword);
 
        this.msg.setContent(builder.mailContent, "text/html;charset=utf-8");
 
        this.transport.sendMessage(this.msg, new Address[]{ new InternetAddress(builder.toAddress)});
    }
}