我是靠谱客的博主 俊逸电灯胆,这篇文章主要介绍java使用javax.mail包发送电子邮件:设置账号、密码、主题、文本、附件,现在分享给大家,希望可以做个参考。

java使用javax.mail包发送电子邮件。这个实例可发送多附件。

这里使用163邮箱进行测试。可以设置账号、密码、主题、文本内容、附件

测试代码

 

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args){ Mail sendmail = new Mail(); sendmail.setHost("smtp.163.com"); sendmail.setUserName("aaaaaaaaa@163.com"); sendmail.setPassWord("aaaaaaaaa"); sendmail.setTo("aaaaaaaaa@163.com"); sendmail.setFrom("aaaaaaaaa@163.com"); sendmail.setSubject("你好,这是测试!"); sendmail.setContent("你好这是一个带多附件的测试!"); //Mail sendmail = new Mail("aaaaaaaaa@163.com","aaaaaaaaa@163.com","smtp.163.com","aaaaaaaaa","aaaaaaaaa","你好","胃,你好吗?"); sendmail.attachfile("D:\test.jpg"); sendmail.attachfile("D:\test.mp4"); sendmail.sendMail(); }

邮件发送类的实现

复制代码
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.lp.app.net; import java.util.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; String to = "";//收件人 String from = "";//发件人 String host = "";//smtp主机 String username = "" ; String password = "" ; String filename = "";//附件文件名 String subject = "";//邮件主题 String content = "";//邮件正文 Vector file = new Vector();//附件文件集合 public Mail(){ } //构造器,提供直接的参数传入 public Mail(String to,String from,String smtpServer,String username,String password,String subject,String content){ this.to = to; this.from = from; this.host = smtpServer; this.username = username; this.password = password; this.subject = subject; this.content = content; } //设置邮件服务器地址 public void setHost(String host){ this.host = host; } //设置登录服务器校验密码 public void setPassWord(String pwd){ this.password = pwd; } //设置登录服务器校验用户 public void setUserName(String usn){ this.username = usn; } //设置邮件发送目的邮箱 public void setTo(String to){ this.to = to; } //设置邮件发送源邮箱 public void setFrom(String from){ this.from = from; } //设置邮件主题 public void setSubject(String subject){ this.subject = subject; } //设置邮件内容 public void setContent(String content){ this.content = content; } //把主题转换为中文 public String transferChinese(String strText){ try{ strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B"); }catch(Exception e){ e.printStackTrace(); } return strText; } //往附件组合中添加附件 public void attachfile(String fname){ file.addElement(fname); } //发送邮件 public boolean sendMail(){ //构造mail session Properties props = System.getProperties(); props.put("mail.smtp.host",host); props.put("mail.smtp.auth","true"); Session session=Session.getDefaultInstance(props, new Authenticator(){ public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(username,password); } }); try { //构造MimeMessage 并设定基本的值 MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address={new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO,address); subject = transferChinese(subject); msg.setSubject(subject); //构造Multipart Multipart mp = new MimeMultipart(); //向Multipart添加正文 MimeBodyPart mbpContent = new MimeBodyPart(); mbpContent.setText(content); //向MimeMessage添加(Multipart代表正文) mp.addBodyPart(mbpContent); //向Multipart添加附件 Enumeration efile=file.elements(); while(efile.hasMoreElements()){ MimeBodyPart mbpFile = new MimeBodyPart(); filename=efile.nextElement().toString(); FileDataSource fds = new FileDataSource(filename); mbpFile.setDataHandler(new DataHandler(fds)); mbpFile.setFileName(fds.getName()); //向MimeMessage添加(Multipart代表附件) mp.addBodyPart(mbpFile); } file.removeAllElements(); //向Multipart添加MimeMessage msg.setContent(mp); msg.setSentDate(new Date()); //发送邮件 Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); Exception ex = null; if ((ex=mex.getNextException())!=null){ ex.printStackTrace(); } return false; } return true; } }

本文只试验了163邮件,读者可以按下面的主机地址,试验其他邮箱。但并不是每种邮箱都能成功。

gmail(google.com)
POP3服务器地址:pop.gmail.com(SSL启用 端口:995)
SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587)

21cn.com:
POP3服务器地址:pop.21cn.com(端口:110)
SMTP服务器地址:smtp.21cn.com(端口:25)

sina.com:
POP3服务器地址:pop3.sina.com.cn(端口:110)
SMTP服务器地址:smtp.sina.com.cn(端口:25)

tom.com:
POP3服务器地址:pop.tom.com(端口:110)
SMTP服务器地址:smtp.tom.com(端口:25)

163.com:
POP3服务器地址:pop.163.com(端口:110)
SMTP服务器地址:smtp.163.com(端口:25)

263.net:
POP3服务器地址:pop3.263.net(端口:110)
SMTP服务器地址:smtp.263.net(端口:25)

yahoo.com:
POP3服务器地址:pop.mail.yahoo.com
SMTP服务器地址:smtp.mail.yahoo.com

263.net.cn:
POP3服务器地址:pop.263.net.cn(端口:110)
SMTP服务器地址:smtp.263.net.cn(端口:25)

Foxmail:
POP3服务器地址:POP.foxmail.com(端口:110)
SMTP服务器地址:SMTP.foxmail.com(端口:25)

sinaVIP
POP3服务器:pop3.vip.sina.com (端口:110)
SMTP服务器:smtp.vip.sina.com (端口:25)

sohu.com:
POP3服务器地址:pop3.sohu.com(端口:110)
SMTP服务器地址:smtp.sohu.com(端口:25)

etang.com:
POP3服务器地址:pop.etang.com
SMTP服务器地址:smtp.etang.com

x263.net:
POP3服务器地址:pop.x263.net(端口:110)
SMTP服务器地址:smtp.x263.net(端口:25)

yahoo.com.cn:
POP3服务器地址:pop.mail.yahoo.com.cn(端口:995)
SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587)
雅虎邮箱POP3的SSL不启用端口为110,POP3的SSL启用端口995;SMTP的SSL不启用端口为25,SMTP的SSL启用端口为465

QQ邮箱 QQ企业邮箱
POP3服务器地址:pop.qq.com(端口:110) POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995)
SMTP服务器地址:smtp.qq.com (端口:25) SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:587/465)
SMTP服务器需要身份验证

126邮箱 HotMail
POP3服务器地址:pop.126.com(端口:110) POP3服务器地址:pop.live.com (端口:995)
SMTP服务器地址:smtp.126.com(端口:25) SMTP服务器地址:smtp.live.com (端口:587)

china.com: 139邮箱
POP3服务器地址:pop.china.com(端口:110) POP3服务器地址:POP.139.com(端口:110)
SMTP服务器地址:smtp.china.com(端口:25) SMTP服务器地址:SMTP.139.com(端口:25)

 

最后

以上就是俊逸电灯胆最近收集整理的关于java使用javax.mail包发送电子邮件:设置账号、密码、主题、文本、附件的全部内容,更多相关java使用javax内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(81)

评论列表共有 0 条评论

立即
投稿
返回
顶部