怎样实现用JSP来发送邮件

北大青鸟大学城校区logo 北大青鸟大学城校区
招生简章校园环境师资力量就业明星招生问答软件工程师北京大学学历学员项目联系我们 报名通道

免费在线咨询通道>>

免费在线报名通道>>

北大青鸟报名电话
当前位置:北大青鸟 > 软件教程 > web前端开发 >

怎样实现用JSP来发送邮件

标签:   分类:web前端开发


 们每次发信都要先进入邮箱,然后才能写信.突然一天我的邮箱密码丢了,我进不了邮件了,那可不可以不进邮箱就能给朋友发信呢?现在我告诉你:这是可以的!

  此程序的功能就在此,你可以把它用在论坛上,打开这个程序窗口发送邮件,你也可以把它放在你的桌面上,随心所欲地发送邮件.当然作为一个程序员,你还可以把它拿过来分析一下,看有没有可学知识.

  由于发送HTML类型邮件的设计思想和此类似,所以不在一一列出.程序源码则分别列出.

  一.流程思路:
   

  二.程序源码:

  (1) 发送文本类型信件.这种是接收者收到后打开信箱后直接显示在邮件正文的.它主要有两个程序: mailform.html and sendmail.jsp,另外三个程序为提交成功和不成功及发生内部错误的处理程序.

  mailform.html用来输入用户信息,然后提交表单给sendmail.jsp

  sendmail.jsp 用来处理接收信息并处理信息,最后发送邮件.

  <%--mailform.html 的源代码--%>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
  <head>
  <title>mailform.html</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
  <form name="form1" method="post" action="sendmail.jsp">
  <p align="center">填写邮件内容</p>
  <div align="center">
  <table width="75%" border="1">
  <tr>
  <td><div align="center">收件人</div></td>
  <td><div align="center">
  <input name="to" type="text" id="to" size="30" maxlength="30">
  </div></td>
  </tr>
  <tr>
  <td><div align="center">发件人</div></td>
  <td><div align="center">
  <input name="from" type="text" id="from" size="30" maxlength="30">
  </div></td>
  </tr>
  <tr>
  <td><div align="center">抄送</div></td>
  <td><div align="center">
  <input name="cc" type="text" id="cc" size="30" maxlength="100">
  </div></td>
  </tr>
  <tr>
  <td><div align="center">暗送</div></td>
  <td><div align="center">
  <input name="bcc" type="text" id="bcc" size="30" maxlength="100">
  </div></td>
  </tr>
  <tr>
  <td><div align="center">主题</div></td>
  <td><div align="center">
  <input name="subject" type="text" id="subject" size="30" maxlength="30">
  </div></td>
  </tr>
  <tr>
  <td colspan="2"><div align="center">
  <textarea name="body" cols="40" rows="10" id="body"></textarea>
  </div></td>
  </tr>
  <tr>
  <td colspan="2"><div align="center">
  <input type="submit" name="Submit" value="发送">
  &nbsp;&nbsp;&nbsp;&nbsp;
  <input name="Reset" type="submit" id="Reset" value="清空">
  </div></td>
  </tr>
  </table>
  </div>
  <p align="center">&nbsp;</p>
  </form>
  <p align="center">&nbsp;</p>
  </body>
  </html>

  <%--sendmail.jsp 的源代码--%>
  <%@page contentType="text/html;charset=gb2312"%>
  <%@page errorPage="errpage.jsp"%>
  <%@page import="java.util.*"%>
  <%@page import="javax.mail.*"%>
  <%@page import="javax.mail.internet.*"%>
  <%@page import="javax.activation.*"%>
  <html>
  <head>
  <title>sendmail.jsp</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
  <%
  //邮件服务器的IP地址,注意此时使用的是SMTP 邮件服务器
  //String host="202.102.240.76";
  //获取用户信息
  String to=request.getParameter("to");
  String from=request.getParameter("from");
  String cc=request.getParameter("cc");
  String bcc=request.getParameter("bcc");
  //结合本程序的第一句,共同起显示汉字体的功能
  String subject=request.getParameter("subject");
  subject = new java.lang.String(subject.getBytes("iso-8859-1"));
  //以上方法等同于以下两句,原理相同
  //byte[] subjectTemp=request.getParameter("subject").getBytes("ISO8859_1");
  //String subject=new String(subjectTemp);
  byte[] messageTextTemp=request.getParameter("body").getBytes("ISO8859_1");
  String messageText= new String(messageTextTemp);
  boolean sessionDebug = false;
  //验证所填写的邮件是否正确
  if((to.trim().indexOf("@")==-1)||(to.trim().length()<5)){
  %>
  <jsp:forward page="err.jsp"/>
  <%
  }
  if((from.trim().indexOf("@")==-1)||(from.trim().length()<5)){
  %>
  <jsp:forward page="err.jsp"/>
  <%
  }
  //获得系统属性对象,用下两句的任一句均可
  //Properties props = System.getProperties();
  Properties props=new Properties();
  //设置SMTP主机
  //以下一句是对任意的SMTP 邮件服务器均可,并且有了这一句,将不用设置传输协议了
  props.put("mail.smtp.host", "smtp.jspinsider.com");
  //这样将确定接收方的邮件服务器主机,
  //props.put("mail.host",host);
  //设置邮件传输协议方式
  //props.put("mail.transport.protocol","smtp");
  //获取邮件会话对象
  Session mailSession = Session.getDefaultInstance(props,null);
  mailSession.setDebug(sessionDebug);
  try{
  //创建MIME邮件对象
  Message msg = new MimeMessage(mailSession);
  //设置发信人
  msg.setFrom(new InternetAddress(from));
  //设置收信人
  InternetAddress[] addressTo = {new InternetAddress(to)};
  msg.setRecipients(Message.RecipientType.TO,addressTo);
  //设置抄送人
  if(cc.trim().length()!=0){
  InternetAddress[] addressCc = {new InternetAddress(cc)};
  msg.setRecipients(Message.RecipientType.CC,addressCc);
  }
  //设置暗送人
  if(bcc.trim().length()!=0){
  InternetAddress[] addressBcc = {new InternetAddress(bcc)};
  msg.setRecipients(Message.RecipientType.BCC,addressBcc);
  }
  msg.setSubject(subject);
  msg.setSentDate(new Date());
  msg.setText(messageText);
  //发送邮件
  Transport.send(msg);
  }
  catch(Exception ex){
  out.println("sending failed!");
  }
  %>
  <jsp:forward page="sendok.jsp"/>
  </body>
  </html>

  <%-- sendok.jsp发送成功的页面--%>
  <%@ page contentType="text/html;charset=gb2312"%>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
  <head>
  <title>sendok.jsp</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
  <center>
  <h4>发送成功! </h4>
  <table cellspacing="2" cellpadding="2" border="1">
  <tr>
  <td> 收件人</td><td><%=request.getParameter("to")%></td>
  </tr>
  <tr>
  <td>主题:</td><td><%byte[]
  subjectTemp=request.getParameter("subject").getBytes("ISO8859_1");
  String subject =new String(subjectTemp);
  out.println(subject);
  %>
  </tr>
  <tr>
  <td colspan = "2" align=center><a href ="mailform.html" style="color:rgb(ff,0,0)">
  继续发送</a>
  </td>
  </tr>
  </table>
  </center>
  </body>
  </html>

  <%- errpage.jsp发生内部错误的程序---%>
  <html>
  <head>
  <title>errpage.jsp</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
  出现内部错误
  </body>
  </html>

  <%-- err.jsp 输入有误时的错误提示----%>
  <html>
  <head>
  <title>err.jsp</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
  输入非法
  </body>
  </html>

  (2)HTML类型信件.这种邮件是作为附件接收,收到后把附件打开时是HTML文件.你写入的信件正文在附件的HTML文件里.这个程序主要有两个文件form.html and send.jsp .form.html 用来输入信息表单,send.jsp用作接收信息并处理信息和发送邮件.但这个程序要用到java程序来处理发送.HTML.java 用作发送邮件时调用的,StringDataSource.Java用作汉字显示处理时调用的,注意使用这两个文件时要放在对应的WEB-INF/classes 文件夹下.

  <!--form.html-->
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
  <head>
  <title>mailform.html</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
  <form name="form1" method="post" action="send.jsp">
  <p align="center">填写邮件内容</p>
  <div align="center">
  <table width="75%" border="1">
  <tr>
  <td><div align="center">SMTP主机:</div></td>
  <td><div align="center">
  <input tabindex="text" name="smtp" size="30">
  </div></td>
  </tr>
  <tr>
  <td><div align="center">收件人</div></td>
  <td><div align="center">
  <input name="to" type="text" id="to" size="30" maxlength="30">
  </div></td>
  </tr>
  <tr>
  <td><div align="center">发件人</div></td>
  <td><div align="center">
  <input name="from" type="text" id="from" size="30" maxlength="30">
  </div></td>
  </tr>
  <tr>
  <td><div align="center">抄送</div></td>
  <td><div align="center">
  <input name="cc" type="text" id="cc" size="30" maxlength="100">
  </div></td>
  </tr>
  <tr>
  <td><div align="center">暗送</div></td>
  <td><div align="center">
  <input name="bcc" type="text" id="bcc" size="30" maxlength="100">
  </div></td>
  </tr>
  <tr>
  <td><div align="center">主题</div></td>
  <td><div align="center">
  <input name="subject" type="text" id="subject" size="30" maxlength="30">
  </div></td>
  </tr>
  <tr>
  <td colspan="2"><div align="center">
  <textarea name="body" cols="40" rows="10" id="body"></textarea>
  </div></td>
  </tr>
  <tr>
  <td colspan="2"><div align="center">
  <input type="submit" name="Submit" value="发送">
  &nbsp;&nbsp;&nbsp;&nbsp;
  <input name="Reset" type="submit" id="Reset" value="清空">
  </div></td>
  </tr>
  </table>
  </div>
  <p align="center">&nbsp;</p>
  </form>
  <p align="center">&nbsp;</p>
  </body>
  </html>

  <!—send.jsp-->
  <%@page errorPage="errpage.jsp"%>
  <%
  //变量声明
  java.lang.String smtp,from,to,cc,bcc,subject,body;
  //获得用户输入数据
  smtp = request.getParameter("smtp");
  from = request.getParameter("from");
  to = request.getParameter("to");
  cc = request.getParameter("cc");
  bcc = request.getParameter("bcc");
  subject = request.getParameter("subject");
  if(subject!=null){
  subject = new java.lang.String(subject.getBytes("iso-8859-1"));
  }
  body = request.getParameter("body");
  //发送留言
  mymail.mail.HTML.send(smtp,from,to,cc,bcc,subject,body);
  %>
  <jsp:forward page="sendok.jsp"/>
  /*HTML.java*/
  /*
  * 功能:用作调用send() 方法来发送邮件
  *
  * mymail.mail.HTML.Java
  * package mymail.mail;
  */
  public final class HTML
  {
  public static void send(
  java.lang.String smtp,    /*SMTP主机地址*/
  java.lang.String from,    /*发信人*/
  java.lang.String to,        /*收信人*/
  java.lang.String cc,       /*抄送人*/
  java.lang.String bcc,      /*暗送人*/
  java.lang.String subject, /*主题*/
  java.lang.String body     /*内容*/
  ) throws java.lang.Exception{
  //变量声明
  java.util.Properties props;             //系统属性
  javax.mail.Session mailSession;     //邮件会话对象
  javax.mail.internet.MimeMessage mimeMsg;  //MIME邮件对象
  //设置系统属性
  props = java.lang.System.getProperties(); //获得系统属性对象
  props.put("mail.smtp.host",smtp); //设置SMTP主机
  //获得邮件会话对象
  mailSession = javax.mail.Session.getDefaultInstance(props,null);
  //创建MIME邮件对象
  mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
  //设置发信人
  mimeMsg.setFrom(new javax.mail.internet.InternetAddress(from));
  //设置收信人
  if(to!=null)
  {
  mimeMsg.setRecipients(javax.mail.Message.RecipientType.TO,javax.mail.
  internet.InternetAddress.parse(to));
  }
  //设置抄送人
  if(cc!=null)
  {
  mimeMsg.setRecipients(javax.mail.Message.RecipientType.CC,javax.mail.
  internet.InternetAddress.parse(cc));
  }
  //设置暗送人
  if(bcc!=null)
  {
  mimeMsg.setRecipients
  (javax.mail.Message.RecipientType.BCC,javax.mail.internet.InternetAddress.parse(bcc));
  }
  //设置邮件主题
  //mimeMsg.setSubject(subject);
  mimeMsg.setSubject(subject,"gb2312");
  //设置邮件内容,将邮件body部分转化为HTML格式
  mimeMsg.setDataHandler
  (new javax.activation.DataHandler(new mymail.jaf.StringDataSource (body,"text/html")));
  //发送邮件
  javax.mail.Transport.send(mimeMsg);
  }
  }
  /* StringDataSource.Java */
  /*
  * 功能:将文本型转化为HTML 格式
  * 注意引入的 jaf包
  *
  */
  package mymail.jaf;
  //mymail.jaf.StringDataSource.Java
  //import javax.activation.*;
  public class StringDataSource implements javax.activation.DataSource{
  private java.lang.String data;
  private java.lang.String type;
  public StringDataSource(java.lang.String data,java.lang.String type)
  {
  this.data = data;
  this.type = type;
  }
  public java.io.InputStream getInputStream() throws java.io.IOException
  {
  return new java.io.StringBufferInputStream(data);
  }
  public java.io.OutputStream getOutputStream() throws java.io.IOException
  {
  throw new java.io.IOException("it does not support this method now!");
  }
  public java.lang.String getContentType(){
  return type;
  }
  public java.lang.String getName(){
  return " mymail ";
  }
  }


若有疑问请拨打北大青鸟咨询热线:010-80146691或点击免费在线咨询!
  • xml地图 网站地图 招生简章 合作企业 学员项目 联系我们
  • 关闭窗口