JavaMail笔记

如何在java中发送邮件?我这里参考的是JavaMail开源项目

依赖jar包:java.mail.jar 点击下载

普通邮件发送

测试用的是163邮箱,测试邮件发多了就报554了被识别为垃圾邮件–!

import com.sun.mail.smtp.SMTPTransport;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

public class Mail {

    public static void main(String[] args) {
        try {
            sendEmail("出塞", "秦时明月汉时关,万里长征人未还", "[email protected]");
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param subject 邮件主题
     * @param text 邮件内容
     * @param to 收件人
     * @throws GeneralSecurityException
     * @throws MessagingException
     */
    private static void sendEmail(String subject, String text, String to) throws GeneralSecurityException, MessagingException {
        String mailer   = "Microsoft Outlook Express 6.00.2900.2869"; // 发信客户端
        String mailhost = "smtp.163.com"; // smtp服务器
        String port     = "25"; // 默认端口为25
        String user     = "[email protected]"; // 邮箱账号
        String password = "xxx"; // 邮箱密码

        // 1、邮件会话实例
        Properties props = new Properties();        // 属性设置
        props.put("mail.smtp.host", mailhost);
        props.put("mail.smtp.auth", "true");

        Session session = Session.getInstance(props, null);
        session.setDebug(true);

        // 2、邮件消息内容封装
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(user));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        msg.setSubject(subject);
        msg.setText(text);
        msg.setHeader("X-Mailer", mailer);
        msg.setSentDate(new Date());

        // 3、连接服务器并发送邮件
        SMTPTransport t = (SMTPTransport) session.getTransport();
        try {
            t.connect(user, password);
            t.sendMessage(msg, msg.getAllRecipients());
        } finally {
            t.close();
        }

        System.out.println("\nMail was sent successfully.");
    }
}

SSL加密

测试用了163和sina的邮箱都成功了

import com.sun.mail.smtp.SMTPTransport;
import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

public class Mail {

    public static void main(String[] args) {
        try {
            sendEmail("出塞", "秦时明月汉时关,万里长征人未还", "[email protected]");
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    private static void sendEmail(String subject, String text, String to) throws GeneralSecurityException, MessagingException {
        String mailer   = "Microsoft Outlook Express 6.00.2900.2869"; // 发信客户端
        String mailhost = "smtp.sina.com";
        String port     = "465";
        String user     = "[email protected]"; // 邮箱账号
        String password = "xxx"; // 邮箱密码

        // 属性设置
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", mailhost);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", port);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.ssl.enable", "true"); // 启用SSL
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.socketFactory", sf); // 信任证书

        // 1、邮件会话
        Session session = Session.getInstance(props, null);
        session.setDebug(true);

        // 2、邮件内容
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(user));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        msg.setSubject(subject);
        msg.setText(text);
        msg.setHeader("X-Mailer", mailer);
        msg.setSentDate(new Date());

        // 3、连接服务器
        SMTPTransport t = (SMTPTransport) session.getTransport();
        try {
            t.connect(user, password);
            t.sendMessage(msg, msg.getAllRecipients());
        } finally {
            t.close();
        }

        System.out.println("\nMail was sent successfully.");
    }
}

STARTTLS加密

测试用的是微软outlook邮箱发送成功

import com.sun.mail.smtp.SMTPTransport;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

public class Mail {

    public static void main(String[] args) {
        try {
            sendEmail("出塞", "秦时明月汉时关,万里长征人未还", "[email protected]");
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    private static void sendEmail(String subject, String text, String to) throws GeneralSecurityException, MessagingException {
        String mailer   = "Microsoft Outlook Express 5.50.4807.1700"; // 发信客户端
        String user     = "[email protected]"; // 邮箱账号
        String password = "xxx"; // 邮箱密码

        // 属性设置
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp-mail.outlook.com"); // 微软outlook smtp服务器
        props.put("mail.smtp.port", "587");

        // 1、邮件会话
        Session session = Session.getInstance(props, null);
        session.setDebug(true); // 设置打印日志

        // 2、邮件内容
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(user));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        msg.setSubject(subject);
        msg.setText(text);
        msg.setHeader("X-Mailer", mailer);
        msg.setSentDate(new Date());

        // 3、连接服务器
        SMTPTransport t = (SMTPTransport) session.getTransport();
        try {
            t.connect(user, password);
            t.sendMessage(msg, msg.getAllRecipients());
        } finally {
            t.close();
        }

        System.out.println("\nMail was sent successfully.");
    }
}

注:有些邮箱可能需要去官网设置smtp状态,因为可能默认没有开启smtp支持;并且smtp服务器的具体地址一般在邮箱官网设置里寻找;

参考

http://www.oracle.com/technetwork/java/javamail/index.html
https://javaee.github.io/javamail/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章