java 邮件推送工具

 

1.添加依赖的包

      
		javax.mail
		    mail
			1.4.7
		
		
			javax.activation
			activation
			1.1.1
		

 2.配置邮箱配置

dabay:
  HOST:     XXXXXX                            ---邮箱发送服务器(/需要注册)
  PROTOCOL: smtp                              ---发送协议      
  PORT: 25                                    ---端口
  FROM:    *****.@XXXXX                       ---发送人的邮箱
  PWORRD: XXXXXXXXXX                          ---发送人邮箱密码  
  toEmil:    *******.@XXXXX                   ---接收人邮箱账号

3.引入工具类

/**
 * 
 */
package 

import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

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


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Description:   
 * @author 
 * @date 2018年5月7日下午4:41:38
 *  
 */
@Component
public class SendEmail {

@Value("${dabay.HOST}")
private String  host;
    @Value("${dabay.PROTOCOL}")
    private String  protocol;
    @Value("${dabay.PORT}")
    private String  port;
    @Value("${dabay.FROM}")
    private String  from;
    @Value("${dabay.PWORRD}")
    private String  password;

    /**
     * 获取Session
     * @return
     * @throws GeneralSecurityException 
     */
    private Session getSession() {
        Properties props = new Properties();
        //设置服务器地址
        props.put("mail.smtp.host", host);
        //设置协议
        props.put("mail.store.protocol" ,protocol);
        //设置端口
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth" , true);
        
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }

        };
        Session session = Session.getDefaultInstance(props , authenticator);

        return session;
    }

    public  void send(String toEmail , String content,String title) {
    	Session session = getSession();
        try {
            // Instantiate a message
            Message msg = new MimeMessage(session);

            //Set message attributes
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(toEmail)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(title);
            msg.setSentDate(new Date());
            msg.setContent(content , "text/html;charset=utf-8");

            //Send the message
            Transport.send(msg);
        }
        catch (Exception mex) {
            mex.printStackTrace();
        }
    }
}

5.调用

@Value("${dabay.toEmil}")
private  String toEmil;

String content="邮件推送内容";
String title="邮件推送标题";

sendEmail.send(toEmil,content,title);

 

你可能感兴趣的:(deom,总结,常用)