您好,登錄后才能下訂單哦!
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郵箱發送,需要注意的是,此處的密碼不是登錄密碼呦,而是設置中客戶端授權密碼呦。
執行測試文件之后,可以登錄郵箱看到發送的結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。