java mail 发送邮件

能抄送、密送、附件
注意:1、javaMail需要一个Properties来注册Session
2、邮箱密码是用邮箱里面对stmp的授权码 而不是登录密码
3、用到两个jar包 mail.jar和activator.jar

MailSender.java

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
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;
import javax.mail.internet.MimeUtility;


public class MailSender {
    private String host = ""; // smtp
    private String from = ""; // 发件地址
    private String to = ""; // 收件地址
    private String cc = ""; //抄送地址
    private String bcc = "";//密送地址
    private String affix = ""; // 附件地址
    private String user = ""; // 发件邮箱名
    private String pwd = ""; // 邮箱stmp授权码
    private String subject = ""; // 邮件标题
    private String content = "";// 邮件内容


    public void send(String host, String user, String pwd,File attachment,String from, String to, String cc , String bcc , String subject,String content) {
        this.host = host;
        this.user = user;
        this.pwd = pwd;
        this.from = from;
        this.to = to;
        this.cc = cc;
        this.bcc = bcc;
        this.subject = subject;
        this.content = content;

        Properties props = new Properties();

        // 设置邮箱服务器的Properties
        props.put("mail.smtp.host", host);
        // 设置通过服务器认证
        props.put("mail.smtp.auth", "true");

        // 创建一个 session
        Session session = javax.mail.Session.getInstance(props);

        // 开启debug模式 可以logcat上看发件时的信息
        session.setDebug(true);

        MimeMessage message = new MimeMessage(session);
        try {
            Multipart multipart = new MimeMultipart();

            // 设置发件地址
            message.setFrom(new InternetAddress(from));
            // 设置接收地址
            //多个收件地址
            String[] eachRecipients = null;
            eachRecipients = to.split("\\|");
            for(int i=0;i<eachRecipients.length;i++){
                String recipient = eachRecipients[i];
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            }
            //多个抄送地址
            String[] cceachRecipients = null;
            cceachRecipients=cc.split("\\|");
            for(int i=0;i<cceachRecipients.length;i++){
                String recipient = cceachRecipients[i];
                message.addRecipient(Message.RecipientType.CC, new InternetAddress(to));
            }
            //密送地址
            message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));

            // 设置邮件标题
            message.setSubject(subject);

            // 设置邮件内容
            BodyPart contentPart = new MimeBodyPart();
            contentPart.setText(content);
            multipart.addBodyPart(contentPart);

            // 设置附件
            if (attachment != null) {
                BodyPart attachmentBodyPart = new MimeBodyPart();
                DataSource source = new FileDataSource(attachment);
                attachmentBodyPart.setDataHandler(new DataHandler(source));
                //防止附件发出后中文变乱码
                attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
                multipart.addBodyPart(attachmentBodyPart);
            }

            message.setContent(multipart);

            message.saveChanges();

            Transport transport = session.getTransport("smtp");

            // 连接服务器
            transport.connect(host, user, pwd);

            // 发送
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

调用的时候就这样

File affix = new File("D:/AAAAAA.xls");
        String from = "a@126.com";
        String to = "b@126.com,c@126.com";
        String cc = "d@126.com,e@126.com";
        String bcc = "f@126.com";
        String title = "邮件标题 asddfad";
        String content = "邮件内容aadsfasdf12334#!!#%¥#¥…………&(&*+——“:?";
        MailSender cn = new MailSender();
        // 设置smtp服务器以及邮箱的帐号和密码
        cn.send("smtp.126.com", "a@126.com", "password", affix, from, to, cc, bcc, title, content);

就这么多了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值