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;
}
}
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;
}
}
相关文章推荐
- linux系统编程之文件与IO(一):文件描述符、open,close
- 文件io之——open/close
- 19_Linux文件IO的打开和关闭--open/close
- Day25、malloc 内存分配、mmap内存映射、文件的操作(文件描述符、open\close\read)
- linux系统编程:文件操作--open、create、close
- 【UNIX环境高级编程】文件 IO 操作 一 ( open | close | creat | lseek | write | read )
- Linux文件IO-open,write,read,lseek,close
- Linux-文件操作(open、write、read、close、iotcl)
- Linux系统开发 2 文件IO open() close() read() write() perror() lseek() fcntl() ioctl()
- C程序设计语言- UNIX系统接口-7.1----文件描述符、低级IO(read、write)、随机访问、(open、creat、close、unlink)
- Linux系统编程-文件IO(一)
- 文件操作ofstream,open,close,ifstream,fin,按照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件
- linux系统编程之文件与IO:实现ls -l功能
- 文件IO编程之open和close函数使用实例
- Linux系统编程(4)——文件与IO之ioctl函数
- 文件操作编程一(open、close、read、write、lseek函数的使用)
- 【体会Python之美】【文件】file,open,read,readline,readlines,write,close
- linux系统编程之文件与IO:时间函数小结
- linux文件操作函数(open、write、read、close)可用于串口读写
- Linux系统编程(3)——文件与IO之fcntl函数