java 工具篇 (mail 邮件发送)

 

注意 :需要两个jar包 activation.jar‘、mail.jar放在项目中 (可直接在怕pom中下载)

网易企业账号的服务器是smtphm.qiye.163.com个人是smtp.163.com

:打开163邮箱并登录:

找到设置->P0P3/SMTP/IMAP。

java 工具篇 (mail 邮件发送)_第1张图片

这里将POP3/SMTP服务器勾上,并且获取授权码即可。在下面我们可以看到SMTP服务器主机名。
将这两个信息填上去,就可以愉快的发送邮件了!!

二:需要获取到授权码

然后就贴代码拉~~~~~~~~

实体

package java_mail;

/**
 * @author LiBin
 * @create 2018-05-22
 **/
public class MailInfo {
    //邮箱服务器 如smtp.163.com
    private String host ;
    //用户邮箱 如**@163
    private String formName ;
    //用户授权码 不是用户名密码 可以自行查看相关邮件服务器怎么查看
    private String formPassword ;
    //消息回复邮箱
    private String replayAddress ;
    //发送地址
    private String toAddress ;
    //发送主题
    private String subject ;
    //发送内容
    private String content ;
    //发件人名字
    private String outName ;

    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public String getFormName() {
        return formName;
    }
    public void setFormName(String formName) {
        this.formName = formName;
    }
    public String getFormPassword() {
        return formPassword;
    }
    public void setFormPassword(String formPassword) {
        this.formPassword = formPassword;
    }
    public String getReplayAddress() {
        return replayAddress;
    }
    public void setReplayAddress(String replayAddress) {
        this.replayAddress = replayAddress;
    }
    public String getToAddress() {
        return toAddress;
    }
    public void setToAddress(String toAddress) {
        this.toAddress = toAddress;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

    public String getOutName() {
        return outName;
    }

    public void setOutName(String outName) {
        this.outName = outName;
    }
}

 实现方法类

package test;

import org.junit.Test;

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.Security;
import java.util.Date;
import java.util.Properties;

/**
 * @author LiBin
 * @create 2018-05-22
 **/
public class MailSendUtil {    

    private final static String host = "smtphm.qiye.163.com"; //163的服务器
    private final static String formName = "[email protected]";//你的邮箱(打开P0P3/SMTP/IMAP的邮箱)
    private final static String password = "BRe6guSTGL1fYkZW"; //授权码
    private final static String replayAddress = "[email protected]"; //你的邮箱(发送人的)

   /**
    * 发送html邮件
    *
    **/
    public static void sendHtmlMail(MailInfo info)throws Exception{
        info.setHost(host);
        info.setFormName(formName);
        info.setFormPassword(password);   //网易邮箱的授权码
        info.setReplayAddress(replayAddress);

        Message message = getMessage(info);
        // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(info.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 将MiniMultipart对象设置为邮件内容
        message.setContent(mainPart);
        Transport.send(message);
    }



    /**
     * 发送文字邮件
     *  
     **/
    public static void sendTextMail(MailInfo info) throws Exception {

        info.setHost(host);
        info.setFormName(formName);
        info.setFormPassword(password);   //网易邮箱的授权码
        info.setReplayAddress(replayAddress);
        Message message = getMessage(info);
        //消息发送的内容
        message.setText(info.getContent());

        Transport.send(message);
    }

   
    private static Message getMessage(MailInfo info) throws Exception{
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final Properties p = System.getProperties() ;
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        p.setProperty("mail.smtp.host", info.getHost());
        p.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.user", info.getFormName());

        p.setProperty("mail.smtp.pass", info.getFormPassword());
        //邮箱发送服务器端口,这里设置为465端口
        p.setProperty("mail.smtp.port", "465");
        p.setProperty("mail.smtp.socketFactory.port", "465");

        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session session = Session.getInstance(p, new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
            }
        });
        session.setDebug(true);
        Message message = new MimeMessage(session);
        //消息发送的主题
        message.setSubject(info.getSubject());
        //接受消息的人
        message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
        //消息的发送者
        message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"****云平台"));
        // 创建邮件的接收者地址,并设置到邮件消息中
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(info.getToAddress()));
        // 消息发送的时间
        message.setSentDate(new Date());
        return message ;
    }

}

 

注:看到很多人(mail.smtp.port)用25的端口,在本地测试不会有问题但是放在服务器25端口就会被占用

你可能感兴趣的:(工具)