android中发送邮件我大概发现了3种,代码如下

package src.icetest;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log; public class IcetestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("IcetestActivity", "start ice test step 1");
// sendMailIntent();
//sendMailByApache();
sendMailByJavaMail();
} // you need config the mail app in your android moble first,and the mail will send by the mail app. and there are one big bug:
//you can't send the mail Silently and you need to click the send button
public int sendMailByIntent() {
String[] reciver = new String[] { "181712000@qq.com" };
String[] mySbuject = new String[] { "test" };
String myCc = "cc";
String mybody = "测试Email Intent";
Intent myIntent = new Intent(android.content.Intent.ACTION_SEND);
myIntent.setType("plain/text");
myIntent.putExtra(android.content.Intent.EXTRA_EMAIL, reciver);
myIntent.putExtra(android.content.Intent.EXTRA_CC, myCc);
myIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, mySbuject);
myIntent.putExtra(android.content.Intent.EXTRA_TEXT, mybody);
startActivity(Intent.createChooser(myIntent, "mail test")); return ; }
/*this method can't be used in android mobile successful,but it can run normally in PC.
Because it will cause the java.lang.NoClassDefFoundError: javax.activation.DataHandler error
May be there are some way to solove it ......there are always javax package not found in android virtual mobile.
By the way ,the method use Apache mail jar
*/
public int sendMailByApache() { try {
HtmlEmail email = new HtmlEmail();
// 这里是发送服务器的名字
email.setHostName("smtp.gmail.com");
// 编码集的设置
email.setTLS(true);
email.setSSL(true); email.setCharset("gbk");
// 收件人的邮箱
email.addTo("181712000@qq.com");
// 发送人的邮箱
email.setFrom("wcf0000@gmail.com");
// 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
email.setAuthentication("wcf1000", "");
email.setSubject("测试Email Apache");
// 要发送的信息
email.setMsg("测试Email Apache");
// 发送
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
Log.i("IcetestActivity", e.getMessage());
} return ;
}
/*
* this method use javamail for android ,it is a good jar,
* you can see the demo in http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android
* and you also need three jars ,which I offered in attachement
*
* */
public int sendMailByJavaMail() {
Mail m = new Mail("wcfXXXX@gmail.com", "XXXXX");
m.set_debuggable(true);
String[] toArr = {"18170000@qq.com"};
m.set_to(toArr);
m.set_from("18170000@qq.com");
m.set_subject("This is an email sent using icetest from an Android device");
m.setBody("Email body. test by Java Mail");
try {
//m.addAttachment("/sdcard/filelocation");
if(m.send()) {
Log.i("IcetestActivity","Email was sent successfully."); } else {
Log.i("IcetestActivity","Email was sent failed.");
}
} catch (Exception e) {
// Toast.makeText(MailApp.this,
// "There was a problem sending the email.",
// Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
} return ;
}
}

第一种方法是调用了系统的mail app,你首先要配置系统的mail app,但是这个方法的最大问题是,你运行这个方法后

他并不会默认的发送邮件,而是弹出mail的app界面,你需要手动的点击发送,如图

第二种,是调用了apache的common库,在pc上可以正常运行,但是在android虚拟机中会报错java.lang.NoClassDefFoundError: javax.activation.DataHandler error

javax包无法找到,我看了下这个问题还是比较普遍的,大家普遍表示虚拟机是被阉割的版本,javax好像存在不全,这个实际上就无法运行

第三种,实际是javamail有人做了移植,专门为android做了开发,这下就比较好了,网上的demo代码也比较到位,只有一个问题,就是要自己添加一个mail.java,而且对stmp要手动添加。

其实理论上还应该有一种,自己实现smtp服务器,全程采用socket编程,直接与目标服务器交流,这个win下面我写过,但是android下就算了,而且长期来讲面临着smtp服务器以后会被进行方向查询,以提高安全性。

http://wcf1987.iteye.com/blog/1292509

android邮件发送几种方式的更多相关文章

  1. Android 数据存储五种方式

    1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用Shared ...

  2. android 定位的四种方式

    [原文]  开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助: android 定 ...

  3. android 定位的几种方式介绍

    [地理位置] android 定位的几种方式介绍 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面 www.androidkaifa.com 总结了一下网络 ...

  4. Android 反射-换一种方式编程

    Android 反射-换一种方式编程 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/59109933 本文出自[赵彦军的博客] 上一 ...

  5. 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)

    在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...

  6. Android学习—下载Android SDK的两种方式

    在Android Studio中下载Android SDK的两种方式 Android studio下载地址:http://www.android-studio.org/ 方式一.设置HTTP Prox ...

  7. Android邮件发送详解

    转载:http://flysnow.iteye.com/blog/1128354 Android中我为什么发不了邮件???我手机里明明有邮件客户端的,可我为什么不能调用它发送邮件???相信这是很多人会 ...

  8. Android数据存储五种方式总结

    本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据     2 文件存储数据       3 SQLite数据库存储数据 4 使用Cont ...

  9. 将Eclipse代码导入到Android Studio的两种方式

    转: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0104/2259.html 说到使用Android Studio,除了新建 ...

随机推荐

  1. IOS开发UI篇-NavigationController的基本使用

    一.简介 一个iOS的app很少只由一个控制器组成,除非这个app极其简单 当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个view时,可以用一个大的view去管理1个或者多个小v ...

  2. Qt 文件搜索(写入文本文件)

    代码无意间在网上找到,下载回来后改了几个格式,编译后经测试可以正常使用,这个个文件搜索的很好的例子,有两种搜索方式:一种是按文件名作为关键字进行搜索,一种是以文档中所包含的关键字进行搜索,贴两张图先: ...

  3. SqlCommand类

    一.常用属性 CommandText 获取或设置要对数据源执行的 Transact-SQL 语句.表名或存储过程. CommandTimeout 获取或设置在终止执行命令的尝试并生成错误之前的等待时间 ...

  4. Java Socket 简单梳理

    Sockets let you send raw streams of bytes back and forth between two computers, giving you fairly lo ...

  5. eclipse工具再学习

    今天下午最后近1小时及晚上2个多小时,我都花费时间在工程环境配置上,自尊心被严重摧残,各种郁闷和抱怨.源头是我部分刷新代码后运行工程依赖的jar报错,后来找同事发现是因为我没更新pom.xml文件,重 ...

  6. VC断点不可用的问题

    文章一 今天我笔记本的VC编译器莫名其妙的就进入不了断点了,而台式机确是好的,问题是一进入调试,断点就自动被取消了,郁闷了一下午,无法进入断点的原因很多,都有想重装VC编译器的打算了,晚上回来查查资料 ...

  7. 给ie浏览器添加推荐浏览器提示

    <script type="text/javascript"> var isIE = !!window.ActiveXObject; var isIE6 = isIE ...

  8. 开源安卓播放器:Dolphin Player 简单分析

    Dolphin播放器(Dolphin Player)是一款开源的音频和视频播放器,它支持大多数的音频和视频文件模式,也支持大部分的字幕文件格式.它是基于ffmpeg的. 项目主页:http://cod ...

  9. PB C/S轉B/S ODBC方式連接數據庫

    PB C/S轉B/S ODBC方式連接數據庫,DSN需要建為系統而不是使用者DSN,否則連不上數據庫.

  10. zoj 3471 Most Powerful(状态压缩dp)

    Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These ato ...