Android发送邮件

需要三个jar包,可自行百度进行下载。
activation.jar
additionnal.jar
mail.jar
本例子是使用163邮箱进行邮件发送的。
这里面有些注意事项,下图中的权限是要开启的
Android发送邮件_第1张图片
另外一个注意事项就是,代码中的账户是 你的邮箱账号,但是密码是下图中的授权码,否则的话会提示“授权失败”
Android发送邮件_第2张图片

所需要的权限

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

废话不多说,直接上代码。

import android.os.AsyncTask;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * 邮件管理类
 */
public class MailManager {

    //以163邮箱为例,需要提前在163邮箱设置里面开启smtp.
    //发送账户
    private static final String SENDER_NAME = "[email protected]";
    //发送账户的密码(客户端授权密码)
    private static final String SENDER_PASS = "wugui1234";
    //邮箱服务器
    private static final String VALUE_MAIL_HOST = "smtp.163.com";
    //邮箱服务器主机
    private static final String KEY_MAIL_HOST = "mail.smtp.host";
    //邮箱是否需要鉴权
    private static final String KEY_MAIL_AUTH = "mail.smtp.auth";
    //需要鉴权
    private static final String VALUE_MAIL_AUTH = "true";
    public static MailManager getInstance() {
        return InstanceHolder.instance;
    }

    private MailManager() {
    }

    private static class InstanceHolder {
        private static MailManager instance = new MailManager();
    }

    class MailTask extends AsyncTask {

        private MimeMessage mimeMessage;

        public MailTask(MimeMessage mimeMessage) {
            this.mimeMessage = mimeMessage;
        }

        @Override
        protected Boolean doInBackground(Void... voids) {
            try {
                Transport.send(mimeMessage);
                return Boolean.TRUE;
            } catch (MessagingException e) {
                e.printStackTrace();
                return Boolean.FALSE;
            }
        }
    }

    public void sendMail(final String title, final String content) {
        MimeMessage mimeMessage = createMessage(title, content);
        MailTask mailTask = new MailTask(mimeMessage);
        mailTask.execute();
    }

    public void sendMailWithFile(String title, String content, String filePath) {
        MimeMessage mimeMessage = createMessage(title, content);
        appendFile(mimeMessage, filePath);
        MailTask mailTask = new MailTask(mimeMessage);
        mailTask.execute();
    }

    public void sendMailWithMultiFile(String title, String content, List pathList) {
        MimeMessage mimeMessage = createMessage(title, content);
        appendMultiFile(mimeMessage, pathList);
        MailTask mailTask = new MailTask(mimeMessage);
        mailTask.execute();
    }

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

    private MimeMessage createMessage(String title, String content) {
        Properties properties = System.getProperties();
        properties.put(KEY_MAIL_HOST, VALUE_MAIL_HOST);
        properties.put(KEY_MAIL_AUTH, VALUE_MAIL_AUTH);
        Session session = Session.getInstance(properties, getAuthenticator());
        //创建消息
        MimeMessage mimeMessage = new MimeMessage(session);
        try {
            //设置发送者
            mimeMessage.setFrom(new InternetAddress(SENDER_NAME));
            //设置接收者
            InternetAddress[] addresses = new InternetAddress[]{new InternetAddress(SENDER_NAME)};
            mimeMessage.setRecipients(Message.RecipientType.TO,
                    "[email protected]");
            //设置邮件的主题
            mimeMessage.setSubject(title);
            //设置邮件的内容
            MimeBodyPart textPart = new MimeBodyPart();
            textPart.setContent(content, "text/html;charset=gbk");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(textPart);
            mimeMessage.setContent(multipart);
            //设置发送时间
            mimeMessage.setSentDate(new Date());
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return mimeMessage;
    }

    //添加文件附件
    private void appendFile(MimeMessage message, String filePath) {
        try {
            Multipart multipart = (Multipart) message.getContent();
            MimeBodyPart filePart = new MimeBodyPart();
            filePart.attachFile(filePath);
            multipart.addBodyPart(filePart);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    //添加多个文件附件
    private void appendMultiFile(MimeMessage message, List pathList) {
        try {
            Multipart multipart = (Multipart) message.getContent();
            for (String path : pathList) {
                MimeBodyPart filePart = new MimeBodyPart();
                filePart.attachFile(path);
                multipart.addBodyPart(filePart);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

调用时代码

  //发送邮件
  MailManager.getInstance().sendMailWithFile("123","123","/sdcard/Test/log.txt");

感谢:https://github.com/Dev-Wiki/MailSender

你可能感兴趣的:(Android,邮件发送)