java sendemail_Java Send email

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值