liujie
2023-05-26 786466f6accfe836160342c56cdf3415e09bcb38
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
package com.stylefeng.guns.modular.system.utils;
 
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
 
public class EmailUtil {
 
 
    private static String from = "andy.wang@cca19.com";//发送者的谷歌邮箱
    private static String password = "Zhang20140616_";//谷歌邮箱密码
    public static boolean sendMailGMail(String to, String content) {
        return gmailSender(from,password,to,"标题", content);
    }
 
    public static void main(String[] args) {
        sendMailGMail("1793218484@qq.com","a");
    }
 
    private static void gmailSsl(Properties props) {
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        props.put("mail.debug", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.port", "465");
        //props.put("mail.smtp.port", "587");
        props.put("mail.smtp.socketFactory.port", "465");
        //props.put("mail.smtp.socketFactory.port", "587");
        props.put("mail.smtp.auth", "true");
    }
 
 
    public static boolean gmailSender(String from,String password,String email,String subject,String content) {
 
        Properties props = new Properties();
        gmailSsl(props);
        Session session = Session.getDefaultInstance(props,
                new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(from, password);
                    }
                });
        Message msg = new MimeMessage(session);
 
        try {
            msg.setFrom(new InternetAddress(from));
            msg.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(email));
            msg.setSubject(subject);
            msg.setText(content);
            msg.setSentDate(new Date());
            Transport.send(msg);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return true;
    }
 
    public static void sendMail(String mailHost, String fromMail, String fromName, String fromMailPwd,
                                String toMails, String mailTitle, String mailContent) throws Exception {
        final String username = "andy.wang@cca19.com";
        final String password = "Zhang20140616_";
 
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
 
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
 
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("1793218484@qq.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("1793218484@qq.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                    + "\n\n No spam to my email, please!");
 
            Transport.send(message);
 
            System.out.println("Done");
 
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
 
}