java技术——邮件的发送

配置文件

1
2
3
4
5
6
7
8
mail.propertis

mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.host=smtp.qq.com
mail.smtp.port=465
username=
password=

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.bobohe.mail;

import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.mail.Authenticator;
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;

public class SendEmailUtil {

private static String username;
private static String password;

public static boolean sendEmail() throws Exception {
//读取邮件的配置文件
ResourceBundle rb = ResourceBundle.getBundle("mail",Locale.ENGLISH);

//获取配置参数
String host = rb.getString("mail.smtp.host");
String port = rb.getString("mail.smtp.port");
username = rb.getString("username");
password = rb.getString("password");

//设置参数
Properties props = new Properties();
props.setProperty("mail.smtp.auth", rb.getString("mail.smtp.auth"));
props.setProperty("mail.smtp.starttls.enable",rb.getString("mail.smtp.starttls.enable"));
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.port", port);
props.setProperty("mail.smtp.ssl.enable", "true");
props.setProperty("mail.smtp.ssl.trust", host);
props.setProperty("mail.smtp.ssl.checkserveridentity","false");

//获取session对象
Session session = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

//打印debug信息
session.setDebug(true);

//邮件内容
Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(username));

msg.setRecipient(Message.RecipientType.TO, new InternetAddress("solo.he@mpos.net"));

msg.setSentDate(new Date());

msg.setSubject("hava a test");

MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent("</br>hava a test</br>hava a test</br>", "text/html;charset=UTF-8");

Multipart mulp = new MimeMultipart();
mulp.addBodyPart(mbp);

msg.setContent(mulp);

//获取Service对象
Transport tran = session.getTransport("smtp");

//连接服务器
tran.connect(host, username, password);

//发送邮件
tran.sendMessage(msg, msg.getAllRecipients());

return true;
}

public static void main(String[] args) throws Exception {
sendEmail();
}

}

  正确的填写配置文件即可发送邮件。JavaMail version 为 1.4.3。由于使用props.setProperty(“mail.smtp.ssl.trust”, host)添加信任的服务器地址。所以不需要导入证书。如果没添加这个配置,怎需要导入证书到JDK的Java\jre6\lib\security目录下。

生成证书相关代码

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
package com.bobohe.mail;

import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.mail.Authenticator;
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;

public class SendEmailUtil {

private static String username;
private static String password;

public static boolean sendEmail() throws Exception {
//读取邮件的配置文件
ResourceBundle rb = ResourceBundle.getBundle("mail",Locale.ENGLISH);

//获取配置参数
String host = rb.getString("mail.smtp.host");
String port = rb.getString("mail.smtp.port");
username = rb.getString("username");
password = rb.getString("password");

//设置参数
Properties props = new Properties();
props.setProperty("mail.smtp.auth", rb.getString("mail.smtp.auth"));
props.setProperty("mail.smtp.starttls.enable",rb.getString("mail.smtp.starttls.enable"));
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.port", port);
props.setProperty("mail.smtp.ssl.enable", "true");
// props.setProperty("mail.smtp.ssl.trust", host);
props.setProperty("mail.smtp.ssl.checkserveridentity","false");

//获取session对象
Session session = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

//打印debug信息
session.setDebug(true);

//邮件内容
Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(username));

msg.setRecipient(Message.RecipientType.TO, new InternetAddress("solo.he@mpos.net"));

msg.setSentDate(new Date());

msg.setSubject("hava a test");

MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent("</br>hava a test</br>hava a test</br>", "text/html;charset=UTF-8");

Multipart mulp = new MimeMultipart();
mulp.addBodyPart(mbp);

msg.setContent(mulp);

//获取Service对象
Transport tran = session.getTransport("smtp");

//连接服务器
tran.connect(host, username, password);

//发送邮件
tran.sendMessage(msg, msg.getAllRecipients());

return true;
}

public static void main(String[] args) throws Exception {
sendEmail();
}

}

  代码执行一次就会在JDK的Java\jre6\lib\security目录下生成证书。输入参数为邮件服务器host和port。用:号隔开。

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 配置文件
  2. 2. java相关代码
  3. 3. 生成证书相关代码
,