The concrete implementation of the Java send mail _java

Source: Internet
Author: User
Tags constant

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 ();
}
}
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.