android邮件发送几种方式

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

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 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("181712000@qq.com");  
            // 发送人的邮箱  
            email.setFrom("wcf0000@gmail.com");  
            // 若是须要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码  
            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("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 1;  
    }  
}

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

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

 

 

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

javax包没法找到,我看了下这个问题仍是比较广泛的,你们广泛表示虚拟机是被阉割的版本,javax好像存在不全,这个实际上就没法运行安全

 

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

 

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

相关文章
相关标签/搜索