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>發(fā)送文本型文件</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服務(wù)器:<input type="text" id="SMTPHost" name="SMTPHost"><br>
                登錄賬號:<input type="text" id="user" name="user"><br>
                登錄密碼:<input type="password" id="password" name="password"><br>
                發(fā)件人郵箱:<input type="text" id="from" name="from"><br>
                收件人郵箱:<input type="text" id="to" name="to"><br>
                郵件主題:<input type="text" id="subject" name="subject"><br>
                郵件內(nèi)容:<textarea rows="5" cols="40" name="content"></textarea><br><br>
                <input type="submit" name="submit" value="發(fā)送">&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(){
            //創(chuàng)建一個屬性對象
            Properties props=new Properties();
            //指定smtp服務(wù)器
            props.put("mail.smtp.host", SMTPHost);
            //指定是否需要smtp驗證
            props.put("mail.smtp.auth","true");
            try {
             //創(chuàng)建一個授權(quán)驗證對象
             SmtpAuth auth=new SmtpAuth();
             auth.setAccount(user, password);
             //創(chuàng)建一個session對象
             Session mailSession=Session.getDefaultInstance(props);
             mailSession.setDebug(true);
             //創(chuàng)建一個Message對象
             Message message=new MimeMessage(mailSession);
             //指定發(fā)件人郵箱
             message.setFrom(new InternetAddress(from));
             //指定收件人郵箱
             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
             //指定郵箱主題
             message.setSubject(subject);
             //指定郵箱內(nèi)容
             message.setText(content);
             //指定郵件發(fā)送日期
             message.setSentDate(new Date());
             //指定郵件優(yōu)先級 1:緊急 3:普通 5:緩慢
             message.setHeader("X-Priority", "1");
             message.saveChanges();
             //創(chuàng)建一個Transport對象
             Transport transport=mailSession.getTransport("smtp");
             //連接SMTP服務(wù)器
             transport.connect(SMTPHost,user, password);
             //發(fā)送郵件
             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;
           //設(shè)置賬號信息
           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("郵件發(fā)送成功");
           }
           else
           {
            out.println("郵件發(fā)送失敗");
           }
          %>

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

           

          長春語林科技歡迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 饶平县| 顺平县| 义马市| 万荣县| 陇西县| 潮安县| 碌曲县| 勃利县| 荣成市| 那曲县| 玛多县| 西盟| 海林市| 望江县| 金溪县| 利辛县| 满洲里市| 红安县| 宜阳县| 五莲县| 黎城县| 革吉县| 博爱县| 闽清县| 昌吉市| 始兴县| 鄂托克前旗| 玉田县| 合川市| 灯塔市| 东阿县| 合阳县| 建德市| 年辖:市辖区| 仙桃市| 广河县| 阿拉尔市| 庆安县| 梁山县| 云梦县| 都昌县|