Android 后台发送邮件

添加依赖包

需要添加javamail中的三个包activation.jar,additionnal.jar,mail.jar
下载地址:https://code.google.com/archive/p/javamail-android/downloads

添加权限

需要在AndroidManifest.xml中添加权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

基本原理

通过代码用smtp的方式登陆发送邮箱send@host.com在通过这个邮箱发送邮件给get@tohost.com
发送邮件主要包括3个部分:创建连接,创建邮件体,发送邮件。

创建Authenticator的子类

创建一个类MailSender继承自Authenticator,
在类的构造方法中,创建连接属性Properties,为实例添加服务器地址(由发件邮箱所在的服务器提供),是否需要smpt身份验证(是否需要用户名,密码等)以及SSL协议端口号
重载getPasswordAuthentication()方法将user(发邮件的邮件地址)以及password传入
编写sendmail方法,处理发送邮件的逻辑

public class MailSender extends Authenticator {

    //服务器地址
    private String mailhost = "smtp.163.com";
    //用于发送邮件的邮箱地址
    private String user;

    //用于发送邮件的邮箱密码
    private String password;

    //会话(每创建一次连接就要有一个会话)
    private Session session;


    public MailSender(String user, String password) {
        this.user = user;
        this.password = password;

        //创建连接属性
        Properties props = new Properties();

        //设置通信协议
        props.setProperty("mail.transport.protocol", "SMTP");

        //设置服务器地址
        props.setProperty("mail.smtp.host", mailhost);

        //设置是否需要SMTP身份验证
        props.put("mail.smtp.auth", "true");
        //设置SSL协议端口号
        props.put("mail.smtp.port", "25");


        session = session.getInstance(props,this);
    }
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }
    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
        try{
          //创建邮件体
            MimeMessage message = new MimeMessage(session);
            DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
            message.setSender(new InternetAddress(sender));
            message.setSubject(subject);
            message.setDataHandler(handler);
            if (recipients.indexOf(',') > 0)
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
            else
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
            message.saveChanges();

            try {
                session.setDebug(true);
                //创建连接
                Transport trans = session.getTransport("smtp");
                //连接服务器             
                trans.connect(mailhost,user, password);

                //发送消息
                trans.send(message);
            } catch (AuthenticationFailedException ae) {
                ae.printStackTrace();
            } catch (MessagingException mex) {
                mex.printStackTrace();
                Exception ex = null;
                if ((ex = mex.getNextException()) != null) {
                    ex.printStackTrace();
                }
           }
        }catch(Exception e){
          e.printStackTrace();
        }
    }
}

在需要发送邮件的地方

最好使用异步

                    MailSender sender = new MailSender("send@host.com", "password");
                    sender.sendMail("title",
                            "body",

                            "send@host.com",
                            "password");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值