package com.cn.cz.MailSenderActivity;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new com.cn.cz.MailSenderActivity.JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
安卓发送邮件javamail完整项目
共31个文件
class:12个
java:4个
jar:3个
3星 · 超过75%的资源 需积分: 10 25 下载量 29 浏览量
2014-09-13
08:58:21
上传
评论
收藏 1019KB ZIP 举报
温馨提示
eclipseADT,android application project,安卓发送邮件javamail。
资源推荐
资源详情
资源评论
收起资源包目录
MailSenderActivity.zip (31个子文件)
MailSenderActivity
activation.jar 50KB
bin
classes.dex 493KB
com
cn
cz
MailSenderActivity
MailSenderActivity.class 1021B
R$layout.class 424B
R$id.class 412B
R$drawable.class 430B
GMailSender$ByteArrayDataSource.class 2KB
R$attr.class 370B
R.class 601B
JSSEProvider$1.class 1KB
R$string.class 457B
MailSenderActivity$1.class 2KB
GMailSender.class 3KB
JSSEProvider.class 692B
resources.ap_ 11KB
MailSenderActivity.apk 256KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
values
strings.xml 187B
drawable-mdpi
icon.png 3KB
layout
main.xml 379B
additionnal.jar 45KB
mail.jar 430KB
assets
default.properties 449B
gen
com
cn
cz
MailSenderActivity
R.java 743B
src
com
cn
cz
MailSenderActivity
MailSenderActivity.java 2KB
GMailSender.java 4KB
JSSEProvider.java 919B
.project 854B
.classpath 524B
AndroidManifest.xml 761B
共 31 条
- 1
资源评论
- sunofsoul2014-09-28可以运行,但是有地啊小问题,没有成功。调试中。。。
- 佳木成林_刘乙隆2016-07-27不行,运行出错
Android虾米
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功