基於JavaMail的郵件傳送
阿新 • • 發佈:2019-01-04
通過javamail 實現傳送郵件。
注意:伺服器有些埠是沒有開放的 需要去開放埠。 有些郵箱是需要開啟對應授權服務的。
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 >
- import java.util.Properties;
- publicclass MailSenderInfo {
- // 傳送郵件的伺服器的IP(或主機地址)
- private String mailServerHost;
- // 傳送郵件的伺服器的埠
- private String mailServerPort;
- // 發件人郵箱地址
- private String fromAddress;
- // 收件人郵箱地址
- private String toAddress;
- // 登陸郵件傳送伺服器的使用者名稱
- private String userName;
- // 登陸郵件傳送伺服器的密碼
- private String password;
- // 是否需要身份驗證
- privateboolean validate = true;
- // 郵件主題
- private String subject;
- // 郵件的文字內容
- 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);
- //設定是否安全驗證,預設為false,一般情況都設定為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;
- }
- publicvoid setMailServerHost(String mailServerHost) {
- this.mailServerHost = mailServerHost;
- }
- public String getMailServerPort() {
- return mailServerPort;
- }
- publicvoid setMailServerPort(String mailServerPort) {
- this.mailServerPort = mailServerPort;
- }
- publicboolean isValidate() {
- return validate;
- }
- publicvoid setValidate(boolean validate) {
- this.validate = validate;
- }
- public String[] getAttachFileNames() {
- return attachFileNames;
- }
- publicvoid setAttachFileNames(String[] fileNames) {
- this.attachFileNames = fileNames;
- }
- public String getFromAddress() {
- return fromAddress;
- }
- publicvoid setFromAddress(String fromAddress) {
- this.fromAddress = fromAddress;
- }
- public String getPassword() {
- return password;
- }
- publicvoid setPassword(String password) {
- this.password = password;
- }
- public String getToAddress() {
- return toAddress;
- }
- publicvoid setToAddress(String toAddress) {
- this.toAddress = toAddress;
- }
- public String getUserName() {
- return userName;
- }
- publicvoid setUserName(String userName) {
- this.userName = userName;
- }
- public String getSubject() {
- return subject;
- }
- publicvoid setSubject(String subject) {
- this.subject = subject;
- }
- public String getContent() {
- return content;
- }
- publicvoid setContent(String textContent) {
- this.content = textContent;
- }
- }
3.建立一個驗證器
- import javax.mail.Authenticator;
- import javax.mail.PasswordAuthentication;
- /**
- * 郵件使用者名稱和密碼認證器
- */
- publicclass 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() {
- returnnew PasswordAuthentication(userName, password);
- }
- }
- privatevoid email(HttpSession session, String email) {
- // 設定郵件伺服器資訊
- MailSenderInfo mailInfo = new MailSenderInfo();
- mailInfo.setMailServerHost("smtp-mail.outlook.com");// 傳送郵件的伺服器的IP(或主機地址)
- mailInfo.setMailServerPort("587");//有些埠在伺服器上是沒開放的 這裡需要注意下
- mailInfo.setValidate(true);
- // 郵箱使用者名稱(根據自己情況設定) 這裡可以多弄幾個郵箱過來 避免郵箱賬號需要驗證 或者被當成垃圾郵件封號 A失敗就用B
- mailInfo.setUserName("此處填寫跟上面傳送郵件伺服器對應的郵箱");
- // 郵箱密碼(根據自己情況設定)
- mailInfo.setPassword("這是你的密碼");
- // 發件人郵箱(根據自己情況設定,如果你沒對郵箱進行特別設定,應該和郵箱使用者名稱一致)
- mailInfo.setFromAddress("這裡跟上面一樣");
- // 收件人郵箱(根據自己情況設定)
- mailInfo.setToAddress(email);
- // 郵件標題
- mailInfo.setSubject("我是標題");
- // 郵件內容
- mailInfo.setContent("我是內容,正經的內容不是垃圾郵箱");