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("出塞", "秦时明月汉时关,万里长征人未还", "xxx@qq.com");
        } 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     = "xxx@163.com"; // 邮箱账号
        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("出塞", "秦时明月汉时关,万里长征人未还", "xxx@qq.com");
        } 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     = "xxx@sina.com"; // 邮箱账号
        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("出塞", "秦时明月汉时关,万里长征人未还", "xxx@outlook.com");
        } 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     = "xxx@outlook.com"; // 邮箱账号
        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/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值