📄 postmail.java
字号:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class for Servlet: postMail
*
*/
public class postMail extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
/*String mailform=""; String mailto="";String mailsubject="";String mailcontent="";*/
String host="smtp.163.com";
String user="yiyi7817";
String password="841209";
String tto="";
String ttitle="";
String tcontent="";
String sender="yiyi7817@163.com";
public void setHost(String host)
{
this.host=host;
}
public void setAccount(String user,String password)
{
this.user=user;
this.password=password;
}
public String getUser()
{
return user;
}
public String getPassword()
{
return password;
}
public String getHost()
{
return host;
}
public String getSender()
{
return sender;
}
public void setSender(String sender)
{
this.sender = sender;
}
public postMail() {
host="smtp.163.com";
user="yiyi7817";
password="841209";
}
public void setTitle(String title)
{
this.ttitle=title;
}
public void setReceiver(String receiver)
{
this.tto=receiver;
}
public void setContent(String content)
{
this.tcontent=content;
}
public String getTto()
{
return tto;
}
public String getTtitle()
{
return ttitle;
}
public String getTcontent()
{
return tcontent;
}
public void sendMail()
{
try{
Properties props=new Properties();
props.put("mail.smtp.host",this.getHost());
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);
MimeMessage message=new MimeMessage(s);
InternetAddress from=new InternetAddress(this.getSender());
message.setFrom(from);
InternetAddress to=new InternetAddress(this.getTto());
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(this.getTtitle());
message.setSentDate(new Date());
BodyPart mdp=new MimeBodyPart();
mdp.setContent(this.getTcontent(),"text/html;charset=gb2312");
Multipart mm=new MimeMultipart();
mm.addBodyPart(mdp);
message.setContent(mm);
message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect(this.getHost(),this.getUser(),this.getPassword());
transport.sendMessage(message,message.getAllRecipients());
transport.close();
}catch(MessagingException e){
}
}
public void doSend(String receiver,String tt,String ct)
{
String rece="",content="",title="";
rece=receiver;
title=tt;
content=ct;
this.setReceiver(rece);
this.setTitle(title);
this.setContent(content);
this.sendMail();
}
public void setWelcome(String name,String password)
{
String content="",title="";
title="Welcome to Create Account in Star Hotel Reservation System";
content="Hello, "+name+" . This is a welcome mail.<br /> Please Remember these information: username: "+name+" , password: "+password+" (hashed). <br />";
content=content.concat("");
this.setTitle(title);
this.setContent(content);
}
public void setUpdate(String password)
{
String content="",title="";
title="Notice for Update Information in Star Hotel Reservation System";
content="Hello. This is a confirm mail.<br /> Please Remember these information: Your password: "+password+" (hashed). <br />";
content=content.concat("");
this.setTitle(title);
this.setContent(content);
}
public void setConfirm(String name,String lk)
{
String content="",title="",link=lk;
title="Confirm mail from Star Hotel Reservation System";
content="Hello, "+name+" . This is an email confirm process. Please view this link to complete your submition: ";
content=content.concat(link);
this.setTitle(title);
this.setContent(content);
}
public void setReservation(String name,String id)
{
String content="",title="",ID=id;
title="Reservation Notice from Star Hotel Reservation System";
content="Hello, "+name+" . This is your reservation id:"+id+". Please save this id and you can track or cancel your reservation online.";
this.setTitle(title);
this.setContent(content);
}
public void setFindout(String fname,String lname,String uname,String lk)
{
String content="",title="",link=lk;
String name=fname+" "+lname;
String username=uname;
title="Find out Password from Star Hotel Reservation System";
content="Hello, "+name+" . This is an Password renew process.This is your username: "+username+". Then view this link to complete process. "+link;
this.setTitle(title);
this.setContent(content);
}
public void setCancel(String ID,String name)
{
String content="";
String title="Reservation Cancel from Star Hotel Reservation System";
content="This is a reservation cancel notice. Your Reservation which number is "+ID+" has been canceled by "+name+".";
this.setTitle(title);
this.setContent(content);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession(true);
String rece="",content="",title="";
rece=request.getParameter("receiver");
content=request.getParameter("content");
title=request.getParameter("title");
this.setReceiver(rece);
this.setTitle(title);
this.setContent(content);
if (this.getTtitle().equals(null))
{
this.setTitle("Hello!");
}
else if (this.getSender().equals(null))
{
this.setSender("yiyi7817@163.com");
}
else if(this.getTcontent().equals(null))
{
this.setContent("Just is a Test!");
}
else ;
this.sendMail();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -