java邮件开发(转)
2013-10-30 11:53
357 查看
原文地址http://blog.csdn.net/cyh1111/article/details/4349200
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";
}
}
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";
}
}
相关文章推荐
- java 中的 加密
- java 中 路径的获取 (转)
- java 正则式
- Eclipse 设置(转)
- java xml文件操作(转)
- java 获取 系统时间 Calendar
- 读取数据库中的数据到xml java
- java.lang.OutOfMemoryError处理错误
- Eclipse 插件开发
- java 邮件开发(转)
- 使用 Eclipse 开发 J2EE 应用(转)
- Java调用WMIC命令的方法阻塞
- Java调用WMIC命令的方法阻塞(转)
- java 窗体,未测试
- java 文件压缩
- java 实现指定文件的复制和 拷贝
- java 文件夹创建(创)--测试成功
- java file中 mkdir()与 mkdirs()方…
- java中判断字符串是否为数字的三种方法
- JAVA操作properties文件