Java发送邮件(将系统的异常信息发送到邮箱中)
2009-07-15 08:45
309 查看
1. 首先建立一个简单的邮件Model
package com.itmg.tool.email;
import java.io.Serializable;
public class MailModel implements Serializable {
private static final long serialVersionUID = 1L;
private String subjectText;
private String bodyText;
private String[] receiver;
private String content_format = "text/html; charset=UTF-8";
private boolean isdebug = false;
public MailModel() {
// TODO Auto-generated constructor stub
}
public String getBodyText() {
return bodyText;
}
public void setBodyText(String bodyText) {
this.bodyText = bodyText;
}
public String[] getReceiver() {
return receiver;
}
public void setReceiver(String[] receiver) {
this.receiver = receiver;
}
public String getSubjectText() {
return subjectText;
}
public void setSubjectText(String subjectText) {
this.subjectText = subjectText;
}
public String getContent_format() {
return content_format;
}
public void setContent_format(String content_format) {
this.content_format = content_format;
}
public boolean isIsdebug() {
return isdebug;
}
public void setIsdebug(boolean isdebug) {
this.isdebug = isdebug;
}
}
2. 认证
package com.itmg.tool.email;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username, password);
}
}
3. 建立发邮件的类
package com.itmg.tool.email;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import com.itmg.db.TransServer;
public class SendMail extends Thread{
private static Logger log = Logger.getLogger(SendMail.class.getName());
private static final String SMTP_HOST_NAME = "smtp.gmail.com"; //发邮件的服务器
private static final String emailFromAddress = "xxx@gmail.com";
private static final String emailFromAddressPassword = "xxxxx";
private static final String emailTitle = "Exception";//email 的标题名字
private MailModel mailModel;
public SendMail(MailModel mailModel) {
// TODO Auto-generated constructor stub
this.mailModel = mailModel;
}
public void sendMessage() throws MessagingException{
String recipients[] = mailModel.getReceiver();
String subject = mailModel.getSubjectText();
String message = mailModel.getBodyText();
boolean isdebug = mailModel.isIsdebug();
Properties props = System.getProperties();
Session sendMailSession;
props.put("mail.smtp.auth", "true");
props.put("mail.debug", isdebug);
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.user", emailFromAddress);
props.put("mail.smtp.password", emailFromAddressPassword);
PopupAuthenticator popA = new PopupAuthenticator();
PasswordAuthentication pop = popA.performCheck(emailFromAddress, emailFromAddressPassword);
sendMailSession = Session.getInstance(props, popA);
MimeMessage mimeMessage = new MimeMessage(sendMailSession);
try {
mimeMessage.setFrom(new InternetAddress(emailFromAddress, emailTitle,
"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
mimeMessage.setRecipients(Message.RecipientType.TO, addressTo);
mimeMessage.setSubject(subject, "UTF-8");
mimeMessage.setContent(message,
mailModel.getContent_format());
mimeMessage.setHeader("Content-Transfer-Encoding", "7bit");
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage);
log.info("========>the email has been sent out!");
}
public void run()
{
try {
sendMessage();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
4. 负责发邮件的Action
package com.itmg.search.action;
import com.itmg.search.action.base.SearchBaseAction;
import com.itmg.tool.email.MailModel;
import com.itmg.tool.email.SendMail;
import com.itmg.util.TransServerUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
public class SendMailAction extends SearchBaseAction{
private static final long serialVersionUID = 1L;
public String execute() throws Exception {
ValueStack valueStack = ActionContext.getContext().getValueStack();
String message = (String)valueStack.findValue("exceptionStack");
MailModel mailModel = new MailModel();
StringBuffer sb = new StringBuffer();
sb.append("RemoteHost"+request.getRemoteHost()+"/r/n");
sb.append("RemoteAddr"+request.getRemoteAddr()+"/r/n");
sb.append("User-Agent:"+request.getHeader("User-Agent")+"/r/n");
sb.append("RefererString:"+request.getHeader("referer")+"/r/n");
sb.append(message);
mailModel.setBodyText(sb.toString());
mailModel.setSubjectText("The API.com server error notice");
mailModel.setReceiver(new String[]{TransServerUtil.ToMeEmail});
mailModel.setContent_format("text/plain;charset=utf-8");
SendMail emailSend = new SendMail(mailModel);
Thread emailThread = new Thread(emailSend);
emailThread.start();
return "index";
}
}
package com.itmg.tool.email;
import java.io.Serializable;
public class MailModel implements Serializable {
private static final long serialVersionUID = 1L;
private String subjectText;
private String bodyText;
private String[] receiver;
private String content_format = "text/html; charset=UTF-8";
private boolean isdebug = false;
public MailModel() {
// TODO Auto-generated constructor stub
}
public String getBodyText() {
return bodyText;
}
public void setBodyText(String bodyText) {
this.bodyText = bodyText;
}
public String[] getReceiver() {
return receiver;
}
public void setReceiver(String[] receiver) {
this.receiver = receiver;
}
public String getSubjectText() {
return subjectText;
}
public void setSubjectText(String subjectText) {
this.subjectText = subjectText;
}
public String getContent_format() {
return content_format;
}
public void setContent_format(String content_format) {
this.content_format = content_format;
}
public boolean isIsdebug() {
return isdebug;
}
public void setIsdebug(boolean isdebug) {
this.isdebug = isdebug;
}
}
2. 认证
package com.itmg.tool.email;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username, password);
}
}
3. 建立发邮件的类
package com.itmg.tool.email;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import com.itmg.db.TransServer;
public class SendMail extends Thread{
private static Logger log = Logger.getLogger(SendMail.class.getName());
private static final String SMTP_HOST_NAME = "smtp.gmail.com"; //发邮件的服务器
private static final String emailFromAddress = "xxx@gmail.com";
private static final String emailFromAddressPassword = "xxxxx";
private static final String emailTitle = "Exception";//email 的标题名字
private MailModel mailModel;
public SendMail(MailModel mailModel) {
// TODO Auto-generated constructor stub
this.mailModel = mailModel;
}
public void sendMessage() throws MessagingException{
String recipients[] = mailModel.getReceiver();
String subject = mailModel.getSubjectText();
String message = mailModel.getBodyText();
boolean isdebug = mailModel.isIsdebug();
Properties props = System.getProperties();
Session sendMailSession;
props.put("mail.smtp.auth", "true");
props.put("mail.debug", isdebug);
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.user", emailFromAddress);
props.put("mail.smtp.password", emailFromAddressPassword);
PopupAuthenticator popA = new PopupAuthenticator();
PasswordAuthentication pop = popA.performCheck(emailFromAddress, emailFromAddressPassword);
sendMailSession = Session.getInstance(props, popA);
MimeMessage mimeMessage = new MimeMessage(sendMailSession);
try {
mimeMessage.setFrom(new InternetAddress(emailFromAddress, emailTitle,
"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
mimeMessage.setRecipients(Message.RecipientType.TO, addressTo);
mimeMessage.setSubject(subject, "UTF-8");
mimeMessage.setContent(message,
mailModel.getContent_format());
mimeMessage.setHeader("Content-Transfer-Encoding", "7bit");
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage);
log.info("========>the email has been sent out!");
}
public void run()
{
try {
sendMessage();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
4. 负责发邮件的Action
package com.itmg.search.action;
import com.itmg.search.action.base.SearchBaseAction;
import com.itmg.tool.email.MailModel;
import com.itmg.tool.email.SendMail;
import com.itmg.util.TransServerUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
public class SendMailAction extends SearchBaseAction{
private static final long serialVersionUID = 1L;
public String execute() throws Exception {
ValueStack valueStack = ActionContext.getContext().getValueStack();
String message = (String)valueStack.findValue("exceptionStack");
MailModel mailModel = new MailModel();
StringBuffer sb = new StringBuffer();
sb.append("RemoteHost"+request.getRemoteHost()+"/r/n");
sb.append("RemoteAddr"+request.getRemoteAddr()+"/r/n");
sb.append("User-Agent:"+request.getHeader("User-Agent")+"/r/n");
sb.append("RefererString:"+request.getHeader("referer")+"/r/n");
sb.append(message);
mailModel.setBodyText(sb.toString());
mailModel.setSubjectText("The API.com server error notice");
mailModel.setReceiver(new String[]{TransServerUtil.ToMeEmail});
mailModel.setContent_format("text/plain;charset=utf-8");
SendMail emailSend = new SendMail(mailModel);
Thread emailThread = new Thread(emailSend);
emailThread.start();
return "index";
}
}
相关文章推荐
- 提交订单系统自动发送邮件到收件人邮箱优化(邮件内容信息以月为单位)
- 提交订单系统自动发送邮件到收件人邮箱优化(邮件内容信息以月为单位)
- 提交订单系统自动发送邮件到收件人邮箱优化(邮件内容信息以月为单位)
- 提交订单系统自动发送邮件到收件人邮箱优化(邮件内容信息以月为单位)
- Android使用UncaughtExceptionHandler捕获全局异常 android获取手机信息大全Java追加文件内容使用JavaMail发送邮件OkHttp获取系统时间
- 提交订单系统自动发送邮件到收件人邮箱优化(邮件内容信息以月为单位)
- 提交订单系统自动发送邮件到收件人邮箱优化(邮件内容信息以月为单位)
- 提交订单系统自动发送邮件到收件人邮箱优化(邮件内容信息以月为单位)
- 提交订单系统自动发送邮件到收件人邮箱优化(邮件内容信息以月为单位)
- 提交订单系统自动发送邮件到收件人邮箱优化(邮件内容信息以月为单位)
- java实现系统捕获异常发送邮件案例
- 提交订单系统自动发送邮件到收件人邮箱优化(邮件正文内容为当天数据,附件内容为当月数据)
- Java实现通过smtp服务器向指定邮箱发送邮件
- android 调用系统邮件 发送邮件到指定邮箱
- JAVA邮箱发送信息
- 在小型OA系统中使用Java发送邮件
- python 检测文件,有异常信息就发送邮件
- yii2使用邮箱组件发送邮件信息 Sorry, we are unable to reset password for email provided
- 使用JAVA模拟发送邮件到邮箱
- Android 调用系统的邮箱app发送邮件