java實現郵件傳送詳解
阿新 • • 發佈:2020-03-30
java實現郵件傳送邏輯並不複雜(不包含附件),只是根據官方呼叫官方提供的sdk,首先需要引入maven依賴:
javax.mail
<dependency > <groupId >com.sun.mail</groupId > <artifactId >javax.mail</artifactId > <version >1.6.0</version > </dependency >
然後構造傳送郵件所需的實體類
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; //郵箱伺服器地址 private String host; //主機埠 private Integer port; //傳送者的郵箱賬號 private String userName; //傳送者的密碼 private String password; //傳送者的郵箱地址 private String fromAddress; //接收者的郵箱地址 private String toAddress; //設定郵件主題 private String subject; //設定郵件內容 private String context; //設定郵件型別 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; } }
其次,編寫呼叫郵件傳送方法
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"); //建立會話 VerifyEmail verifyEmail = new VerifyEmail(emailEntity.getUserName(),emailEntity.getPassword()); Session mailSession = Session.getInstance(properties,verifyEmail); mailSession.setDebug(true); //建立資訊物件 Message message = new MimeMessage(mailSession); InternetAddress from = new InternetAddress(emailEntity.getFromAddress()); InternetAddress to = new InternetAddress(emailEntity.getToAddress()); //設定郵件資訊的來源 message.setFrom(from); //設定郵件的接收者 message.setRecipient(MimeMessage.RecipientType.TO,to); message.setSubject(emailEntity.getSubject()); //設定郵件傳送日期 message.setSentDate(new Date()); //設定郵件內容 message.setContent(emailEntity.getContext(),emailEntity.getContextType()); message.saveChanges(); //傳送郵件 Transport transport = mailSession.getTransport("smtp"); transport.connect(emailEntity.getHost(),emailEntity.getUserName(),emailEntity.getPassword()); System.out.println("傳送:" + transport); transport.sendMessage(message,message.getAllRecipients()); System.out.println("success"); return true; } catch (MessagingException e) { e.printStackTrace(); System.out.println("fial..."); return false; } } }
在呼叫郵件傳送方法中使用到驗證郵箱登入名和密碼是否正確的方法
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; } //構造方法 public VerifyEmail(){ super(); } public VerifyEmail(String userName,String password) { super(); this.userName = userName; this.password = password; } protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(userName,password); } }
編寫測試類,測試郵件傳送方法是否成功
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("郵件傳送結果=="+flag); } }
在這裡測試的163郵箱傳送,需要注意的是,此處的密碼不是登入密碼呦,而是設定中客戶端授權密碼呦。
執行測試檔案之後,可以登入郵箱看到傳送的結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。