First is a concrete class that inherits from Javax.mail.Authenticator. The Getpasswordauthentication () method is to build a Passwordauthentication object and return, a little convoluted design intent such as Java Mail, It may be that Javax.mail.Authenticator has provided us with additional security verification measures.
| code as follows |
|
|
Package com.mzule.simplemail;
Import Javax.mail.Authenticator;
Import javax.mail.PasswordAuthentication;
/**
* Server mailbox logon Authentication
*
* @author mzule
*
*/
public class Mailauthenticator extends authenticator {
/**
* Username (logon mailbox)
*/
private string Username
/**
* Password
*/
private string password; p>/**
* Initialize mailboxes and Passwords
*
* @param username Mailboxes
* @param password Password
*/
Public mailauthenticator (String u Sername, 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;
}
} |
Call the mailbox sender above, you can build a factory class, the factory class can encapsulate the creation process, so read the configuration file to get the mailbox username, the password will become very convenient. The following code was written while I was writing the observer pattern, just a simple demonstration of the factory class.
| The code is as follows |
|
|
Package com.mzule.dp.observer.factory;
Import Com.mzule.dp.observer.constant.MailSenderType;
Import Com.mzule.simplemail.SimpleMailSender;
/**
* Outbox Factory
*
* @author Mzule
*
*/
public class Mailsenderfactory {
/**
* Service Email
*/
private static Simplemailsender servicesms = null;
/**
* Get Mailbox
*
* @param type Mailbox types
* @return Type-compliant mailboxes
*/
public static Simplemailsender Getsender (Mailsendertype type) {
if (type = = Mailsendertype.service) {
if (servicesms = = null) {
servicesms = new Simplemailsender ("invisible@126.com",
"Hidden");
}
return servicesms;
}
return null;
}
} |
Send the message, or the Observer mode demo inside the code, shout.
| code as follows |
|
|
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) {
//Send mail
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, price change, "items you care about"
+ product.getname () + "drop Prices, by "
+ product.getprice () +" yuan to "+ Price +" Yuan, the decline of "
+ decrease +" RMB. Hurry up and go shopping. ");
}
catch (Addressexception e) {
E.printstacktrace ();
} catch (Messagingexception e) {
E.printstacktr Ace ();
}
}
}
} |