java實(shí)現(xiàn)郵件發(fā)送邏輯并不復(fù)雜(不包含附件),只是根據(jù)官方調(diào)用官方提供的sdk,首先需要引入maven依賴:
茅箭網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),茅箭網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為茅箭1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個(gè)售后服務(wù)好的茅箭做網(wǎng)站的公司定做!
javax.mail
<dependency > <groupId >com.sun.mail</groupId > <artifactId >javax.mail</artifactId > <version >1.6.0</version > </dependency >
然后構(gòu)造發(fā)送郵件所需的實(shí)體類
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)建會(huì)話 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ā)送方法中使用到驗(yàn)證郵箱登錄名和密碼是否正確的方法
package com.email; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * 驗(yàn)證郵箱 * @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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前標(biāo)題:java實(shí)現(xiàn)郵件發(fā)送詳解
分享路徑:http://muchs.cn/article36/gjsopg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、搜索引擎優(yōu)化、關(guān)鍵詞優(yōu)化、標(biāo)簽優(yōu)化、定制開發(fā)、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)