Java发送邮件

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
package com.google.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
*/
public class EmailUtil {

private static Properties properties = null;

public static void init(Properties properties)
{
EmailUtil.properties = properties;
}

public static void send(String subject, String message) {
try {
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String userName = properties.getProperty("mail.user");
String password = properties.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, authenticator);
MimeMessage mime = new MimeMessage(session);
InternetAddress form = new InternetAddress(properties.getProperty("mail.user"));
mime.setFrom(form);
InternetAddress to = new InternetAddress(properties.getProperty("main.to"));
mime.setRecipient(RecipientType.TO, to);

mime.setSubject(subject);
mime.setContent(message, "text/html;charset=UTF-8");

Transport.send(mime);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", "smtp.exmail.qq.com");
properties.put("mail.user", "user1@example.com");
properties.put("mail.password", "password");
properties.put("main.to","user2@qq.com");

EmailUtil.init(properties);
EmailUtil.send("hello", "这是来自大洋彼岸的声音!");
}

}