java發送郵件的具體實現_java

來源:互聯網
上載者:User

首先是繼承自javax.mail.Authenticator的一個具體類。getPasswordAuthentication()方法也就是構建一個PasswordAuthentication對象並返回,有點費解JAVA Mail這樣的設計意圖,可能javax.mail.Authenticator為我們提供了附加的保證安全的驗證措施吧。

複製代碼 代碼如下:

package com.mzule.simplemail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
* 伺服器郵箱登入驗證
*
* @author MZULE
*
*/
public class MailAuthenticator extends Authenticator {

/**
* 使用者名稱(登入郵箱)
*/
private String username;
/**
* 密碼
*/
private String password;

/**
* 初始化郵箱和密碼
*
* @param username 郵箱
* @param password 密碼
*/
public MailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}

String getPassword() {
return password;
}

@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}

String getUsername() {
return username;
}

public void setPassword(String password) {
this.password = password;
}

public void setUsername(String username) {
this.username = username;
}

}

  調用上面的郵箱發送器,可以構建一個工廠類,工廠類可以封裝建立的過程,所以通過讀設定檔擷取信箱使用者名,密碼都會變得十分方便。下面的代碼是我在寫觀察者模式的時候寫的,只是簡單示範了工廠類。

複製代碼 代碼如下:

package com.mzule.dp.observer.factory;

import com.mzule.dp.observer.constant.MailSenderType;
import com.mzule.simplemail.SimpleMailSender;

/**
* 寄件匣工廠
*
* @author MZULE
*
*/
public class MailSenderFactory {

/**
* 服務郵箱
*/
private static SimpleMailSender serviceSms = null;

/**
* 擷取郵箱
*
* @param type 郵箱類型
* @return 符合類型的郵箱
*/
public static SimpleMailSender getSender(MailSenderType type) {
if (type == MailSenderType.SERVICE) {
if (serviceSms == null) {
serviceSms = new SimpleMailSender("invisible@126.com",
"hidden");
}
return serviceSms;
}
return null;
}

}

  發送郵件,還是觀察者模式DEMO裡面的代碼,呼呼。

複製代碼 代碼如下:

package com.mzule.dp.observer.observer;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import com.mzule.dp.observer.constant.MailSenderType;
import com.mzule.dp.observer.factory.MailSenderFactory;
import com.mzule.dp.observer.po.Product;
import com.mzule.simplemail.SimpleMailSender;

public class ProductPriceObserver implements Observer {

@Override
public void update(Observable obj, Object arg) {
Product product = null;
if (obj instanceof Product) {
product = (Product) obj;
}
if (arg instanceof Float) {
Float price = (Float) arg;
Float decrease = product.getPrice() - price;
if (decrease > 0) {
// 發送郵件
SimpleMailSender sms = MailSenderFactory
.getSender(MailSenderType.SERVICE);
List<String> recipients = new ArrayList<String>();
recipients.add("invisible@qq.com");
recipients.add("invisible@gmail.com");
try {
for (String recipient : recipients) {
sms.send(recipient, "價格變動", "您關注的物品"
+ product.getName() + "降價了,由"
+ product.getPrice() + "元降到" + price + "元,降幅達"
+ decrease + "元人民幣。趕快購物吧。");
}
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.