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.aygfsteel.com/TrampEagle/archive/2009/03/18/48326.html

          posted on 2009-03-22 15:07 YXY 閱讀(143) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 定远县| 安塞县| 铜梁县| 综艺| 宣武区| 石阡县| 建平县| 绥江县| 木里| 宁安市| 杭锦后旗| 灌阳县| 利津县| 崇仁县| 常州市| 肥西县| 河津市| 阜宁县| 博乐市| 江孜县| 青岛市| 阿鲁科尔沁旗| 蕉岭县| 微山县| 巍山| 合江县| 白城市| 息烽县| 延庆县| 团风县| 行唐县| 河南省| 普定县| 新营市| 莱芜市| 阳山县| 舞阳县| 尤溪县| 江阴市| 将乐县| 额尔古纳市|