CSDN博客

img yzhxy2002

jsp发送邮件

发表于2004/7/16 10:14:00  2205人阅读

<%@ page contentType="text/html;charset=gb2312" %>
<%request.setCharacterEncoding("gb2312");%><!--中文处理代码-->
<!--引入要用到的类库-->
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.sql.*"%>
<%
String CountryID="",AreaID="",UserID="",sql="",Etel="",CompanyName="";
ResultSet rs=null;

Etel=(String)session.getValue("Etel");
CompanyName=request.getParameter("username");
try{
String[] etelid=Etel.split("-");
CountryID=etelid[0];
AreaID=etelid[1];
UserID=etelid[2];
}catch(Exception e){}
%>
<jsp:useBean id="conn" class="db.DbConnection" scope="page"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>

<body>
<%
 
  try{

  //从html表单中获取邮件信息
  String tto=request.getParameter("to");
  String ttitle=request.getParameter("title");
  String tcontent=request.getParameter("content");
  tcontent="";
  String myEmail=request.getParameter("myEmail");
  out.print(myEmail);
  //************************************************
  out.print(ttitle+tcontent);
  Properties props=new Properties();//也可用Properties props = System.getProperties();
 // props.put("mail.smtp.host","smtp.163.net");//存储发送邮件服务器的信息
  props.put("mail.smtp.host","221.136.100.35");
  props.put("mail.smtp.auth","true");//同时通过验证
  Session s=Session.getInstance(props);//根据属性新建一个邮件会话
  s.setDebug(true);

  MimeMessage message=new MimeMessage(s);//由邮件会话新建一个消息对象

  //设置邮件
  InternetAddress from=new InternetAddress("yzhxy2002@etoall.cn");
  message.setFrom(from);//设置发件人
  InternetAddress to=new InternetAddress(tto);
  message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
  message.setSubject(ttitle);//设置主题
  message.setText(tcontent);//设置信件内容
  message.setSentDate(new Date());//设置发信时间

  //发送邮件
  message.saveChanges();//存储邮件信息
  Transport transport=s.getTransport("smtp");
  //transport.connect("smtp.163.net","boy","iloveyou");//以smtp方式登录邮箱
  transport.connect("221.136.100.35","163@etoall.cn","cn.etoall");
  transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有
  //已设好的收件人地址
  transport.close();
  //更新数据
  /*
  sql="select * from email where Email='"+to+"'";
  rs=conn.executeQuery(sql);
  if (rs.next()){
     out.print("<script>alert('该邮箱已经发过了');history.go(-1);</script>");
  }else{
    sql="insert into email(email,CompanyName,SendPerson) values('"+to+"','"+CompanyName+"','"+Etel+"')";
 conn.executeUpdate(sql);
 sql="update userinfo set sum=sum+1 where CountryID='"+CountryID+"' and AreaID='"+AreaID+"' and UserID='"+UserID+"' ";
 out.print(sql);
 conn.executeUpdate(sql);
  }
  */
%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<p><a href="recEmail.jsp">去看看我的信箱</a><br>
<br>
<a href="sendEmail.jsp">再发一封</a> </p>
</div>
<% 
 }catch(MessagingException e)
 {
  out.println(e.toString());
 }

%>
 </body>
</html>

0 0

相关博文

我的热门文章

img
取 消
img