The first is a specific class inherited from javax. mail. Authenticator. The getPasswordAuthentication () method is to construct a PasswordAuthentication object and return it. It is a bit confusing with the design intent such as JAVA Mail, which may be javax. mail. authenticator provides us with additional security verification measures.
The Code is as follows: |
|
Package com. mzule. simplemail; Import javax. mail. Authenticator; Import javax. mail. PasswordAuthentication; /** * Server email login verification * * @ Author MZULE * */ Public class MailAuthenticator extends Authenticator { /** * User Name (logon email) */ Private String username; /** * Password */ Private String password; /** * Initialize the email address and password. * * @ Param username: Email * @ 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; } } |
By calling the mailbox sender, you can build a factory class. The factory class can encapsulate the creation process. Therefore, it is very convenient to read the configuration file to obtain the mailbox user name and password. The following code is written when I write the observer mode. It just demonstrates 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; /** * Producer Factory * * @ Author MZULE * */ Public class MailSenderFactory { /** * Service mailbox */ Private static SimpleMailSender serviceSms = null; /** * Get email * * @ Param type: Mailbox type * @ Return: a mailbox of the matching type */ Public static SimpleMailSender getSender (MailSenderType ){ If (type = MailSenderType. SERVICE ){ If (serviceSms = null ){ ServiceSms = new SimpleMailSender ("invisible@126.com ", "Hidden "); } Return serviceSms; } Return null; } } |
Send an email or call the code in the Observer mode DEMO.
The Code is 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 an email 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", "item of interest" + Product. getName () + "reduced price," + Product. getPrice () + "RMB reduced to" + price + "RMB, down" + Decrease + "RMB. Buy now. "); } } Catch (AddressException e ){ E. printStackTrace (); } Catch (MessagingException e ){ E. printStackTrace (); } } } } } |