posts - 325,  comments - 25,  trackbacks - 0
          1.input.jsp

          <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            <head> 
              <title>發送文本型文件</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="javamail,keyword2,keyword3">
           <meta http-equiv="description" content="send mail use javamail">
           <meta http-equiv="content-type" content="text/html;charset="utf-8">
           <!--
           <link rel="stylesheet" type="text/css" href="styles.css">
           -->
            </head>
           
            <body>
              <h2>
               <form name="form1" method="post" action="sendMail1.jsp">
                SMTP服務器:<input type="text" id="SMTPHost" name="SMTPHost"><br>
                登錄賬號:<input type="text" id="user" name="user"><br>
                登錄密碼:<input type="password" id="password" name="password"><br>
                發件人郵箱:<input type="text" id="from" name="from"><br>
                收件人郵箱:<input type="text" id="to" name="to"><br>
                郵件主題:<input type="text" id="subject" name="subject"><br>
                郵件內容:<textarea rows="5" cols="40" name="content"></textarea><br><br>
                <input type="submit" name="submit" value="發送">&nbsp;
                <input type="reset" name="reset" value="重置">
               </form>
              </h2>
             
            </body>
          </html>
          2.SendTestMail.java

          package com.lhb.mail;

          import java.util.Date;
          import java.util.Properties;

          import javax.mail.Message;
          import javax.mail.Session;
          import javax.mail.Transport;
          import javax.mail.internet.InternetAddress;
          import javax.mail.internet.MimeMessage;

          public class SendTextMail {
           
           String SMTPHost="";
           String user="";
           String password="";
           String from="";
           String to="";
           String subject="";
           String content="";
           
           public SendTextMail(){
            
           }

           public String getSMTPHost() {
            return SMTPHost;
           }

           public void setSMTPHost(String host) {
            SMTPHost = host;
           }

           public String getUser() {
            return user;
           }

           public void setUser(String user) {
            this.user = user;
           }

           public String getPassword() {
            return password;
           }

           public void setPassword(String password) {
            this.password = password;
           }

           public String getFrom() {
            return from;
           }

           public void setFrom(String from) {
            this.from = from;
           }

           public String getTo() {
            return to;
           }

           public void setTo(String to) {
            this.to = to;
           }

           public String getSubject() {
            return subject;
           }

           public void setSubject(String subject) {
            try {
             subject=new String(subject.getBytes("ISO8859-1"),"utf-8");
            } catch (Exception e) {
             e.printStackTrace();
            }
            this.subject = subject;
           }

           public String getContent() {
            return content;
           }

           public void setContent(String content) {
            try {
             content=new String(content.getBytes("ISO8859-1"),"utf-8");
            } catch (Exception e) {
             e.printStackTrace();
            }
            this.content = content;
           }
           public boolean send(){
            //創建一個屬性對象
            Properties props=new Properties();
            //指定smtp服務器
            props.put("mail.smtp.host", SMTPHost);
            //指定是否需要smtp驗證
            props.put("mail.smtp.auth","true");
            try {
             //創建一個授權驗證對象
             SmtpAuth auth=new SmtpAuth();
             auth.setAccount(user, password);
             //創建一個session對象
             Session mailSession=Session.getDefaultInstance(props);
             mailSession.setDebug(true);
             //創建一個Message對象
             Message message=new MimeMessage(mailSession);
             //指定發件人郵箱
             message.setFrom(new InternetAddress(from));
             //指定收件人郵箱
             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
             //指定郵箱主題
             message.setSubject(subject);
             //指定郵箱內容
             message.setText(content);
             //指定郵件發送日期
             message.setSentDate(new Date());
             //指定郵件優先級 1:緊急 3:普通 5:緩慢
             message.setHeader("X-Priority", "1");
             message.saveChanges();
             //創建一個Transport對象
             Transport transport=mailSession.getTransport("smtp");
             //連接SMTP服務器
             transport.connect(SMTPHost,user, password);
             //發送郵件
             transport.sendMessage(message, message.getAllRecipients());
             transport.close();
             return true;
             
            } catch (Exception e) {
             e.printStackTrace();
             return false;
            }
           }
          }

          3.SmtpAuth.java

          package com.lhb.mail;

          import javax.mail.Authenticator;
          import javax.mail.PasswordAuthentication;

          public class SmtpAuth extends Authenticator {
           String user,password;
           //設置賬號信息
           void setAccount(String user,String password){
            this.user=user;
            this.password=password;
           }
           //取得PsswordAuthentication對象
           protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication(user,password);
           }
          }

           

          4.sendMail1.jsp

          <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
          <%@ page import="com.lhb.mail.SendTextMail"%>
          <jsp:useBean id="mySend" class="com.lhb.mail.SendTextMail"></jsp:useBean>
          <jsp:setProperty name="mySend" property="*"/>
          <%
           boolean status=mySend.send();
           if(status){
            out.println("郵件發送成功");
           }
           else
           {
            out.println("郵件發送失敗");
           }
          %>

          posted on 2008-05-23 09:57 長春語林科技 閱讀(296) 評論(0)  編輯  收藏 所屬分類: util
          <2008年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

           

          長春語林科技歡迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 成都市| 精河县| 永吉县| 海阳市| 五莲县| 新沂市| 灌云县| 镇远县| 苏尼特左旗| 兰坪| 武川县| 合肥市| 徐州市| 永康市| 黎川县| 缙云县| 呼伦贝尔市| 湟中县| 鹤山市| 鱼台县| 宝应县| 肥乡县| 马公市| 河池市| 肇东市| 遵义县| 宁陵县| 都安| 梧州市| 大名县| 合肥市| 扶风县| 浮山县| 邓州市| 富顺县| 福州市| 衡山县| 龙门县| 项城市| 长泰县| 鹤壁市|