android 发送邮件(带附件)

implementation 'com.sun.mail:android-mail:1.6.0'
implementation 'com.sun.mail:android-activation:1.6.0'

 

 

package com.xxx.utils;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;

/**
 * author: wr
 * date: on 2018/8/29.
 */
public class MailService {

    public static void send_email(String email, String content) throws IOException, MessagingException {
        File file = new File(content);


        //创建配置文件
        Properties props = new Properties();
        // 开启认证
        props.put("mail.smtp.auth", true);
        // 设置协议方式
        props.put("mail.transport.protocol", "smtp");
        // 设置主/机名
        props.put("mail.smtp.host", "smtp.163.com");//host
        // 设置SSL加密(未采用SSL时,端口一般为25,可以不用设置;采用SSL时,端口为465,需要显示设置)
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        // 设置账户和密码
        props.put("mail.smtp.username", "176xxxxxxxx");//username
        props.put("mail.smtp.password", "123456");//password
        // 创建会话,getDefaultInstace得到的始终是该方法初次创建的缺省的对象,getInstace每次获取新对象
        Session session = Session.getInstance(props, new MyAuthenticator("176xxxxxxxx","123456"));
        // 显示错误信息
        session.setDebug(true);
        // 创建发送时的消息对象
        MimeMessage message = new MimeMessage(session);
        // 设置发送发的账户和名称
        message.setFrom(new InternetAddress("176xxxxxxxx@163.com", "123456", "UTF-8"));
        // 获取收件方的账户和名称
        message.setRecipients(MimeMessage.RecipientType.TO, email);
        // 设置主题
        message.setSubject("作业统计");

        // 设置带附件的内容
        Multipart multipart = new MimeMultipart();
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(content,  "text/html; charset=utf-8");
        multipart.addBodyPart(messageBodyPart);
        // 添加邮件附件
        MimeBodyPart attachPart = new MimeBodyPart();
        DataSource source = new FileDataSource(content);
        attachPart.setDataHandler(new DataHandler(source));
        attachPart.setFileName("作业统计");
        multipart.addBodyPart(attachPart);
        // 保存邮件内容
        message.setContent(multipart); 

	// 设置文本内容内容
	//message.setContent(content, "text/html; charset=utf-8");

        // 发送
        Transport.send(message);

    }
}
new Thread() {
    @Override
    public void run() {
        //把网络访问的代码放在这里
        try {
            MailService.send_email(emailStr, fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}.start();

 

 


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值