Android 使用 JavaMail
今天学习了一下JavaMail,javamail发送邮件确实是一个比较麻烦的问题不用第三方邮件程序。为了以后使用方便,自己写了段代码.
Javamail-Android配置步骤:
下载Android版本JavaMail包,additional.jar、mail.jar和activation.jar,下载地址JavaMail-Android
在项目与src同一目录级别下,新建文件夹lib,将下载的3个jar包放入该文件夹
右键->Properties->Java Build Path->Libraries,选择Add External JARs,找到项目下lib目录的3个jar包
我的代码有三个类:
第一个类:MailSenderInfo.java
01
package com.util.mail;
02
/**
03
* 发送邮件需要使用的基本信息
04
*/
05
import java.util.Properties;
06
public class MailSenderInfo {
07
// 发送邮件的服务器的IP和端口
08
private String mailServerHost;
09
private String mailServerPort = "25";
10
// 邮件发送者的地址
11
private String fromAddress;
12
// 邮件接收者的地址
13
private String toAddress;
14
// 登陆邮件发送服务器的用户名和密码
15
private String userName;
16
private String password;
17
// 是否需要身份验证
18
private boolean validate = false;
19
// 邮件主题
20
private String subject;
21
// 邮件的文本内容
22
private String content;
23
// 邮件附件的文件名
24
private String[] attachFileNames;
25
/**
26
* 获得邮件会话属性
27
*/
28
public Properties getProperties(){
29
Properties p = new Properties();
30
p.put("mail.smtp.host", this.mailServerHost);
31
p.put("mail.smtp.port", this.mailServerPort);
32
p.put("mail.smtp.auth", validate ? "true" : "false");
33
return p;
34
}
35
public String getMailServerHost() {
36
return mailServerHost;
37
}
38
public void setMailServerHost(String mailServerHost) {
39
this.mailServerHost = mailServerHost;
40
}
41
public String getMailServerPort() {
42
return mailServerPort;
43
}
44
public void setMailServerPort(String mailServerPort) {
45
this.mailServerPort = mailServerPort;
46
}
47
public boolean isValidate() {
48
return validate;
49
}
50
public void setValidate(boolean validate) {
51
this.validate = validate;
52
}
53
public String[] getAttachFileNames() {
54
return attachFileNames;
55
}
56
public void setAttachFileNames(String[] fileNames) {
57
this.attachFileNames = fileNames;
58
}
59
public String getFromAddress() {
60
return fromAddress;
61
}
62
public void setFromAddress(String fromAddress) {
63
this.fromAddress = fromAddress;
64
}
65
public String getPassword() {
66
return password;
67
}
68
public void setPassword(String password) {
69
this.password = password;
70
}
71
public String getToAddress() {
72
return toAddress;
73
}
74
public void setToAddress(String toAddress) {
75
this.toAddress = toAddress;
76
}
77
public String getUserName() {
78
return userName;
79
}
80
public void setUserName(String userName) {
81
this.userName = userName;
82
}
83
public String getSubject() {
84
return subject;
85
}
86
public void setSubject(String subject) {
87
this.subject = subject;
88
}
89
public String getContent() {
90
return content;
91
}
92
public void setContent(String textContent) {
93
this.content = textContent;
94
}
95
}
第二个类:MultiMailsender.java
001
package com.util.mail;
002
003
import java.util.Date;
004
import java.util.Properties;
005
006
import javax.mail.Address;
007
import javax.mail.BodyPart;
008
import javax.mail.Message;
009
import javax.mail.MessagingException;
010
import javax.mail.Multipart;
011
import javax.mail.Session;
012
import javax.mail.Transport;
013
import javax.mail.internet.InternetAddress;
014
import javax.mail.internet.MimeBodyPart;
015
import javax.mail.internet.MimeMessage;
016
import javax.mail.internet.MimeMultipart;
017
018
/**
019
* 发送邮件给多个接收者、抄送邮件
020
*/
021
public class MultiMailsender {
022
023
024
/**
025
* 以文本格式发送邮件
026
* @param mailInfo 待发送的邮件的信息
027
*/
028
public boolean sendTextMail(MultiMailSenderInfo mailInfo) {
029
// 判断是否需要身份认证
030
MyAuthenticator authenticator = null;
031
Properties pro = mailInfo.getProperties();
032
if (mailInfo.isValidate()) {
033
 
补充:移动开发 , Android ,