Sometimes we want to send an email out when catch an exception. We need to use javax.mail jar to help us to do that. Actually we have several ways to do that. Here is to introduce the easiest way without authentication.
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.stereotype.Component;
import twitter4j.internal.logging.Logger;
@Component("emailSender")
public class EmailSender {
private static Logger _logger = Logger.getLogger(EmailSender.class);
public static Date lastEmailSendingDate = new Date();
private static long count = 0;
private Calendar calendar = Calendar.getInstance();
private String mailSmtpHost;
private String mailTo;
public String getMailSmtpHost() {
return mailSmtpHost;
}
public void setMailSmtpHost(String mailSmtpHost) {
this.mailSmtpHost = mailSmtpHost;
}
public String getMailTo() {
return mailTo;
}
public void setMailTo(String mailTo) {
this.mailTo = mailTo;
}
public void sendEmail(String mailFrom, String mailSubject, String mailText) {
if (getMailSendingFlag()) {
Properties properties = new Properties();
properties.put("mail.smtp.host", mailSmtpHost);
Session emailSession = Session.getDefaultInstance(properties);
Message emailMessage = new MimeMessage(emailSession);
try {
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
emailMessage.setFrom(new InternetAddress(mailFrom));
emailMessage.setSubject(mailSubject);
emailMessage.setText(mailText);
Transport.send(emailMessage);
lastEmailSendingDate = new Date();
count++;
} catch (AddressException e) {
_logger.error("EmailSender, AddressException: " + e.getMessage());
} catch (MessagingException e) {
_logger.error("EmailSender, MessagingException: " + e.getMessage());
}
}
}
private boolean getMailSendingFlag() {
_logger.debug("getMailSendingFlag() - start!");
calendar.setTime(lastEmailSendingDate);
long timeend = calendar.getTimeInMillis();
calendar.setTime(new Date());
long timethis = calendar.getTimeInMillis();
long interval = Long.parseLong(TwitterUtil.getPropertyValueByKey("mailSender.interval"));
boolean flag = ((timethis - timeend - interval) > 0 ? true : false) || (count == 0);
_logger.debug("getMailSendingFlag() - end!");
return flag;
}
}This class has two functions:
1. sendMail() is used to send email out without authentication, but you need to pass mailFrom, subject and text. mailto and server are passed from command line when we use command line to start the app.
2. private method getMailSendingFlag is used to do mail control. We don't want to receive email continuously without any stop. I use a timestamp to do that. Only 30 minutes to send an email out.
Sometimes we want to send an email out when catch an exception. We need to use javax.mail jar to help us to do that. Actually we have several ways to do that. Here is to introduce