Android发送Email的两种方法:
方法一:通过Intent调用内置的Gmail发送邮件
优点:简单、方便
缺点:缺少灵活性,只能使用关联的gmail发送邮件
示例代码:
- String[] recipients = {"接收方邮件地址", "接收方邮件地址"};
- String subject = "邮件标题";
- String text = "邮件内容";
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_SEND);
- intent.putExtra(Intent.EXTRA_EMAIL, recipients);
- intent.putExtra(Intent.EXTRA_SUBJECT, subject);
- intent.putExtra(Intent.EXTRA_TEXT, text);
- startActivity(Intent.createChooser(intent, "Sending..."));
方法二:使用JavaMail发送邮件
优点:灵活
缺点:复杂,配置麻烦
配置步骤:
-
下载Android版本JavaMail包,additional.jar、mail.jar和activation.jar,下载地址JavaMail-Android
-
在项目与src同一目录级别下,新建文件夹lib,将下载的3个jar包放入该文件夹
-
右键->Properties->Java Build Path->Libraries,选择Add External JARs,找到项目下lib目录的3个jar包添加OK,最后lib文件夹右键->build path->use as source folder
实现步骤:
-
建立与邮件服务器间的会话Session
-
构建邮件消息MINEMessage
-
Transport发送邮件
注意:
AndroidManifest下添加网络权限
配置会话Session的Properties时,值为String型"true"和"false",而不是布尔型
示例代码:
- package dyingbleed.iteye;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.AddressException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.mail.util.ByteArrayDataSource;
- public class MailSender extends Authenticator {
- public static final String host = "smtp.gmail.com";
- private final String userName;
- private final String password;
- private Session session;
- public MailSender(String userName, String password) {
- this.userName = userName;
- this.password = password;
- initialize(); //初始化
- }
- private void initialize() {
- Properties props = new Properties();
- props.setProperty("mail.transport.protocol", "smtp");
- props.setProperty("mail.host", host);
- props.put("mail.smtp.auth", true);
- session = Session.getDefaultInstance(props, this);
- }
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(userName, password);
- }
- /**
- * 发送Email
- * @param subject 标题
- * @param body 内容
- * @param sender 发送者
- * @param recipients 接收者
- * @throws MessagingException
- * @throws AddressException
- * */
- public synchronized void sendMail(String subject, String body, String sender, String recipients) throws AddressException, MessagingException {
- MimeMessage message = new MimeMessage(session);
- DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
- /*
- * 设置MIME消息
- * */
- message.setSender(new InternetAddress(sender));
- message.setSubject(subject);
- message.setDataHandler(handler);
- if(recipients.contains(",")) {
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
- } else {
- message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
- }
- Transport.send(message); //发送
- }
- }
Android通过Intent发送电子邮件含附件
File file = new File("\sdcard\android123.cwj"); //附件文件地址
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("subject", file.getName()); //
intent.putExtra("body", "android123 - email sender"); //正文
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); //添加附件,附件为file对象
if (file.getName().endsWith(".gz")) {
intent.setType("application/x-gzip"); //如果是gz使用gzip的mime
} else if (file.getName().endsWith(".txt")) {
intent.setType("text/plain"); //纯文本则用text/plain的mime
} else {
intent.setType("application/octet-stream"); //其他的均使用流当做二进制数据来发送
}
startActivity(intent); //调用系统的mail客户端进行发送