jsp邮件发送

在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>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值