dream.in.java

能以不变应万变是聪明人做事的准则。万事从小事做起,积累小成功,问鼎大成功,是成功者的秘诀。

JavaMail应用成功例子


First,you should download javamail from and jaf.
写下这个东西,以后备用~~~

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%
 request.setCharacterEncoding("GB2312");
%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Insert title here</title>
 </head>
 <body>
  <%
   try {
    //从HTML中获取表单信息
    String tto = request.getParameter("to");
    String ttitle = request.getParameter("title");
    String tcontent = request.getParameter("content");
    String ffrom = request.getParameter("from");
    
    int postion = ffrom.indexOf("@");
    String userName = ffrom.substring(0,postion);//发件人邮件名
    out.print(tto + "<br>");
    out.print(ttitle + "<br>");
    out.print(tcontent + "<br>");
    out.print(ffrom + "<br>");
    out.print(userName + "<br>");

    Properties props = new Properties();
    props.put("mail.smtp.host", "stmpt.163.com");//储存发送邮件服务器的信息
    props.put("mail.smtp.auth", "true");//同时通过验证,一定要为true
    Session s = Session.getInstance(props);//根据属性新建一个邮件会话
    s.setDebug(true);

    MimeMessage message = new MimeMessage(s);//由邮件会话新建一个消息对象
    //设置邮件
    //设置发件人
    InternetAddress from = new InternetAddress(ffrom);
    message.setFrom(from);

    InternetAddress to = new InternetAddress(tto);
    message.setRecipient(Message.RecipientType.TO, to);//设置收件人
    message.setSubject(ttitle);//设置主题
    message.setText(tcontent);//设置信件内容
    message.setSentDate(new Date());//设置发信时间

    message.saveChanges();//储存邮件信息
    Transport transport = s.getTransport("smtp");
    transport.connect("smtp.163.com", userName,
      "你的邮件密码");//以stmp方式登录邮箱,必须有用户名和密码
    transport.sendMessage(message,
     message.getAllRecipients());//发送邮件,第二个参数为所有已设好的收件人地址
    transport.close();
  %>

  <div align="center">
   <p>
    <font color="red">发送成功</font>
   </p>
   <br>
   <a href="index.htm">send one again</a>
  </div>
  <%
   } catch (Exception e) {
    e.printStackTrace();
   }
  %>
 </body>
</html>

另外有一篇的文章可以参考:http://www.blogjava.net/TrampEagle/archive/2009/03/18/48326.html

posted on 2009-03-22 15:07 YXY 阅读(139) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: