java 发送邮件

依赖:

 
          javax.mail
          mail
          1.4.7
 

代码:

public class MailUtil {

    private static final String HOST = "stmp.xxxxx.com";

    private static final String SENDER = "[email protected]";

    private static final String PASS_TOKEN = "xfvraxxxxxhirxxxbighe";

    private Session session;

    private Message msg;

    private InternetAddress[] sendTo;

    private String subject;

    private String content;


    public MailUtil(String subject, String content, List toList) throws Exception {
        this.init(subject, content, toList);
    }

    private void init(String subject, String content, List toList) throws MessagingException {
        this.subject = subject;
        this.content = content;
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.put("mail.smtp.HOST", HOST);//服务器地址
        //初始化session
        this.session = Session.getInstance(props);
        session.setDebug(true);
        //初始化发送地址列表
        this.sendTo = new InternetAddress[toList.size()];
        for (int i = 0, j = toList.size(); i < j; i++) {
            this.sendTo[i] = new InternetAddress(toList.get(i));
        }
        //初始化message
        this.msg = new MimeMessage(session);
        this.msg.setSubject(this.subject);
        this.msg.setSentDate(new Date());
        //发件人邮箱
        this.msg.setFrom(new InternetAddress(this.SENDER));
        //收件人邮箱
        this.msg.setRecipients(Message.RecipientType.TO,
                this.sendTo);
    }


    private void sendMessage() throws Exception {
        //这时Transport对象与相应传输协议通信,这里是SMTP协议
        Transport transport = session.getTransport();
        // 登录邮箱 。配置发件人邮箱,密码或授权码
        transport.connect(SENDER, PASS_TOKEN);
        //发送邮件
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
    }

    public void send() throws Exception {
        msg.setText(content);
        this.sendMessage();
    }

    public void send(List files) throws Exception {

        List fileList = new ArrayList<>(files.size());
        files.forEach(x -> fileList.add(new File(x)));

        Multipart multipart = new MimeMultipart();

        // 设置正文
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(content, "text/html;charset=utf-8");
        multipart.addBodyPart(messageBodyPart);

        if (files.size() > 0) {
            for (File file : fileList) {
                BodyPart attachmentPart = new MimeBodyPart();
                DataSource source = new FileDataSource(file);
                attachmentPart.setDataHandler(new DataHandler(source));
                //避免中文乱码的处理
                attachmentPart.setFileName(file.getName());
                multipart.addBodyPart(attachmentPart);
            }
        }
        // 发送完整消息
        msg.setContent(multipart);
        msg.saveChanges();
        this.sendMessage();
    }


    public static void main(String[] args) throws Exception {
        List sendTo = new ArrayList<>();
        sendTo.add("[email protected]");

        List files = new ArrayList<>();
        files.add("C:\\阿里巴巴Java开发...1528268103.pdf");
        files.add("C:\\Desktop\\数据库.txt");
        //带附件
        new MailUtil("java主题", "java内容 hello world", sendTo).send(files);
       //不带附件
        new MailUtil("java主题", "java内容 hello world", sendTo).send();

    }

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