The 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, Perhaps Javax.mail.Authenticator provides us with additional security assurance measures.
Copy Code code as follows:
Package com.mzule.simplemail;
Import Javax.mail.Authenticator;
Import javax.mail.PasswordAuthentication;
/**
* Server mailbox Logon Verification
*
* @author Mzule
*
*/
public class Mailauthenticator extends authenticator {
/**
* User name (login mailbox)
*/
Private String username;
/**
* Password
*/
private String password;
/**
* Initialization of mailboxes and passwords
*
* @param username Mailbox
* @param password 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;
}
}
Call the above mailbox sender, you can build a factory class, the factory class can encapsulate the creation process, so read the configuration file to get the mailbox username, 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.
Copy Code code 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.
Copy Code 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 of interest"
+ product.getname () + "lower price, by"
+ product.getprice () + "yuan to" + Price + "yuan, drop up"
+ Decrease + "yuan yuan." Hurry up and go shopping. ");
}
catch (Addressexception e) {
E.printstacktrace ();
catch (Messagingexception e) {
E.printstacktrace ();
}
}
}
}
}