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[] { "[email protected]" }; 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 1; } /*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("[email protected]"); // 发送人的邮箱 email.setFrom("[email protected]"); // 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码 email.setAuthentication("wcf1000", "00000"); email.setSubject("测试Email Apache"); // 要发送的信息 email.setMsg("测试Email Apache"); // 发送 email.send(); } catch (EmailException e) { // TODO Auto-generated catch block Log.i("IcetestActivity", e.getMessage()); } return 1; } /* * 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("[email protected]", "XXXXX"); m.set_debuggable(true); String[] toArr = {"[email protected]"}; m.set_to(toArr); m.set_from("[email protected]"); 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 1; } }
第一种方法是调用了系统的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
时间: 2024-10-28 04:38:06