Android-通过JavaMail静默发邮件

在此总结一下开发过程中踩过的坑,希望能帮助到后面需要此功能的同学。

1.确保你的邮箱已开启SMTP服务

主要参考这篇文章:http://www.tuicool.com/articles/M7rqUzA 文中有github源码,非常感谢作者的分享,以163邮箱为例!

但是有个大坑就是 文中的SENDER_PASS不是密码,而是客户端授权密码

在163邮箱设置里可以获取 客户端授权密码

 private Authenticator getAuthenticator() {
        return new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(SENDER_NAME, SENDER_PASS);
            }
        };
    }

2. 使用SSL实现加密传输的可参考下文:


3.搞清楚所发送邮箱服务器所用的加密方式,加密方式不对可能会导致错误:

com.sun.mail.smtp.SMTPAddressFailedException: 553 5.7.1 <xxx@xxx>: Sender address rejected: not logged in

com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 <localhost>: Helo command rejected: need fully-qualified hostname

****以上2个错误在我司自己的服务器,我指定mail.smtp.starttls.enable,就成功了,163不需要指定。可以参考下面方式。

private static Properties createConfiguration() {
    return new Properties() {
        {
            put("mail.smtp.auth", "true");
            put("mail.smtp.host", "smtp.gmail.com");
            put("mail.smtp.port", "587");
            put("mail.smtp.starttls.enable", "true");//这个是tls加密时候开启
        }
    };
 }


4.端口的指定也很重要,可以咨询运维人员,查看具体配置,否则也不能发送成功!

5.SMTP参数相关可以参考 官方的介绍  

https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html;

6.想理解JavaMail的相关知识

可移步到这位博主的系列文章 http://983836259.blog.51cto.com/7311475/1664173。

7.可以查看log和错误信息,方便调试。session.setDebug(true);

发送成功的log:

03-14 17:38:00.905 I/System.out: DEBUG: setDebug: JavaMail version 1.4.1
03-14 17:38:01.548 I/System.out: DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1]
03-14 17:38:01.628 I/System.out: 220 163.com Anti-spam GT for Coremail System (163com[20141201])
03-14 17:38:01.660 I/System.out: 250-mail
03-14 17:38:01.661 I/System.out: 250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFh64BpUCa0xDrUUUUj
03-14 17:38:01.679 I/System.out: DEBUG SMTP: Found extension "coremail", arg "1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFh64BpUCa0xDrUUUUj"
03-14 17:38:01.852 I/System.out: MAIL FROM:<xxxxxx@163.com>
03-14 17:38:01.888 I/System.out: 250 Mail OK
03-14 17:38:01.921 I/System.out: 250 Mail OK
03-14 17:38:01.965  I/System.out: Message-ID: <102654324.1.1489484281545.JavaMail.root@localhost>
03-14 17:38:02.024  I/System.out: 250 Mail OK queued as smtp11,D8CowACnvar5ucdYWWGAMQ--.45246S2 1489484282


实现功能可以参考下面的加以修改:

import java.util.Properties;
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.InternetAddress;
import javax.mail.internet.MimeMessage;


public class App {

    public static void main(String[] args) {
        String from = "XXXX@gmail.com";
        String to = "XXXX@web.de";

        Properties properties = createConfiguration();

        SmtpAuthenticator authentication = new SmtpAuthenticator();

        Session session = Session.getDefaultInstance(properties, authentication);

        try {

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(to));

            message.setSubject("This is the Subject Line!");

            // Now set the actual message
            message.setText("This is actual message");

           // Send message
           Transport.send(message);

           System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
           mex.printStackTrace();
     }
 }

 private static Properties createConfiguration() {
    return new Properties() {
        {
            put("mail.smtp.auth", "true");
            put("mail.smtp.host", "smtp.gmail.com");
            put("mail.smtp.port", "587");
            put("mail.smtp.starttls.enable", "true");
        }
    };
 }

 private static class SmtpAuthenticator extends Authenticator {

        private String username = "XXXX@gmail.com";
        private String password = "secret";

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值