基于JavaMail實現(xiàn)郵件發(fā)送
更新時間:2018年03月23日 09:13:37 作者:玩命丶DAN
這篇文章主要為大家詳細介紹了基于JavaMail實現(xiàn)郵件發(fā)送功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
通過javamail 實現(xiàn)發(fā)送郵件,供大家參考,具體內(nèi)容如下
注意:服務(wù)器有些端口是沒有開放的 需要去開放端口。 有些郵箱是需要開啟對應授權(quán)服務(wù)的。
1.maven依賴:
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api --> <dependency > <groupId >javax.mail </groupId > <artifactId >mail </artifactId > <version >1.4.5 </version > </dependency > <dependency > <groupId >com.sun.mail </groupId > <artifactId >javax.mail </artifactId > </dependency >
2.新建個實體類 用來保存信息
import java.util.Properties; public class MailSenderInfo { // 發(fā)送郵件的服務(wù)器的IP(或主機地址) private String mailServerHost; // 發(fā)送郵件的服務(wù)器的端口 private String mailServerPort; // 發(fā)件人郵箱地址 private String fromAddress; // 收件人郵箱地址 private String toAddress; // 登陸郵件發(fā)送服務(wù)器的用戶名 private String userName; // 登陸郵件發(fā)送服務(wù)器的密碼 private String password; // 是否需要身份驗證 private boolean validate = true; // 郵件主題 private String subject; // 郵件的文本內(nèi)容 private String content; // 郵件附件的文件名 private String[] attachFileNames; public Properties getProperties() { Properties p = new Properties(); p.put("mail.smtp.host", this.mailServerHost); p.put("mail.smtp.port", this.mailServerPort); //設(shè)置是否安全驗證,默認為false,一般情況都設(shè)置為true p.put("mail.smtp.auth", "true"); p.put("mail.smtp.starttls.enable","true"); p.put("mail.smtp.EnableSSL.enable","true"); return p; } public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] fileNames) { this.attachFileNames = fileNames; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String textContent) { this.content = textContent; } }
3.創(chuàng)建一個驗證器
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * 郵件用戶名和密碼認證器 */ public class MyAuthenticator extends Authenticator{ String userName = null; String password = null; public MyAuthenticator() { } public MyAuthenticator(String username, String password) { this.userName = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }
4.在調(diào)用的地方給實體類賦值
private void email(HttpSession session, String email) { // 設(shè)置郵件服務(wù)器信息 MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost("smtp-mail.outlook.com");// 發(fā)送郵件的服務(wù)器的IP(或主機地址) mailInfo.setMailServerPort("587");//有些端口在服務(wù)器上是沒開放的 這里需要注意下 mailInfo.setValidate(true); // 郵箱用戶名(根據(jù)自己情況設(shè)置) 這里可以多弄幾個郵箱過來 避免郵箱賬號需要驗證 或者被當成垃圾郵件封號 A失敗就用B mailInfo.setUserName("此處填寫跟上面發(fā)送郵件服務(wù)器對應的郵箱"); // 郵箱密碼(根據(jù)自己情況設(shè)置) mailInfo.setPassword("這是你的密碼"); // 發(fā)件人郵箱(根據(jù)自己情況設(shè)置,如果你沒對郵箱進行特別設(shè)置,應該和郵箱用戶名一致) mailInfo.setFromAddress("這里跟上面一樣"); // 收件人郵箱(根據(jù)自己情況設(shè)置) mailInfo.setToAddress(email); // 郵件標題 mailInfo.setSubject("我是標題"); // 郵件內(nèi)容 mailInfo.setContent("我是內(nèi)容,正經(jīng)的內(nèi)容不是垃圾郵箱"); // 發(fā)送郵件 SimpleMailSender sms = new SimpleMailSender(); // 發(fā)送文體格式 sms.sendTextMail(mailInfo); }
5.這里才是真正的發(fā)送郵件
public class SimpleMailSender { public boolean sendTextMail(MailSenderInfo mailInfo) { // 判斷是否需要身份認證 MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { // 如果需要身份認證,則創(chuàng)建一個密碼驗證器 authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } // 根據(jù)郵件會話屬性和密碼驗證器構(gòu)造一個發(fā)送郵件的session Session sendMailSession = Session.getDefaultInstance(pro, authenticator); try { // 根據(jù)session創(chuàng)建一個郵件消息 Message mailMessage = new MimeMessage(sendMailSession); // 創(chuàng)建郵件發(fā)送者地址 Address from = new InternetAddress(mailInfo.getFromAddress()); // 設(shè)置郵件消息的發(fā)送者 mailMessage.setFrom(from); // 創(chuàng)建郵件的接收者地址,并設(shè)置到郵件消息中 Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO, to); // 設(shè)置郵件消息的主題 mailMessage.setSubject(mailInfo.getSubject()); // 設(shè)置郵件消息發(fā)送的時間 mailMessage.setSentDate(new Date()); // 設(shè)置郵件消息的主要內(nèi)容 String mailContent = mailInfo.getContent(); mailMessage.setText(mailContent); mailMessage.saveChanges(); // 發(fā)送郵件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot使用properties定義短信模板的方法教程
這篇文章主要給大家介紹了關(guān)于spring boot使用properties定義短信模板的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01SpringCloud超詳細講解微服務(wù)網(wǎng)關(guān)Zuul
這篇文章主要介紹了SpringCloud Zuul微服務(wù)網(wǎng)關(guān),負載均衡,熔斷和限流,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07