您的位置:首页 > 运维架构 > Linux

linux系统编程之文件与IO:文件描述符、open,close

2013-07-11 22:18 405 查看
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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: