android邮件发送几种方式

时间:2022-09-17 13:26:29

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界面,你需要手动的点击发送,如图

android邮件发送几种方式

第二种,是调用了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. spark-sql访问hive的问题记录

    好久没有弄博客了... hive0.14 spark0.12 [hadoop@irs bin]$ ./spark-sql Spark assembly has been built with Hive ...

  2. jQuery 关于 end() 方法的详细解释

    <ul class="first"> <li class="foo">list item 1</li> <li> ...

  3. Uva 10881 Piotr&rsquo&semi;s Ants 蚂蚁

    一根长度为 L 厘米的木棍上有 n 只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为 1 厘米/秒.当两只蚂蚁相撞时,二者同时调头(掉头用的时间忽略不计).给出每只蚂蚁的初始位置和朝向,计算 T 秒之后 ...

  4. Linux中的磁盘

    Linux的磁盘管理 (很重要请注意高能预警) 硬盘:几个盘片,双面,磁性颗粒, 处理速率不同步:借助于一个中间层 文件系统(FileSystem)     可以实现对磁盘行的文件进行读写     文 ...

  5. web&period;xml配置中的 文件类型&lt&semi;mime-mapping&gt&semi;

    <mime-mapping> <extension>doc</extension> <mime-type>application/msword</ ...

  6. mysql 性能分析套件

    #!/usr/local/python3./bin/python3. #!coding:utf- #################################### #目地:用于诊断mysql性 ...

  7. vue-router&lpar;配置子路由--单页面多路由区域操作&rpar;

    1.配置子路由: import Post from "@components/Post" export default new Router({ routers:[ { path: ...

  8. Spark2&period;1&period;0模型设计与基本架构(上)

    随着近十年互联网的迅猛发展,越来越多的人融入了互联网——利用搜索引擎查询词条或问题:社交圈子从现实搬到了Facebook.Twitter.微信等社交平台上:女孩子们现在少了逛街,多了在各大电商平台上的 ...

  9. 第五章:Reminders实验:第一部分&lbrack;Learn Android Studio 汉化教程&rsqb;

    Learn Android Studio 汉化教程 By now you are familiar with the basics of creating a new project, program ...

  10. Java中的增强for循环

    增强 for 循环 1. 增强的 for 循环对于遍历 Array 或 Collection 的时候相当方便. import java.util.*; public class Test { publ ...