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
package com.yeshi.buwan.util.email;
 
import java.util.Date;
import java.util.Properties;
 
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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 org.apache.log4j.Logger;
 
public class MailSenderUtil {
    static Logger log = Logger.getLogger(MailSenderUtil.class);
 
    public static boolean sendTextMail(MailSendInfo mailInfo) {
        MyAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if (mailInfo.isValidate()) {
            authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }
        Session sendMailSession = Session.getInstance(pro, authenticator);
        try {
            Message mailMessage = new MimeMessage(sendMailSession);
            Address from = new InternetAddress(mailInfo.getFromAddress());
            mailMessage.setFrom(from);
            Address to = new InternetAddress(mailInfo.getToAddress());
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            mailMessage.setSubject(mailInfo.getSubject());
            mailMessage.setSentDate(new Date());
            String mailContent = mailInfo.getContent();
            mailMessage.setText(mailContent);
            Transport.send(mailMessage);
            return true;
        } catch (MessagingException ex) {
            ex.printStackTrace();
            log.error(ex.getMessage());
        }
        return false;
    }
 
    public static boolean sendHtmlMail(MailSendInfo mailInfo) {
        MyAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if (mailInfo.isValidate()) {
            authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }
        Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
        try {
            Message mailMessage = new MimeMessage(sendMailSession);
            Address from = new InternetAddress(mailInfo.getFromAddress());
            mailMessage.setFrom(from);
            Address to = new InternetAddress(mailInfo.getToAddress());
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            mailMessage.setSubject(mailInfo.getSubject());
            mailMessage.setSentDate(new Date());
            Multipart mainPart = new MimeMultipart();
            BodyPart html = new MimeBodyPart();
            html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
            mainPart.addBodyPart(html);
            mailMessage.setContent(mainPart);
            Transport.send(mailMessage);
            return true;
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
        return false;
    }
 
    public static boolean sendEmail(String email, String title, String content) {
        String username = "he15901227708@163.com";
        String pwd = "hexiaohui1011";
        MailSendInfo mailInfo = new MailSendInfo();
        mailInfo.setMailServerHost("smtp." + username.split("@")[1]);
        mailInfo.setMailServerPort("25");
        mailInfo.setValidate(true);
 
        mailInfo.setUserName(username);
        mailInfo.setPassword(pwd);
        mailInfo.setFromAddress(username);
        mailInfo.setToAddress(email);
        mailInfo.setSubject(title);
        StringBuffer buffer = new StringBuffer();
        buffer.append(content);
        mailInfo.setContent(buffer.toString());
 
        return sendTextMail(mailInfo);
    }
 
    public static boolean sendEmail(String toEmail, String fromEmail, String fromEmailPwd, String title,
            String content) {
        String username = fromEmail;
        String pwd = fromEmailPwd;
        MailSendInfo mailInfo = new MailSendInfo();
        mailInfo.setMailServerHost("smtp." + username.split("@")[1]);
        mailInfo.setMailServerPort("25");
        mailInfo.setValidate(true);
        mailInfo.setUserName(username);
        mailInfo.setPassword(pwd);
        mailInfo.setFromAddress(username);
        mailInfo.setToAddress(toEmail);
        mailInfo.setSubject(title);
        StringBuffer buffer = new StringBuffer();
        buffer.append(content);
        mailInfo.setContent(buffer.toString());
        return sendTextMail(mailInfo);
    }
 
}