Android不调用第三方程序发送邮件

android平台发送邮件有两种方式,第一种调用第三方程序如Foxmail、网易邮箱大师等。第二种可以不调用任何程序,使用javax扩展包实现后台静默发送邮件。

下面的代码实现了后台静默发送邮件的功能。完整的demo两种方法都实现了,文章最后提供下载地址。

发件人信息实体类

MailSenderInfo.java

// 发送邮件的服务器的IP和端口
private String mailServerHost;
// 发送邮件服务器端口,有些邮箱可能不是25
private String mailServerPort = "25";
// 邮件发送者的地址
private String fromAddress;
// 邮件接收者的地址
private String toAddress;
// 登陆邮件发送服务器的用户名和密码
private String userName;
private String password;
// 是否需要身份验证
private boolean validate = false;
// 邮件主题
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名
private String[] attachFileNames;


/**
* 获得邮件会话属性
*/
public Properties getProperties(){
    Properties p = new Properties();
    p.put("mail.smtp.host", this.mailServerHost);
    p.put("mail.smtp.port", this.mailServerPort);
    p.put("mail.smtp.auth", validate ? "true" : "false");
    return p;
}

getter...
setter...

设置发件地址、收件地址、内容等各个信息

MultiMailsender.MultiMailSenderInfo mailInfo = new MultiMailsender.MultiMailSenderInfo();
mailInfo.setMailServerHost("smtp.126.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("用户名");
mailInfo.setPassword("邮箱密码或授权码"); //许多邮箱使用smtp服务登录第三方软件需要使用授权码而不是密码
mailInfo.setFromAddress("发件邮箱地址");
mailInfo.setToAddress("收件邮箱地址");
mailInfo.setSubject("邮件的主题");
mailInfo.setContent("邮件的内容");
String[] receivers = new String[]{"群发邮件收件地址列表"};
String[] ccs = receivers;
mailInfo.setReceivers(receivers);
mailInfo.setCcs(ccs);

定义各种邮件发送的方法

以文本格式发送邮件
public static boolean sendTextMail(MultiMailSenderInfo mailInfo) {
      // 判断是否需要身份认证
      MyAuthenticator authenticator = null;
      Properties pro = mailInfo.getProperties();
      if (mailInfo.isValidate()) {
      // 如果需要身份认证,则创建一个密码验证器
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
      }
      // 根据邮件会话属性和密码验证器构造一个发送邮件的session
      Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
      try {
      // 根据session创建一个邮件消息
      Message mailMessage = new MimeMessage(sendMailSession);
      // 创建邮件发送者地址
      Address from = new InternetAddress(mailInfo.getFromAddress());
      // 设置邮件消息的发送者
      mailMessage.setFrom(from);
      // 创建邮件的接收者地址,并设置到邮件消息中
      Address[] tos = null;
      String[] receivers = mailInfo.getReceivers();
      if (receivers != null){
          // 为每个邮件接收者创建一个地址
          tos = new InternetAddress[receivers.length + 1];
          tos[0] = new InternetAddress(mailInfo.getToAddress());
          for (int i=0; i<receivers.length; i++){
              tos[i+1] = new InternetAddress(receivers[i]);
          }
      } else {
          tos = new InternetAddress[1];
          tos[0] = new InternetAddress(mailInfo.getToAddress());
      }

      // Message.RecipientType.TO属性表示接收者的类型为TO
      mailMessage.setRecipients(Message.RecipientType.TO,tos);
      // 设置邮件消息的主题
      mailMessage.setSubject(mailInfo.getSubject());
      // 设置邮件消息发送的时间
      mailMessage.setSentDate(new Date());
      // 设置邮件消息的主要内容
      String mailContent = mailInfo.getContent();
      mailMessage.setText(mailContent);
      // 发送邮件
      Transport.send(mailMessage);
      return true;
      } catch (MessagingException ex) {
          ex.printStackTrace();
      }
      return false;
}
以Html方式群发邮件
public static boolean sendMailtoMultiReceiver(MultiMailSenderInfo mailInfo){
    MyAuthenticator authenticator = null;
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(),
            mailInfo.getPassword());
    }
    Session sendMailSession = Session.getInstance(mailInfo.getProperties(), authenticator);
    try {
        Message mailMessage = new MimeMessage(sendMailSession);
        // 创建邮件发送者地址
        Address from = new InternetAddress(mailInfo.getFromAddress());
        mailMessage.setFrom(from);
        // 创建邮件的接收者地址,并设置到邮件消息中
        Address[] tos = null;
        String[] receivers = mailInfo.getReceivers();
        if (receivers != null){
        // 为每个邮件接收者创建一个地址
        tos = new InternetAddress[receivers.length + 1];
        tos[0] = new InternetAddress(mailInfo.getToAddress());
        for (int i=0; i<receivers.length; i++){
            tos[i+1] = new InternetAddress(receivers[i]);
        }
        } else {
        tos = new InternetAddress[1];
        tos[0] = new InternetAddress(mailInfo.getToAddress());
        }
        // 将所有接收者地址都添加到邮件接收者属性中
        mailMessage.setRecipients(Message.RecipientType.TO, tos);

        mailMessage.setSubject(mailInfo.getSubject());
        mailMessage.setSentDate(new Date());
        // 设置邮件内容
        Multipart mainPart = new MimeMultipart();
        BodyPart html = new MimeBodyPart();
        html.setContent(mailInfo.getContent(), "text/html; charset=GBK");
        mainPart.addBodyPart(html);
        mailMessage.setContent(mainPart);
        // 发送邮件
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}
带抄送功能的邮件
public static boolean sendMailtoMultiCC(MultiMailSenderInfo mailInfo){
MyAuthenticator authenticator = null;
if (mailInfo.isValidate()) {
    authenticator = new MyAuthenticator(mailInfo.getUserName(),
        mailInfo.getPassword());
}
Session sendMailSession = Session.getInstance(mailInfo
    .getProperties(), authenticator);
try {
    Message mailMessage = new MimeMessage(sendMailSession);
    // 创建邮件发送者地址
    Address from = new InternetAddress(mailInfo.getFromAddress());
    mailMessage.setFrom(from);
    // 创建邮件的接收者地址,并设置到邮件消息中
    Address to = new InternetAddress(mailInfo.getToAddress());
    mailMessage.setRecipient(Message.RecipientType.TO, to);

    // 获取抄送者信息
    String[] ccs = mailInfo.getCcs();
    if (ccs != null){
    // 为每个邮件接收者创建一个地址
    Address[] ccAdresses = new InternetAddress[ccs.length];
    for (int i=0; i<ccs.length; i++){
        ccAdresses[i] = new InternetAddress(ccs[i]);
    }
    // 将抄送者信息设置到邮件信息中,注意类型为Message.RecipientType.CC
    mailMessage.setRecipients(Message.RecipientType.CC, ccAdresses);
    }

    mailMessage.setSubject(mailInfo.getSubject());
    mailMessage.setSentDate(new Date());
    // 设置邮件内容
    Multipart mainPart = new MimeMultipart();
    BodyPart html = new MimeBodyPart();
    html.setContent(mailInfo.getContent(), "text/html; charset=GBK");
    mainPart.addBodyPart(html);
    mailMessage.setContent(mainPart);
    // 发送邮件
    Transport.send(mailMessage);
    return true;
} catch (MessagingException ex) {
    ex.printStackTrace();
}
return false;
}

发送邮件

MultiMailsender sms = new MultiMailsender();
//sms.sendTextMail(mailInfo);//发送文体格式
//MultiMailsender.sendHtmlMail(mailInfo);//发送html格式
boolean isSuccess = MultiMailsender.sendMailtoMultiCC(mailInfo);//

注意事项

  • 发送邮件属于联网耗时操作,需要开启新线程。
  • 添加联网权限
  • 如果出现javax.mail.AuthenticationFailedException异常,请检查是否因为一下问题:
    1. 是否添加联网权限
    2. 是否打开网络连接
    3. 所填发件地址是否打开SMTP、POP3服务协议
    4. 账号或密码填写错误
    5. 有的邮件服务商要求登录客户端是使用授权密码登录

下载地址

完整demo下载地址点这里

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值