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>
                登錄賬號(hào):<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)建一個(gè)屬性對(duì)象
            Properties props=new Properties();
            //指定smtp服務(wù)器
            props.put("mail.smtp.host", SMTPHost);
            //指定是否需要smtp驗(yàn)證
            props.put("mail.smtp.auth","true");
            try {
             //創(chuàng)建一個(gè)授權(quán)驗(yàn)證對(duì)象
             SmtpAuth auth=new SmtpAuth();
             auth.setAccount(user, password);
             //創(chuàng)建一個(gè)session對(duì)象
             Session mailSession=Session.getDefaultInstance(props);
             mailSession.setDebug(true);
             //創(chuàng)建一個(gè)Message對(duì)象
             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)先級(jí) 1:緊急 3:普通 5:緩慢
             message.setHeader("X-Priority", "1");
             message.saveChanges();
             //創(chuàng)建一個(gè)Transport對(duì)象
             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è)置賬號(hào)信息
           void setAccount(String user,String password){
            this.user=user;
            this.password=password;
           }
           //取得PsswordAuthentication對(duì)象
           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 長(zhǎng)春語(yǔ)林科技 閱讀(294) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): util
          <2008年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

           

          長(zhǎng)春語(yǔ)林科技?xì)g迎您!

          常用鏈接

          留言簿(6)

          隨筆分類(lèi)

          隨筆檔案

          文章分類(lèi)

          文章檔案

          相冊(cè)

          收藏夾

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 天台县| 红安县| 长沙市| 林州市| 彭阳县| 阿克陶县| 勐海县| 游戏| 阳泉市| 庆城县| 嘉祥县| 衡水市| 永靖县| 新泰市| 乡城县| 翁牛特旗| 镇平县| 扎赉特旗| 琼结县| 镇沅| 黔江区| 定远县| 会理县| 天长市| 承德市| 中江县| 柳江县| 甘谷县| 高台县| 和政县| 彭州市| 五华县| 高唐县| 象山县| 沁源县| 宝兴县| 通城县| 太仆寺旗| 玛沁县| 平湖市| 弥勒县|