本文实例讲解了基于基于jmail实现android邮件发送功能,分享给大家供大家参考,具体内容如下
在android上发送邮件方式:
第一种:借助gmail app客户端,缺点是必须使用gmail帐号,有一点是比较方便,不需要写很多代码,但是不是很灵活。
第二种:基于jmail实现,可以很灵活的自己设置各种属性,不需要gmail帐号
在第二种方式的实现之前,看一下jmail对email结构的划分:
基于smtp协议发送email,所以客户端必须要知道smtp的主机。
腾讯邮件的smtp主机为:stmp.qq.com端口为465基于ssl协议。
最后我做了一个简单的封装,把发送文本加图像附件的功能做出了。
一个单独的class,只要调用一下即可完成:
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
139
140
141
142
143
144
145
|
package com.gloomyfish.jmail.demo;
import java.util.date;
import java.util.properties;
import javax.activation.datahandler;
import javax.activation.datasource;
import javax.activation.filedatasource;
import javax.mail.address;
import javax.mail.message;
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;
public class emailsender {
private string host;
private string port;
private string username;
private string password;
private string[] images;
public string[] getimagepath() {
return images;
}
public void setimagepath(string[] imagepath) {
this .images = imagepath;
}
public emailsender(string host, string port, string username, string password)
{
this .host = host;
this .port = port;
this .username = username;
this .password = password;
}
public void sendemail(string subject, string recepits, string sender, string content)
{
properties props = new properties();
props.put( "mail.smtp.host" , host); //设置smtp的服务器地址
// props.put("mail.smtp.starttls.enable", "true");
// props.put("mail.smtp.port", port); // 设置端口
// props.put("mail.smtp.auth", "true"); //设置smtp服务器要身份验证。
props.put( "mail.smtp.socketfactory.port" , port);
props.put( "mail.smtp.socketfactory.class" , "javax.net.ssl.sslsocketfactory" );
props.put( "mail.smtp.auth" , "true" );
props.put( "mail.smtp.port" , port);
// 返回授权base64编码
popupauthenticator auth = new popupauthenticator(username, password);
// 获取会话对象
session session = session.getinstance(props, auth);
// 设置为debug模式
session.setdebug( true );
// 邮件内容对象组装
mimemessage message = new mimemessage(session);
try
{
address addressfrom = new internetaddress(sender, "jia zhi gang" );
address addressto = new internetaddress(recepits, "my qq e-mail" );
message.setsubject(subject);
message.setsentdate( new date());
message.setfrom(addressfrom);
message.addrecipient(message.recipienttype.to,addressto);
// 邮件文本/html内容
multipart multipart = new mimemultipart();
mimebodypart messagebodypart = new mimebodypart();
messagebodypart.setcontent(content, "text/html" );
multipart.addbodypart(messagebodypart);
// 添加邮件附件
if (images != null && images.length > 0 ) {
for (string filepath : images) {
mimebodypart attachpart = new mimebodypart();
datasource source = new filedatasource(filepath);
attachpart.setdatahandler( new datahandler(source));
attachpart.setfilename(filepath);
multipart.addbodypart(attachpart);
}
}
// 保存邮件内容
message.setcontent(multipart);
// 获取smtp协议客户端对象,连接到指定smpt服务器
transport transport = session.gettransport( "smtp" );
transport.connect(host, integer.parseint(port), username, password);
system.out.println( "connet it success!!!!" );
// 发送邮件到smtp服务器
thread.currentthread().setcontextclassloader( getclass().getclassloader() );
transport.send(message);
system.out.println( "send it success!!!!" );
// 关闭连接
transport.close();
}
catch (exception e)
{
e.printstacktrace();
}
}
public string gethost() {
return host;
}
public void sethost(string host) {
this .host = host;
}
public string getport() {
return port;
}
public void setport(string port) {
this .port = port;
}
public string getusername() {
return username;
}
public void setusername(string username) {
this .username = username;
}
public string getpassword() {
return password;
}
public void setpassword(string password) {
this .password = password;
}
}
|
用户授权类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.gloomyfish.jmail.demo;
import javax.mail.authenticator;
import javax.mail.passwordauthentication;
class popupauthenticator extends authenticator {
private string username;
private string password;
public popupauthenticator(string username, string password)
{
this .username = username;
this .password = password;
}
public passwordauthentication getpasswordauthentication() {
return new passwordauthentication(username, password);
}
}
|
特别注意:
在android上发送邮件必须自己导入三个相关的java文件
以上就是本文的全部内容,希望对大家的学习android软件编程有所帮助。