java 发送email

package com.yushang.e_contract.util;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtil {

    // 设置服务器
    private static final String KEY_SMTP = "mail.smtp.host";
    private static final String VALUE_SMTP = "smtp.163.com";
    // 服务器验证
    private static final String KEY_PROPS = "mail.smtp.auth";
    private static final boolean VALUE_PROPS = true;
    // 发件人用户名、密码
    private final String SEND_USER = "mayicontract@163.com";
    private final String SEND_UNAME = "mayicontract@163.com";
    private final String SEND_PWD = "**************"; // 163设置打开smtp获取授权码
    // 建立会话
    private final MimeMessage message;
    private final Session s;

    /*
     * 初始化方法
     */
    public MailUtil() {
        Properties props = System.getProperties();
        props.setProperty(KEY_SMTP, VALUE_SMTP);
        props.put(KEY_PROPS, "true");

        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.port", "465");
        s = Session.getDefaultInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(SEND_UNAME, SEND_PWD);
            }
        });
        s.setDebug(true);
        message = new MimeMessage(s);
    }

    /**
     * 发送邮件
     *
     * @param headName    邮件头文件名
     * @param sendHtml    邮件内容
     * @param receiveUser 收件人地址
     */
    public void doSendHtmlEmail(String headName, String sendHtml,
                                String receiveUser) {
        try {
            System.out.println("开始发送邮箱~~~");
            // 发件人
            InternetAddress from = new InternetAddress(SEND_USER);
            message.setFrom(from);
            // 收件人
            InternetAddress to = new InternetAddress(receiveUser);
            message.setRecipient(Message.RecipientType.TO, to);
            // 邮件标题
            message.setSubject(headName);
            String content = sendHtml.toString();
            // 邮件内容,也可以使纯文本"text/plain"
            message.setContent(content, "text/html;charset=GBK");
            message.saveChanges();
            Transport transport = s.getTransport("smtp");
            // smtp验证,就是你用来发邮件的邮箱用户名密码
            transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
            // 发送
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

            System.out.println("邮箱发送成功!");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MailUtil se = new MailUtil();
        se.doSendHtmlEmail("测试", "<a href='www.baidu.com'>点击跳转百度</a>", "zeen8876@163.com");
    }
}

扫描下方图片关注公众号【程序员每日一学】让我们每天一起进步~

java 发送email

上一篇:Servlet的Maven 依赖和web.xml


下一篇:cookie 实现Java项目登入页面(记住密码)功能