Java to send the message of the concrete implementation

  • 2020-04-01 02:21:01
  • OfStack

The first is a concrete class that inherits from javax.mail.authenticator. GetPasswordAuthentication () method is to build a PasswordAuthentication object and returns, confusing JAVA Mail such design intent, may javax.mail. Mail. The Authenticator provides us with the additional security verification measures.


package com.mzule.simplemail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class MailAuthenticator extends Authenticator {

private String username;

private String 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 above, you can build a factory class, which can encapsulate the creation process, so it is very convenient to get the mailbox user name and password by reading the configuration file. The following code, which I wrote when I was writing observer mode, simply demonstrates the factory class.


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

public class MailSenderFactory {

private static SimpleMailSender serviceSms = null;

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 an email, or the code in the observer mode DEMO, whoop.


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 E-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 changes ", " Items of your concern "
+ product.getName() + " Reduced the price, by "
+ product.getPrice() + " Yuan to " + price + " Yuan, a drop of up to "
+ decrease + " RMB yuan. Go shopping. ");
}
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
}


Related articles: