Android中使用JavaMail发送邮件ZZ 分类: Android开发 2014-05-30 10:57 87人阅读 评论(0) 收藏
2014-05-30 10:57
281 查看
Android中使用JavaMail发送邮件
项目中有一个需求:当Android程序异常退出的时候把日志啊、堆栈信息整成一个邮件,发给指定邮箱。粗一搜都是用Intent的方式——但这不符合我们的需要,我们并不需要使用用户的邮箱来发送,也不关心用户是否有配置邮箱,只要用户能上网,直接把smtp报文扔给服务器即可。
方法如下:
1)去http://code.google.com/p/javamail-android/,下载additionnal.jar、mail.jar、activation.jar
三个包,在项目中创建libs,将下载的3个jar包放在该目录下。
2)eclipse,编辑项目,Add
Jars,把那三个jar包都添加了。
3)创建EmailSender类
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailSender {
private Properties properties;
private Session session;
private Message message;
private MimeMultipart multipart;
public EmailSender() {
super();
this.properties = new Properties();
}
public void setProperties(String host,String post){
//地址
this.properties.put("mail.smtp.host",host);
//端口号
this.properties.put("mail.smtp.post",post);
//是否验证
this.properties.put("mail.smtp.auth",true);
this.session=Session.getInstance(properties);
this.message = new MimeMessage(session);
this.multipart = new MimeMultipart("mixed");
}
public void setReceiver(String[] receiver) throws
MessagingException{
Address[] address = new
InternetAddress[receiver.length];
for(int i=0;i
address[i] = new InternetAddress(receiver[i]);
}
this.message.setRecipients(Message.RecipientType.TO,
address);
}
public void setMessage(String from,String title,String content)
throws AddressException,
MessagingException{
this.message.setFrom(new InternetAddress(from));
this.message.setSubject(title);
//纯文本的话用setText()就行,不过有附件就显示不出来内容了
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(content,"text/html;charset=gbk");
this.multipart.addBodyPart(textBody);
}
public void addAttachment(String filePath) throws
MessagingException{
FileDataSource fileDataSource = new FileDataSource(new
File(filePath));
DataHandler dataHandler = new
DataHandler(fileDataSource);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDataHandler(dataHandler);
mimeBodyPart.setFileName(fileDataSource.getName());
this.multipart.addBodyPart(mimeBodyPart);
}
public void sendEmail(String host,String account,String pwd) throws
MessagingException{
//发送时间
this.message.setSentDate(new Date());
//发送的内容,文本和附件
this.message.setContent(this.multipart);
this.message.saveChanges();
//创建邮件发送对象,并指定其使用SMTP协议发送邮件
Transport
transport=session.getTransport("smtp");
//登录邮箱
transport.connect(host,account,pwd);
//发送邮件
transport.sendMessage(message,
message.getAllRecipients());
//关闭连接
transport.close();
}
}
4)发送邮件
EmailSender可以当作一个工具类来用,直接调用方法
发邮件是耗时操作,记得起个新线程
EmailSender sender = new EmailSender();
//设置服务器地址和端口,网上搜的到
sender.setProperties("smtp.126.com",
"25");
//分别设置发件人,邮件标题和文本内容
sender.setMessage("******@126.com", "EmailSender", "This
is from JavaMail !");
//设置收件人
sender.setReceiver(new
String[]{"******@gmail.com","******@qq.com"});
//添加附件
sender.addAttachment("/sdcard/debug.txt");
//发送邮件
sender.sendEmail("smtp.126.com", "******@126.com",
"密码******");
注意:需要再AndroidManifest.xml中添加网络权限
项目中有一个需求:当Android程序异常退出的时候把日志啊、堆栈信息整成一个邮件,发给指定邮箱。粗一搜都是用Intent的方式——但这不符合我们的需要,我们并不需要使用用户的邮箱来发送,也不关心用户是否有配置邮箱,只要用户能上网,直接把smtp报文扔给服务器即可。
方法如下:
1)去http://code.google.com/p/javamail-android/,下载additionnal.jar、mail.jar、activation.jar
三个包,在项目中创建libs,将下载的3个jar包放在该目录下。
2)eclipse,编辑项目,Add
Jars,把那三个jar包都添加了。
3)创建EmailSender类
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailSender {
private Properties properties;
private Session session;
private Message message;
private MimeMultipart multipart;
public EmailSender() {
super();
this.properties = new Properties();
}
public void setProperties(String host,String post){
//地址
this.properties.put("mail.smtp.host",host);
//端口号
this.properties.put("mail.smtp.post",post);
//是否验证
this.properties.put("mail.smtp.auth",true);
this.session=Session.getInstance(properties);
this.message = new MimeMessage(session);
this.multipart = new MimeMultipart("mixed");
}
public void setReceiver(String[] receiver) throws
MessagingException{
Address[] address = new
InternetAddress[receiver.length];
for(int i=0;i
address[i] = new InternetAddress(receiver[i]);
}
this.message.setRecipients(Message.RecipientType.TO,
address);
}
public void setMessage(String from,String title,String content)
throws AddressException,
MessagingException{
this.message.setFrom(new InternetAddress(from));
this.message.setSubject(title);
//纯文本的话用setText()就行,不过有附件就显示不出来内容了
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(content,"text/html;charset=gbk");
this.multipart.addBodyPart(textBody);
}
public void addAttachment(String filePath) throws
MessagingException{
FileDataSource fileDataSource = new FileDataSource(new
File(filePath));
DataHandler dataHandler = new
DataHandler(fileDataSource);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDataHandler(dataHandler);
mimeBodyPart.setFileName(fileDataSource.getName());
this.multipart.addBodyPart(mimeBodyPart);
}
public void sendEmail(String host,String account,String pwd) throws
MessagingException{
//发送时间
this.message.setSentDate(new Date());
//发送的内容,文本和附件
this.message.setContent(this.multipart);
this.message.saveChanges();
//创建邮件发送对象,并指定其使用SMTP协议发送邮件
Transport
transport=session.getTransport("smtp");
//登录邮箱
transport.connect(host,account,pwd);
//发送邮件
transport.sendMessage(message,
message.getAllRecipients());
//关闭连接
transport.close();
}
}
4)发送邮件
EmailSender可以当作一个工具类来用,直接调用方法
发邮件是耗时操作,记得起个新线程
EmailSender sender = new EmailSender();
//设置服务器地址和端口,网上搜的到
sender.setProperties("smtp.126.com",
"25");
//分别设置发件人,邮件标题和文本内容
sender.setMessage("******@126.com", "EmailSender", "This
is from JavaMail !");
//设置收件人
sender.setReceiver(new
String[]{"******@gmail.com","******@qq.com"});
//添加附件
sender.addAttachment("/sdcard/debug.txt");
//发送邮件
sender.sendEmail("smtp.126.com", "******@126.com",
"密码******");
注意:需要再AndroidManifest.xml中添加网络权限
相关文章推荐
- Android中使用JavaMail发送邮件ZZ 分类: Android资源 2014-05-30 10:58 93人阅读 评论(0) 收藏
- Android发送邮件 分类: Android开发 2014-05-30 10:57 63人阅读 评论(0) 收藏
- 使用javamail发信过程中的一些问题… 分类: Android开发 2014-05-30 10:57 55人阅读 评论(0) 收藏
- android layout_weight讲解ZZ 分类: Android开发 2014-05-30 10:57 37人阅读 评论(0) 收藏
- Android中如何获取应用版本号zz 分类: Android开发 2014-05-30 10:57 66人阅读 评论(0) 收藏
- 调用android自带的计算器 ZZ 分类: Android开发 2014-05-30 10:57 57人阅读 评论(0) 收藏
- Android 发邮件ZZ 分类: Android开发 2014-05-30 10:57 76人阅读 评论(0) 收藏
- android去除ImageButton白色边框ZZ 分类: Android开发 2014-05-30 10:57 72人阅读 评论(0) 收藏
- [android]使用TabHost+Fragment(替… 分类: Android开发 2014-05-30 10:57 108人阅读 评论(0) 收藏
- Android使用两个Activity页面切换… 分类: Android开发 2014-05-30 10:55 70人阅读 评论(0) 收藏
- android中两种方式打开网页 分类: Android开发 2014-05-30 10:57 54人阅读 评论(0) 收藏
- Android的eclipse常用操作详解ZZ 分类: Android开发 2014-05-30 10:54 62人阅读 评论(0) 收藏
- Android的几个常见小错误ZZ 分类: Android开发 2014-05-30 10:54 58人阅读 评论(0) 收藏
- Android的股票widget源代码 分类: Android开发 2014-05-30 10:57 55人阅读 评论(0) 收藏
- Android上Mac样式的按钮ZZ 分类: Android开发 2014-05-30 10:56 59人阅读 评论(0) 收藏
- Android之指南针学习 分类: Android开发 2014-05-30 10:57 67人阅读 评论(0) 收藏
- 关于android:scrollbarStyle属性 分类: Android开发 2014-05-30 10:57 50人阅读 评论(0) 收藏
- Android开发者必备的42个链接zz 分类: Android开发 2014-05-30 10:53 71人阅读 评论(0) 收藏
- android工程搅混和反编译ZZ 分类: Android开发 Android安装及配置 2014-05-30 10:56 74人阅读 评论(0) 收藏
- Android如何自动检测版本和自动升… 分类: Android开发 2014-05-30 10:57 143人阅读 评论(0) 收藏