发送Email

package com.lot.common.servlet;


import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class SendEmail extends HttpServlet {
private static final Log logger = LogFactory.getLog(MailApply.class);


/**
* Constructor of the object.
*/
public SendEmail() {
super();
}


/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/**
* The doGet method of the servlet. <br>

* This method is called when a form has its tag value method equals to get.

* @param request
*            the request send by the client to the server
* @param response
*            the response send by the server to the client
* @throws ServletException
*             if an error occurred
* @throws IOException
*             if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
// 获得浏览器信息
String Agent = request.getHeader("User-agent");
logger.info("浏览器版本号、操作系统类型:" + Agent);
// 邮件标题
String title = request.getParameter("title");
// 邮件内容
String contanct = request.getParameter("contanct");
// 收信箱
String inbox = request.getParameter("inbox");
logger.info("邮件标题:" + title + ";内容:" + contanct + ";收信箱:" + inbox);
JSONObject jsonObj = new JSONObject();
// 判断商户信息是否为空
if (contanct == null || contanct.equals("")) {
jsonObj.put("ok", false);
out.println(jsonObj.toString());
out.flush();
out.close();
return;
}
// 创建一个线程,调用发送邮件方法,主程序继续执行
Send send = new Send(title, contanct, inbox);
Thread th1 = new Thread(send);
th1.start();
jsonObj.put("ok", true);
out.println(jsonObj.toString());
out.flush();
out.close();
}


/**
* The doPost method of the servlet. <br>

* This method is called when a form has its tag value method equals to
* post.

* @param request
*            the request send by the client to the server
* @param response
*            the response send by the server to the client
* @throws ServletException
*             if an error occurred
* @throws IOException
*             if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}


/**
* Initialization of the servlet. <br>

* @throws ServletException
*             if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}


}


package com.lot.common.servlet;


import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class SendEmail extends HttpServlet {
private static final Log logger = LogFactory.getLog(MailApply.class);


/**
* Constructor of the object.
*/
public SendEmail() {
super();
}


/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/**
* The doGet method of the servlet. <br>

* This method is called when a form has its tag value method equals to get.

* @param request
*            the request send by the client to the server
* @param response
*            the response send by the server to the client
* @throws ServletException
*             if an error occurred
* @throws IOException
*             if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
// 获得浏览器信息
String Agent = request.getHeader("User-agent");
logger.info("浏览器版本号、操作系统类型:" + Agent);
// 邮件标题
String title = request.getParameter("title");
// 邮件内容
String contanct = request.getParameter("contanct");
// 收信箱
String inbox = request.getParameter("inbox");
logger.info("邮件标题:" + title + ";内容:" + contanct + ";收信箱:" + inbox);
JSONObject jsonObj = new JSONObject();
// 判断商户信息是否为空
if (contanct == null || contanct.equals("")) {
jsonObj.put("ok", false);
out.println(jsonObj.toString());
out.flush();
out.close();
return;
}
// 创建一个线程,调用发送邮件方法,主程序继续执行
Send send = new Send(title, contanct, inbox);
Thread th1 = new Thread(send);
th1.start();
jsonObj.put("ok", true);
out.println(jsonObj.toString());
out.flush();
out.close();
}


/**
* The doPost method of the servlet. <br>

* This method is called when a form has its tag value method equals to
* post.

* @param request
*            the request send by the client to the server
* @param response
*            the response send by the server to the client
* @throws ServletException
*             if an error occurred
* @throws IOException
*             if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}


/**
* Initialization of the servlet. <br>

* @throws ServletException
*             if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}

/***************************************结束*****************************************/

package com.lot.common.servlet;


import java.util.Date;
import java.util.Properties;


import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class Send implements Runnable {
private static final Log logger = LogFactory.getLog(Send.class);
private String title; // 邮件标题
private String contanct; // 邮件内容
private String inbox; // 收信箱
public Send(String contanct) {
this.contanct = contanct;
this.title = "活动/报名";
this.inbox = "info@gleepay.com";
}


/**

* @param title
*            标题
* @param contanct
*            内容
* @param inbox
*            收信箱
*/
public Send(String title, String contanct, String inbox) {
this.title = title;
this.contanct = contanct;
this.inbox = inbox;
}


/**
* 发送邮件
*/
public void run() {
// TODO Auto-generated method stub
String str = new String(contanct);
Properties props = new Properties();
// 设置smtp服务器
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.auth", "true");
// 验证邮箱是否可以发送邮件
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("gleepay@gmail.com",
"dingfu123");
}
});
MimeMessage msg = new MimeMessage(session);
MimeMultipart mp = new MimeMultipart();
MimeBodyPart body = new MimeBodyPart();
try {
msg.setFrom(new InternetAddress(inbox));
// info@gleepay.com
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(
inbox));
body.setContent(str, "text/plain;charset=gb2312");// 设置编码
mp.addBodyPart(body);
msg.setContent(mp);
msg.setSubject(title, "UTF-8");
msg.setSentDate(new Date());
msg.saveChanges();
Transport.send(msg);
logger.info("邮件“" + title + "”发送成功,邮件信息:" + contanct);
} catch (AddressException e) {
logger.error("邮件“" + title + "”发送失败,邮件信息:" + contanct);
logger.error(e);
e.printStackTrace();
} catch (MessagingException e) {
logger.error("邮件“" + title + "”发送失败,邮件信息:" + contanct);
logger.error(e);
e.printStackTrace();
} catch (Exception e) {
logger.error("邮件“" + title + "”发送失败,邮件信息:" + contanct);
logger.error(e);
e.printStackTrace();
}
}
}

/***********************************************结束*********************************************************************/

$.ajax({
  type: 'POST',
  url:"../../mailapply",
  data:{"contanct":rtcontanct},
  async:false,
  success: function(data){
var jsonObj=eval("("+data+")");
if(!jsonObj.ok){
alert("发送失败,请重新提交!或请选择其他方式报名!");
}else{
names+=cn;
  $("#mailContent").val("");
  rtcontanct = "";
alert(cn+":您好!恭喜您已经报名成功!我们会在三个工作日内与您联系,感谢您的参加!");
$("#guest_Name").val('');
$("#guest_Phone").val('');
$("#guest_Email").val('');
$("#guest_Post").val('');
}
}
});

/*****************************************************************结束**************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值