我這里用的郵箱主要是用來(lái)新用戶注冊(cè)信息的時(shí)候用來(lái)郵件激活的
<!-- 發(fā)送郵件的maven -->
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!--發(fā)送郵件的gradle-->
// https://mvnrepository.com/artifact/javax.mail/mail
compile group: 'javax.mail', name: 'mail', version: '1.4.7'
package com.mcin.email;
import org.apache.log4j.Logger;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
/**
* Created by Mcin on 2017/5/17.
* 郵箱發(fā)送
*/
public class Email {
private static final Logger logger = Logger.getLogger(Email.class);
// 我這里用的是163郵箱
public static final String HOST = "smtp.163.com"; // 發(fā)送郵件的服務(wù)器地址
public static final String PROTOCOL = "smtp"; // 發(fā)送郵件的協(xié)議
public static final int PORT = 25;
public static final String EMAIL_FROM = "發(fā)件人的email";//發(fā)件人的email
public static final String EMAIL_PWD = "發(fā)件人密碼";//發(fā)件人密碼
/**
* 獲取Session
* @return
*/
public static Session getSession() {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);//設(shè)置服務(wù)器地址
props.put("mail.store.protocol" , PROTOCOL);//設(shè)置協(xié)議
props.put("mail.smtp.port", PORT);//設(shè)置端口
props.put("mail.smtp.auth" , true);
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(EMAIL_FROM, EMAIL_PWD);
}
};
Session session = Session.getDefaultInstance(props , authenticator);
return session;
}
/**
* 發(fā)送郵件信息的內(nèi)容
* @param host
* @param email
* @param activeCode
* @param ch
* @return
*/
public static String sendEmailcentot(String host,String email,String activeCode,char ch) {
StringBuffer sb = new StringBuffer("點(diǎn)擊下面鏈接激活賬號(hào),48小時(shí)內(nèi)有效,否則重新注冊(cè)賬號(hào),鏈接只能使用一次,請(qǐng)盡快激活!</br>");
sb.append("<a href=");
sb.append(ch);
sb.append(host);
sb.append("/home/activeEmail.json?action=activate&email=");
sb.append(email);
sb.append("&activeCode=");
sb.append(activeCode);
sb.append("\">"+host+"/user/register.json?action=activate&email=");
sb.append(email);
sb.append("&activeCode=");
sb.append(activeCode);
sb.append("</a>");
return sb.toString();
}
/**
* 發(fā)送郵件
* @param toEmail
* @param host
* @return
*/
public static Integer sendEmail (String toEmail,String host,String activeCode){
Integer sendResulr = 0;
char ch = '"';
String content = sendEmailcentot(host,toEmail,activeCode,ch);
Session session = getSession();
try {
logger.info("--開(kāi)始發(fā)送郵件 --"+"郵箱 == "+toEmail+" : "+content);
// Instantiate a message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(EMAIL_FROM));
InternetAddress[] address = {new InternetAddress(toEmail)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("賬號(hào)激活郵件");
msg.setSentDate(new Date());
msg.setContent(content , "text/html;charset=utf-8");
//發(fā)送信息
Transport.send(msg);
sendResulr = 1 ; //郵件發(fā)送成功
} catch (MessagingException e) {
logger.error("郵件發(fā)送出現(xiàn)異常 ----------- :"+e.getMessage());
e.printStackTrace();
return sendResulr;
}
return sendResulr;
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。