今天学习了一下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; |
05 |
import java.util.Properties;
|
06 |
public class
MailSenderInfo { |
08 |
private
String mailServerHost; |
09 |
private
String mailServerPort = "25" ;
|
11 |
private
String fromAddress; |
13 |
private
String toAddress; |
15 |
private
String userName; |
16 |
private
String password; |
18 |
private
boolean validate = false ;
|
20 |
private
String subject; |
22 |
private
String content; |
24 |
private
String[] attachFileNames; |
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" );
|
35 |
public
String getMailServerHost() { |
36 |
return
mailServerHost; |
38 |
public
void setMailServerHost(String mailServerHost) {
|
39 |
this .mailServerHost = mailServerHost;
|
41 |
public
String getMailServerPort() { |
42 |
return
mailServerPort; |
44 |
public
void setMailServerPort(String mailServerPort) {
|
45 |
this .mailServerPort = mailServerPort;
|
47 |
public
boolean isValidate() { |
50 |
public
void setValidate( boolean
validate) { |
51 |
this .validate = validate;
|
53 |
public
String[] getAttachFileNames() { |
54 |
return
attachFileNames; |
56 |
public
void setAttachFileNames(String[] fileNames) {
|
57 |
this .attachFileNames = fileNames;
|
59 |
public
String getFromAddress() { |
62 |
public
void setFromAddress(String fromAddress) {
|
63 |
this .fromAddress = fromAddress;
|
65 |
public
String getPassword() { |
68 |
public
void setPassword(String password) {
|
69 |
this .password = password;
|
71 |
public
String getToAddress() { |
74 |
public
void setToAddress(String toAddress) {
|
75 |
this .toAddress = toAddress;
|
77 |
public
String getUserName() { |
80 |
public
void setUserName(String userName) {
|
81 |
this .userName = userName;
|
83 |
public
String getSubject() { |
86 |
public
void setSubject(String subject) {
|
87 |
this .subject = subject;
|
89 |
public
String getContent() { |
92 |
public
void setContent(String textContent) {
|
93 |
this .content = textContent;
|
第二个类:MultiMailsender.java
第三个类:MyAuthenticator.java
下面给出使用上面三个类的代码:
01 |
public static
void main(String[] args){ |
03 |
MultiMailSenderInfo mailInfo =
new MultiMailSenderInfo();
|
04 |
mailInfo.setMailServerHost( "smtp.163.com" );
|
05 |
mailInfo.setMailServerPort( "25" );
|
06 |
mailInfo.setValidate( true );
|
07 |
mailInfo.setUserName( "xxx@163.com" );
|
08 |
mailInfo.setPassword( "**********" ); |
09 |
mailInfo.setFromAddress( "xxx@163.com" );
|
10 |
mailInfo.setToAddress( "xxx@163.com" );
|
11 |
mailInfo.setSubject( "设置邮箱标题" );
|
12 |
mailInfo.setContent( "设置邮箱内容" ); |
13 |
String[] receivers =
new String[]{ "***@163.com" ,
"***@tom.com" }; |
14 |
String[] ccs = receivers; mailInfo.setReceivers(receivers);
|
17 |
MultiMailsender sms =
new MultiMailsender();
|
18 |
sms.sendTextMail(mailInfo); |
19 |
MultiMailsender.sendHtmlMail(mailInfo); |
20 |
MultiMailsender.sendMailtoMultiCC(mailInfo); |
最后,给出朋友们几个注意的地方:
1、使用此代码你可以完成你的javamail的邮件发送功能、发多个邮箱。三个类缺一不可。
2、这三个类我打包是用的com.util.mail包,如果不喜欢,你可以自己改,但三个类文件必须在同一个包中
3、不要使用你刚刚注册过的邮箱在程序中发邮件,如果你的163邮箱是刚注册不久,那你就不要使用“smtp.163.com”。因为你发不出去。刚注册的邮箱是不会给你这种权限的,也就是你不能通过验证。要使用你经常用的邮箱,而且时间比较长的。
4、另一个问题就是mailInfo.setMailServerHost("smtp.163.com");与mailInfo.setFromAddress("xxx@163.com");这两句话。即如果你使用163smtp服务器,那么发送邮件地址就必须用163的邮箱,如果不的话,是不会发送成功的。
5、关于javamail验证错误的问题,网上的解释有很多,但我看见的只有一个。就是我的第三个类。你只要复制全了代码,我想是不会有问题的。
6、 然后在Android项目中添加网络访问权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
原文链接:http://www.cnblogs.com/peakzheng/archive/2012/02/04/2338671.html