Android中的后台邮件发送
一、调用邮件发送工具类进行邮件发送
new Thread(){
public void run() {
try {
GMailSender sender = new GMailSender(
"lixeeq19910119@gmail.com",
"ee768lxq");
sender.sendMail("xxxxx注册验证", "验证码:"
+ localNum,
"lixeeq19910119@gmail.com",
email);
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
将上面的邮箱和密码换成你自己的邮箱和密码。
二、邮件发送工具类
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties; 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; 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 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");
}
}
}
三、Provider类
import java.security.AccessController;
import java.security.Provider; public final class JSSEProvider extends Provider {
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
关于Provider请参看:http://www.766.com/doc/java/security/class-use/Provider.html
四、需要的jar包
jar包下载地址:http://download.csdn.net/detail/lxq_xsyu/6543081
Android中的后台邮件发送的更多相关文章
- [UWP]UWP中获取联系人/邮件发送/SMS消息发送操作
这篇博客将介绍如何在UWP程序中获取联系人/邮件发送/SMS发送的基础操作. 1. 获取联系人 UWP中联系人获取需要引入Windows.ApplicationModel.Contacts名称空间. ...
- iOS 和 Android 中的后台运行问题
后台机制的不同,算是iOS 和 Android的一大区别了,最近发布的iOS7又对后台处理做了一定的更改,找时间总结一下编码上的区别,先做个记录. 先看看iOS的把,首先需要仔细阅读一下Apple的官 ...
- android中调用系统的发送短信、发送邮件、打电话功能
1 调用发送短信功能: Uri smsToUri = Uri.parse("smsto:"); Intent sendIntent = new Intent(Intent.ACT ...
- Android中实现短信发送的一种方式
SendSmsActivity.java: package com.test.smsmangerdemo.sendsmsactivity; import android.support.v7.app. ...
- 採用Android中的httpclient框架发送post请求
/** * 採用httpclientPost请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得 ...
- [转]android中解析后台返回的json字符串
普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid":"100196","Showname&qu ...
- Android中的httpclient框架发送get请求
/** * 採用httpclientGet请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到 ...
- 你有没有觉得邮件发送人固定配置在yml文件中是不妥当的呢?SpringBoot 动态设置邮件发送人
明月当天,不知道你有没有思念的人 前言 之前其实已经写过SpringBoot异步发送邮件,但是今天在一个小项目中要用到发送邮件时,我突然觉得邮件发送人只有一个,并且固定写在yml文件中,就是非常的不妥 ...
- Android中解析网络请求的URL
近期正在做Android网络应用的开发,使用了android网络请求方面的知识.如今向大家介绍网络请求方面的知识.我们知道android中向server端发送一个请求,(这就是我们通常所说的POST请 ...
随机推荐
- JQ实现选项卡(jQuery原型插件扩展)
下边分为两个版本,一种是点击切换选项(index.js),一种是滑过切换选项(index1.js) HTML文件: jq使用jquery-1.11.3.js版本 <!DOCTYPE html&g ...
- DE1-SOC调试linux应用程序
参考http://www.alterawiki.com/wiki/SoCEDSGettingStarted#Getting_Started_with_Linux_Application_Debuggi ...
- 【CS Round #48 (Div. 2 only)】Water Volume
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举0在哪个位置就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> us ...
- spring boot 2.x Path with "WEB-INF" or "META-INF"
学习spring boot 2.x时,使用jsp作为前端页面.在application.properties配置了jsp所在位置 spring.mvc.view.prefix:/WEB-INF/vie ...
- ORA-00119: invalid specification for system parameter LOCAL_LISTENER;
错误提示内容及上下文环境: SQL> grant sysdba to weng;grant sysdba to weng*第 1 行出现错误:ORA-01034: ORACLE not avai ...
- java回调函数这样说,应该明确了吧!
有哥们问我回调怎么用,回调怎么理解? 怎么说好呢,仅仅可意会不可言传呐,非也,回调在实际开发中使用频率事实上是非常高的,恰好我小时候也被回调函数欺负过,居然问了,那么肯定要好好分享一下我的一些经验. ...
- LIVE555源代码研究之四:MediaServer (一)
LIVE555源代码研究之四:MediaServer (一) 从本篇文章開始我们将从简单server程序作为突破点,深入研究LIVE555源代码. 从前面的文章我们知道.不论什么一个基于LIVE555 ...
- 【7.89%】【BNUOJ 52303】Floyd-Warshall
Time limit: 2 seconds Memory limit: 1024 megabytes In ICPCCamp, there are n cities and m (bidirectio ...
- iOS开发Quartz2D 十三:画板涂鸦
一:效果如图: 二:代码 #import "ViewController.h" #import "DrawView.h" #import "Handl ...
- 《JavaScript & jQuery交互式Web前端开发》之JavaScript基础指令
在本节中.你将開始学习阅读和编写JavaScript代码,还将学习怎样编写Web浏览器可以遵照运行的指令.在開始学习后面章节中的更复杂的概念之前.我们先学习语言的一些核心部分,然后看看怎 ...