android邮件发送几种方式
android中发送邮件我大概发现了3种,代码如下
Java代码
packagesrc.icetest;
importorg.apache.commons.mail.EmailException;
importorg.apache.commons.mail.HtmlEmail;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.util.Log;
publicclassIcetestActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("IcetestActivity","starticeteststep1");
//sendMailIntent();
//sendMailByApache();
sendMailByJavaMail();
}
//youneedconfigthemailappinyourandroidmoblefirst,andthemailwillsendbythemailapp.andthereareonebigbug:
//youcan'tsendthemailSilentlyandyouneedtoclickthesendbutton
publicintsendMailByIntent(){
String[]reciver=newString[]{"181712000@qq.com"};
String[]mySbuject=newString[]{"test"};
StringmyCc="cc";
Stringmybody="测试EmailIntent";
IntentmyIntent=newIntent(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,"mailtest"));
return1;
}
/*thismethodcan'tbeusedinandroidmobilesuccessful,butitcanrunnormallyinPC.
Becauseitwillcausethejava.lang.NoClassDefFoundError:javax.activation.DataHandlererror
Maybetherearesomewaytosoloveit......therearealwaysjavaxpackagenotfoundinandroidvirtualmobile.
Bytheway,themethoduseApachemailjar
*/
publicintsendMailByApache(){
try{
HtmlEmailemail=newHtmlEmail();
//这里是发送服务器的名字
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("测试EmailApache");
//要发送的信息
email.setMsg("测试EmailApache");
//发送
email.send();
}catch(EmailExceptione){
//TODOAuto-generatedcatchblock
Log.i("IcetestActivity",e.getMessage());
}
return1;
}
/*
*thismethodusejavamailforandroid,itisagoodjar,
*youcanseethedemoinhttp://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android
*andyoualsoneedthreejars,whichIofferedinattachement
*
**/
publicintsendMailByJavaMail(){
Mailm=newMail("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("ThisisanemailsentusingicetestfromanAndroiddevice");
m.setBody("Emailbody.testbyJavaMail");
try{
//m.addAttachment("/sdcard/filelocation");
if(m.send()){
Log.i("IcetestActivity","Emailwassentsuccessfully.");
}else{
Log.i("IcetestActivity","Emailwassentfailed.");
}
}catch(Exceptione){
//Toast.makeText(MailApp.this,
//"Therewasaproblemsendingtheemail.",
//Toast.LENGTH_LONG).show();
Log.e("MailApp","Couldnotsendemail",e);
}
return1;
}
}
更多相关文章
- Android(安卓)cts all pass 全攻略
- Android(安卓)cts all pass 全攻略
- Android(安卓)下面的一些命令
- Android中的测试小demo
- android的测试工具CTS
- Android(安卓)cts all pass 全攻略
- Android自学笔记-7-Android中的junit
- Android上的单元测试
- android Instrumentation