jsp邮件发送

来源:互联网 发布:建筑总平面图尺寸数据 编辑:程序博客网 时间:2024/05/21 17:59

在lib加入activation.jar  mail.jar两个jar包

index.jsp

 

<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="error.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>发送电子邮件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="../image/com.css"/></head>
<body>
<form method="post" name="form1" action="email.jsp" onSubmit="return email()">
  <table width="480" height="393" border="1" align="center" cellpadding="0" cellspacing="0">
    <tr bordercolor="#D4D0C8" bgcolor="#FEFFC9">
      <td height="27" colspan="2" align="left"><div align="center"><strong>发送电子邮件</strong></div></td>
    </tr>
    <tr bgcolor="#FEFFC9">
      <td width="68" height="27" align="left"><div align="right">收件人:</div></td>
      <td width="406" height="27"><div align="center">
          <input type="text" name="sname" size="51" value="252672099@qq.com">
        </div></td>
    </tr>
    <tr bgcolor="#FEFFC9">
      <td height="27" align="left"><div align="right">发件人:</div></td>
      <td height="27"> <div align="center">
          <input type="text" name="jname" size="51" value="15890251132@126.com">
        </div></td>
    </tr>
    <tr bgcolor="#FEFFC9">
      <td height="27" align="left"><div align="right">密&nbsp; 码:</div></td>
      <td height="27"><div align="center"><input type="password" name="password" value="****"  size="57"></div></td>
    </tr>
    <tr bgcolor="#FEFFC9">
      <td height="27" align="left"><div align="right">主&nbsp; 题:</div></td>
      <td height="27"><div align="center">
          <input name="title" type="text" value="15890251132" size="51">
        </div></td>
    </tr>
    <tr align="left" bgcolor="#FEFFC9">
      <td height="227" valign="top"><div align="right">内 &nbsp;容:</div></td>
      <td height="227"><div align="center">
          <textarea name="message" cols="50"  rows="15"> asfdfasdfda</textarea>
        </div></td>
    </tr>
    <tr align="center" valign="middle" bordercolor="#D4D0C8" bgcolor="#FEFFC9">
      <td height="29" colspan="2"><input type="submit" name="Submit" value="发送">
        &nbsp;&nbsp; <input type="reset" name="Submit2" value="清除"></td>
    </tr>
  </table>
</form>
</body>
</html>

 

 

email.jsp

 

<%@ page language="java" import="javax.mail.*,javax.mail.internet.*,javax.activation.*,java.util.*,javax.mail.Message.*"
 pageEncoding="GB18030"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">

  <title>My JSP 'email.jsp' starting page</title>

  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

 </head>

 <body>
  <%
   try {
    
    
      Properties props = new Properties();
  props.setProperty("mail.smtp.auth", "true");
  props.setProperty("mail.transport.protocol", "smtp");
  props.setProperty("mail.host", "smtp.126.com");
  Session session1 = Session.getInstance(props,new Authenticator()
    {
     protected PasswordAuthentication getPasswordAuthentication()
     {
      return new PasswordAuthentication("15890251132@126.com","*_****");
     }
    }
  );
  String   S =request.getParameter("sname");
    int n =S.indexOf('@');
        int m=S.length() ;
  String server=S.substring(n+1,m);
  out.println(server);
  session1.setDebug(true);
  
  Message msg = new MimeMessage(session1);
  msg.setFrom(new InternetAddress(request
      .getParameter("jname")));
  msg.setSubject(request.getParameter("title"));
  msg.addRecipient(Message.RecipientType.TO,new InternetAddress(S));
  msg.setContent(request.getParameter("message"), "text/html;charset=gbk");
  
  Transport.send(msg);
  %>
  <P>Your mail has been sent.</P>
  <%
   } catch (Exception m) {
    out.println(m.toString());
   }
  %>


 </body>
</html>

原创粉丝点击