Android 发送邮件mail
2014-09-16 10:23
225 查看
andoid利用javamail发送邮件,这里以163邮箱发送为例,126邮箱服务器地址为:smtp.126.com
<span style="font-size:18px;">package com.example.sendemail;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
public class MainActivity extends Activity {
Properties props;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread() {
@Override
public void run() {
super.run();
// 实例化属性对象Properties
props = new Properties();
// 设置smtp的服务器地址
props.put("mail.smtp.host", "smtp.163.com");
// 设置服务器需要身份验证
props.put("mail.smtp.auth", "true");
// 创建会话session来管理连接
Session session = Session.getDefaultInstance(props,
// 匿名内部类创建authenticator实例
new javax.mail.Authenticator() {
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
String username = "1111111@163.com";// 邮箱登陆账号
String pwd = "qqqq";
return new PasswordAuthentication(username, pwd);
}
});
// 实例化MimeMessage对象
MimeMessage message = new MimeMessage(session);
try {
// 设置发送方邮件地址 "FROM"为昵称
Address addressFrom = new InternetAddress(
"1111111@163.com", "FROM");
// 设置接收方方邮件地址
Address addressTo1 = new InternetAddress(
"xxr02@163.com", "To");
// 设置接收方方邮件地址
Address addressTo2 = new InternetAddress(
"1111111@163.com", "To");
// 收件人地址 抄送
Address addressCopy = new InternetAddress(
"1025870@qq.com", "abc");
// 创建邮件内容
message.setContent("hello", "text/plain");// 或者message.setText("hello");
// 设置标题
message.setSubject("title");
// 设置发件人
message.setFrom(addressFrom);
// 添加收件人 在这里可以群发
message.addRecipients(Message.RecipientType.TO,
new Address[] { addressTo1, addressTo2 });
// 抄送人
message.addRecipient(Message.RecipientType.CC, addressCopy);
message.saveChanges();
// 发送邮件
Transport transport = session.getTransport("smtp");// 创建连接
transport.connect("smtp.163.com", "1111111@163.com",
"qqqq");
Transport.send(message);
Log.i("mail", "发送成功");
transport.close();
} catch (UnsupportedEncodingException e) {
Log.i("mail", "发送失败 UnsupportedEncodingException");
e.printStackTrace();
} catch (MessagingException e) {
Log.i("mail", "发送失败,密码错误");
e.printStackTrace();
}
}
}.start();
}
}
</span>依赖的包下载: http://download.csdn.net/detail/u014071669/7920533
<span style="font-size:18px;">package com.example.sendemail;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
public class MainActivity extends Activity {
Properties props;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread() {
@Override
public void run() {
super.run();
// 实例化属性对象Properties
props = new Properties();
// 设置smtp的服务器地址
props.put("mail.smtp.host", "smtp.163.com");
// 设置服务器需要身份验证
props.put("mail.smtp.auth", "true");
// 创建会话session来管理连接
Session session = Session.getDefaultInstance(props,
// 匿名内部类创建authenticator实例
new javax.mail.Authenticator() {
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
String username = "1111111@163.com";// 邮箱登陆账号
String pwd = "qqqq";
return new PasswordAuthentication(username, pwd);
}
});
// 实例化MimeMessage对象
MimeMessage message = new MimeMessage(session);
try {
// 设置发送方邮件地址 "FROM"为昵称
Address addressFrom = new InternetAddress(
"1111111@163.com", "FROM");
// 设置接收方方邮件地址
Address addressTo1 = new InternetAddress(
"xxr02@163.com", "To");
// 设置接收方方邮件地址
Address addressTo2 = new InternetAddress(
"1111111@163.com", "To");
// 收件人地址 抄送
Address addressCopy = new InternetAddress(
"1025870@qq.com", "abc");
// 创建邮件内容
message.setContent("hello", "text/plain");// 或者message.setText("hello");
// 设置标题
message.setSubject("title");
// 设置发件人
message.setFrom(addressFrom);
// 添加收件人 在这里可以群发
message.addRecipients(Message.RecipientType.TO,
new Address[] { addressTo1, addressTo2 });
// 抄送人
message.addRecipient(Message.RecipientType.CC, addressCopy);
message.saveChanges();
// 发送邮件
Transport transport = session.getTransport("smtp");// 创建连接
transport.connect("smtp.163.com", "1111111@163.com",
"qqqq");
Transport.send(message);
Log.i("mail", "发送成功");
transport.close();
} catch (UnsupportedEncodingException e) {
Log.i("mail", "发送失败 UnsupportedEncodingException");
e.printStackTrace();
} catch (MessagingException e) {
Log.i("mail", "发送失败,密码错误");
e.printStackTrace();
}
}
}.start();
}
}
</span>依赖的包下载: http://download.csdn.net/detail/u014071669/7920533
相关文章推荐
- Android中利用commons-email.jar和 mail.jar 发送和接受邮件
- Android开发之JavaMail发送邮件(用户反馈)
- android里发送邮件mail的几种方式
- android javax.mail发送中文邮件
- 【代码】Android: SMS TEL MAIL 发送短信、打电话、发送邮件使用集合
- Android 发送邮件 JavaMail
- Android 发送邮件 JavaMail
- android mail发送邮件(包含163邮箱及qq邮箱,使用mail.jar,activation.jar,additionnal.jar)
- Android Java Mail与Apache Mail发送邮件对比
- .NET 1.1 下不使用 System.Web.Mail.SmtpMail 发送邮件的其它选择
- 用JSP+SERVLET实现的JAVAMAIL发送邮件
- 使用System.Web.Mail通过需验证的邮件服务器发送邮件(转)
- Asp.net 2.0 用SmtpMail 设置发送邮件!!
- 在Asp.Net中使用SmtpMail发送邮件的方法:实例
- 使用System.Web.Mail 组件通过SMTP认证发送邮件
- 使用 DotnetOpenMail发送带附件的邮件
- 利用System.Web.Mail 发送邮件
- PHP PEAR MAIL发送TEXT/HTML格式邮件
- ASP发送邮件函数(Jmail,cdonts,aspemail,WebEasymail)
- 使用System.Net.Mail.SmtpClient发送邮件时出现的乱码问题