-
在我們開發過程中經常會用到郵件,比如 : 發送通知,找回密碼 驗證碼 等等,再次總結了使用javaMail發送郵件,無需單間james等郵件服務器也可發送郵件
-
javaMail官網 在里面可以找到詳細的文檔以及案例和jar包
-
我們都知道在前后端交互都是有協議的,http協議,JavaMail也有自己的協議,SMTP/POP3和IMAP
-
使用javaMail前提是可以連接外網.
-
廢話不多說,直接上案例:
-
導入依賴,在沒使用maven的話導入相應的jar包,點此下載
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.2</version> </dependency>
-
測試案例:這是一個模板工具
package com.bgi.util; import org.springframework.core.io.ClassPathResource; import javax.mail.Address; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.io.IOException; import java.util.Date; import java.util.Properties; public class EmailUtil {
//獲取屬性文件中的值,建議把配置的信息放到屬性文件中,方便修改和獲取 private static Properties properties = new Properties(); static{ try {
//加在屬性文件 properties.load(new ClassPathResource("properties/email.properties").getInputStream()); } catch (IOException e) { } } public static String SMTPSERVER = properties.getProperty("smtp.server"); //從屬性文件中獲取值其中key為smtp.server public static String SMTPPORT = properties.getProperty("smtp.port"); //端口號 465 465 465 不是456 public static String ACCOUT = properties.getProperty("smtp.account");//賬戶名:我的是163賬戶,此賬戶必須在設置中開啟授權碼授權 public static String PWD = properties.getProperty("smtp.pwd"); //授權密碼 public static String users = properties.getProperty("email.users"); //這里是發送給多個用戶多個用戶用都好分割xxx@xx.com,xxx@xx.com public static void sendEmail(String content){ try { // 創建郵件配置 Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); // 使用的協議(JavaMail規范要求) props.setProperty("mail.smtp.host", SMTPSERVER); // 發件人的郵箱的 SMTP 服務器地址 props.setProperty("mail.smtp.port", SMTPPORT); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.auth", "true"); // 需要請求認證 props.setProperty("mail.smtp.ssl.enable", "true");// 開啟ssl // 根據郵件配置創建會話,注意session別導錯包 Session session = Session.getDefaultInstance(props); // 開啟debug模式,可以看到更多詳細的輸入日志 session.setDebug(true); //創建郵件 MimeMessage message = createEmail(session,users,content); //將用戶和內容傳遞過來 //獲取傳輸通道 Transport transport = session.getTransport(); transport.connect(SMTPSERVER,ACCOUT, PWD); //連接,並發送郵件 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { e.printStackTrace(); } } public static MimeMessage createEmail(Session session,String users,String content) throws Exception { // 根據會話創建郵件 MimeMessage msg = new MimeMessage(session); // address郵件地址, personal郵件昵稱, charset編碼方式 InternetAddress fromAddress = new InternetAddress(ACCOUT, "中間件推送", "utf-8"); // 設置發送郵件方 msg.setFrom(fromAddress); // 單個可以直接這樣創建 // InternetAddress receiveAddress = new InternetAddress(); // 設置郵件接收方 Address[] internetAddressTo = new InternetAddress().parse(users);//type:
要被設置為 TO, CC 或者 BCC,這里 CC 代表抄送、BCC 代表秘密抄送。舉例:Message.RecipientType.TOmsg.setRecipients(MimeMessage.RecipientType.TO, internetAddressTo); // 設置郵件標題 msg.setSubject("測試標題", "utf-8"); msg.setText(content); // 設置顯示的發件時間 msg.setSentDate(new Date()); // 保存設置 msg.saveChanges(); return msg; } }
-
email.properties
smtp.server=smtp.163.com smtp.port=465 smtp.account=xxx@163.com smtp.pwd=xxxxx email.users=xxx@163.com,xxxx@qq.com,xxx@xx.cn
-
163郵箱開啟授權:
-
-
qq郵箱開啟授權: 點擊生成授權碼,即可生成授權碼
到此已經可以發送郵件了,如果需要添加附件,可以自行研究,