java實現(xiàn)郵件發(fā)送詳解
java實現(xiàn)郵件發(fā)送邏輯并不復(fù)雜(不包含附件),只是根據(jù)官方調(diào)用官方提供的sdk,首先需要引入maven依賴:
javax.mail
<dependency > <groupId >com.sun.mail</groupId > <artifactId >javax.mail</artifactId > <version >1.6.0</version ></dependency >
然后構(gòu)造發(fā)送郵件所需的實體類
package com.email;import java.io.Serializable;/** * @Author zjt * @Date 2019年03月07 10:37 */public class EmailEntity implements Serializable { private static final long serialVersionUID = 1L; //郵箱服務(wù)器地址 private String host; //主機(jī)端口 private Integer port; //發(fā)送者的郵箱賬號 private String userName; //發(fā)送者的密碼 private String password; //發(fā)送者的郵箱地址 private String fromAddress; //接收者的郵箱地址 private String toAddress; //設(shè)置郵件主題 private String subject; //設(shè)置郵件內(nèi)容 private String context; //設(shè)置郵件類型 private String contextType; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public Integer getPort() { return port; } public void setPort(Integer port) { this.port = port; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } 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 getContext() { return context; } public void setContext(String context) { this.context = context; } public String getContextType() { return contextType; } public void setContextType(String contextType) { this.contextType = contextType; }}
其次,編寫調(diào)用郵件發(fā)送方法
package com.email;import javax.mail.*;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.util.*;/** * @Author zjt * @Date 2019年03月07 10:38 */public class EmailSend { public static boolean EmailSendTest(EmailEntity emailEntity){ try { //配置文件 Properties properties = new Properties(); properties.put('mail.smtp.auth', 'true'); properties.put('mail.smtp.host', emailEntity.getHost()); properties.put('mail.smtp.port', 25); properties.put('mail.smtp.starrttls.enable', 'true'); //創(chuàng)建會話 VerifyEmail verifyEmail = new VerifyEmail(emailEntity.getUserName(), emailEntity.getPassword()); Session mailSession = Session.getInstance(properties, verifyEmail); mailSession.setDebug(true); //創(chuàng)建信息對象 Message message = new MimeMessage(mailSession); InternetAddress from = new InternetAddress(emailEntity.getFromAddress()); InternetAddress to = new InternetAddress(emailEntity.getToAddress()); //設(shè)置郵件信息的來源 message.setFrom(from); //設(shè)置郵件的接收者 message.setRecipient(MimeMessage.RecipientType.TO, to); message.setSubject(emailEntity.getSubject()); //設(shè)置郵件發(fā)送日期 message.setSentDate(new Date()); //設(shè)置郵件內(nèi)容 message.setContent(emailEntity.getContext() , emailEntity.getContextType()); message.saveChanges(); //發(fā)送郵件 Transport transport = mailSession.getTransport('smtp'); transport.connect(emailEntity.getHost(), emailEntity.getUserName(), emailEntity.getPassword()); System.out.println('發(fā)送:' + transport); transport.sendMessage(message, message.getAllRecipients()); System.out.println('success'); return true; } catch (MessagingException e) { e.printStackTrace(); System.out.println('fial...'); return false; } }}
在調(diào)用郵件發(fā)送方法中使用到驗證郵箱登錄名和密碼是否正確的方法
package com.email;import javax.mail.Authenticator;import javax.mail.PasswordAuthentication;/** * 驗證郵箱 * @Author zjt * @Date 2019年03月07 10:32 */public class VerifyEmail extends Authenticator { //賬號 private String userName; //密碼 private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } //構(gòu)造方法 public VerifyEmail(){ super(); } public VerifyEmail(String userName, String password) { super(); this.userName = userName; this.password = password; } protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(userName, password); }}
編寫測試類,測試郵件發(fā)送方法是否成功
package com.email;import org.junit.jupiter.api.Test;/** * @Author zjt * @Date 2019年03月07 10:26 */public class TestEmail { @Test public void test(){ EmailEntity email = new EmailEntity(); email.setUserName('*******@163.com'); email.setPassword('******'); email.setHost('smtp.163.com'); email.setPort(25); email.setFromAddress('******@163.com'); email.setToAddress('******@163.com'); email.setSubject('這是一封測試郵件!!!!'); email.setContext('看看這是什么'); email.setContextType('text/html;charset=utf-8'); boolean flag = EmailSend.EmailSendTest(email); System.err.println('郵件發(fā)送結(jié)果=='+flag); }}
在這里測試的163郵箱發(fā)送,需要注意的是,此處的密碼不是登錄密碼呦,而是設(shè)置中客戶端授權(quán)密碼呦。
執(zhí)行測試文件之后,可以登錄郵箱看到發(fā)送的結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
