Java发送腾讯企业邮箱邮件,基于ssl协议

在工作中很多时候需要基于代码发送邮件,最近基于JavaMail写了一个测试demo
引入依赖,


        
            javax.mail
            mail
            1.4.1
        
        
            javax.mail
            mail
            1.4.4
        
    

实例代码

package com.demo;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

public class EMailDemo {

    private static String account = "";	//登录用户名
    private static String pass = "";		//登录密码
    private static String host = "smtp.exmail.qq.com";		//服务器地址(邮件服务器)
    private static String port = "465";		//端口
    private static String protocol = "smtp"; //协议

    private static MimeMessage mimeMessage;

    /**
    * 用户名密码验证,需要实现抽象类Authenticator的抽象方法PasswordAuthentication,
   * SMTP验证类(内部类),继承javax.mail.Authenticator
   */
    static class MyAuthenricator extends Authenticator {
        String username = null;
        String password = null;
        public MyAuthenricator(String username,String password){
            this.username=username;
            this.password=password;
        }
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username,password);
        }
    }

    /**
   * 指定发送邮件
   * @param subject 邮件主题
   * @param sendHtml 邮件内容
   * @param receiveUser 收件人
    */
    public static void sendEmail(String subject,
            String sendHtml, String receiveUser){
        Properties prop = new Properties();
        // 协议
        prop.setProperty("mail.transport.protocol", protocol);
        // 服务器
        prop.setProperty("mail.smtp.host", host);
        // 端口
        prop.setProperty("mail.smtp.port", port);
        // 使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");

        //使用SSL,企业邮箱必需!
        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }

        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass));
        // 开启DEBUG模式,在控制台中或日志中有日志信息显示,也就是可以从控制台中看一下服务器的响应信息;
        session.setDebug(true);
        mimeMessage = new MimeMessage(session);

        try {
            //发件人
            mimeMessage.setFrom(new InternetAddress(account));

            //收件人
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveUser));
            //主题
            mimeMessage.setSubject(subject);
            //时间
            mimeMessage.setSentDate(new Date());

            //仅仅发送文本
            mimeMessage.setText(sendHtml,"UTF-8");
            mimeMessage.saveChanges();
            Transport.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        sendEmail("test", "测试", "[email protected]");
    }
}

在测试类添加收件人和发件人以及发件人密码就可以发送邮件了.

你可能感兴趣的:(java,email)