已遷址

          已遷址http://www.cnblogs.com/live365wang/

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            28 隨筆 :: 0 文章 :: 1 評論 :: 0 Trackbacks
          開發前要先下載mail.jar,如果用Myeclipse開發會有沖突,因myeclipse中自帶有mail.jar,但不好用,解決辦法:刪除相關mail類,如我的刪除方法是找到myeclipse安裝目錄D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.6.0.me201007292038\data\libraryset\EE_5\javaee.jar用7-zip壓縮軟件打開,刪除下面的mail文件夾

          郵箱用戶名密碼 
          1 package com.tg.email;
           2 
           3 import javax.mail.Authenticator;
           4 import javax.mail.PasswordAuthentication;
           5 
           6 public class MailAuthenticator extends Authenticator {
           7 
           8     public static String MAIL_USER = "所用Email地址,如cc@163.com";
           9     public static String MAIL_PASSWORD = "123456"; //密碼,不用說了吧
          10     protected PasswordAuthentication getPasswordAuthentication() {
          11         return new PasswordAuthentication(MAIL_USER, MAIL_PASSWORD);
          12     }
          13     
          14 }
          郵件發送類 
            1 package com.tg.email;
            2 
            3 import java.io.IOException;
            4 import java.util.Date;
            5 import java.util.Properties;
            6 import javax.mail.AuthenticationFailedException;
            7 import javax.mail.Authenticator;
            8 import javax.mail.Message;
            9 import javax.mail.MessagingException;
           10 import javax.mail.Multipart;
           11 import javax.mail.Session;
           12 import javax.mail.Transport;
           13 import javax.mail.internet.InternetAddress;
           14 import javax.mail.internet.MimeBodyPart;
           15 import javax.mail.internet.MimeMessage;
           16 import javax.mail.internet.MimeMultipart;
           17 
           18 public class SendMail {
           19     private String mailTo = null;    //郵件接收者
           20     private String mailReply = null;    //郵件回復地址
           21     private String smtpHost = null;    //SMTP服務器
           22     private boolean debug = false;    
           23     private String subject;            //郵件主題
           24     private String msgContent;        //郵件內容
           25     private String messageContentMimeType = "text/html;charset=gb2312";    //郵件內容格式
           26     
           27     /**
           28      * 填充郵件相關信息Method
           29      * @param session
           30      * @param msg
           31      * @throws IOException
           32      * @throws MessagingException
           33      */
           34     private void fillMail(Session session, MimeMessage msg) throws IOException,
           35             MessagingException {
           36         
           37         Multipart mPart = new MimeMultipart();    //模擬信息所需郵件內容方法的參數
           38         if (mailReply != null) {
           39             msg.setFrom(new InternetAddress(mailReply));
           40             System.out.println("發送人Mail地址:" + mailReply);
           41         } else {
           42             System.out.println("沒有指定發送人郵件地址!");
           43             return;
           44         }
           45         if (mailTo != null) {
           46             InternetAddress[] address = InternetAddress.parse(mailTo);
           47             msg.setRecipients(Message.RecipientType.TO, address);
           48         } else {
           49             System.out.println("沒有指定收件人郵件地址!");
           50             return;
           51         }
           52         msg.setSubject(subject);
           53         InternetAddress[] replyAddress = { new InternetAddress(mailReply) };
           54         msg.setReplyTo(replyAddress);
           55 
           56         MimeBodyPart mBodyContent = new MimeBodyPart();    //設置內容編碼和內容
           57         if (msgContent != null) {
           58             mBodyContent.setContent(msgContent, messageContentMimeType);
           59         }else{
           60             mBodyContent.setContent("", messageContentMimeType);
           61         }
           62         mPart.addBodyPart(mBodyContent);
           63         msg.setContent(mPart);
           64         msg.setSentDate(new Date());
           65     }
           66     
           67     
           68     @SuppressWarnings("static-access")
           69     public int sendMail() throws IOException,MessagingException{
           70         
           71         Properties props = System.getProperties();    //JavaMail需要Properties來創建一個session對象,其屬性值就是發送郵件的主機
           72         props.put("mail.smtp.host", smtpHost);
           73         props.put("mail.smtp.auth""true");
           74     
           75         Authenticator auth= new MailAuthenticator();    
           76         Session session=Session.getInstance(props,auth);    //創建session對象
           77         session.setDebug(debug);
           78         MimeMessage msg=new MimeMessage(session);    //創建模擬郵件信息
           79         Transport trans= null;                //創建發送對象
           80         fillMail(session, msg);
           81 
           82         try {
           83             trans=session.getTransport("smtp");
           84             try {
           85                 
           86                 trans.connect(smtpHost,MailAuthenticator.MAIL_USER, MailAuthenticator.MAIL_PASSWORD);
           87                 
           88             } catch (AuthenticationFailedException e) {
           89                 e.printStackTrace();
           90                 System.out.println("連接郵件服務器錯誤:");
           91                 return 3;
           92             } catch (MessagingException e) {
           93                 e.printStackTrace();
           94                 System.out.println("連接郵件服務器錯誤:");
           95                 return 3;
           96             }
           97             trans.send(msg);    //發送郵件
           98             trans.close();
           99         } catch (MessagingException  e) {
          100             System.out.println("發送郵件失敗:");
          101             e.printStackTrace();    
          102         }finally{
          103             try {
          104                 if (trans!=null&&trans.isConnected()) {
          105                     trans.close();
          106                 }
          107             } catch (Exception e) {
          108                 System.out.println(e.toString());
          109             }
          110         }
          111         System.out.println("發送郵件成功!");
          112         return 0;
          113     }
          114     
          115     public SendMail(String smtpHost,String replyAddress,String mailTo,String subject,String content){
          116         this.smtpHost=smtpHost;
          117         this.mailReply=replyAddress;
          118         this.mailTo=mailTo;
          119         this.subject=subject;
          120         this.msgContent=content;
          121     }
          122     
          123     public SendMail(){}
          124     
          125     /**
          126      * main方法測試
          127      * @param args
          128      */
          129     public static void main(String[] args) {
          130         SendMail sm=new SendMail();
          131         sm.setSmtpHost("smtp.163.com");
          132         sm.setMailReply("tg_mail@163.com");
          133         sm.setMailTo("471164275@qq.com");
          134         sm.setMsgContent("測試郵件,請注意查收,如有問題,請聯系開發人員!");
          135         sm.setSubject("郵件標題");
          136         try {
          137             sm.sendMail();
          138         } catch (IOException e) {
          139             e.printStackTrace();
          140         } catch (MessagingException e) {
          141             e.printStackTrace();
          142         }
          143     }
          144     
          145     
          146     public String getMailTo() {
          147         return mailTo;
          148     }
          149 
          150     public void setMailTo(String mailTo) {
          151         this.mailTo = mailTo;
          152     }
          153 
          154     public String getSmtpHost() {
          155         return smtpHost;
          156     }
          157 
          158     public void setSmtpHost(String smtpHost) {
          159         this.smtpHost = smtpHost;
          160     }
          161 
          162     public boolean isDebug() {
          163         return debug;
          164     }
          165 
          166     public void setDebug(boolean debug) {
          167         this.debug = debug;
          168     }
          169 
          170     public String getSubject() {
          171         return subject;
          172     }
          173 
          174     public void setSubject(String subject) {
          175         this.subject = subject;
          176     }
          177 
          178     public String getMsgContent() {
          179         return msgContent;
          180     }
          181 
          182     public void setMsgContent(String msgContent) {
          183         this.msgContent = msgContent;
          184     }
          185 
          186     public String getMessageContentMimeType() {
          187         return messageContentMimeType;
          188     }
          189 
          190     public void setMessageContentMimeType(String messageContentMimeType) {
          191         this.messageContentMimeType = messageContentMimeType;
          192     }
          193 
          194     public void setMailReply(String mailReply) {
          195         this.mailReply = mailReply;
          196     }
          197 
          198     public String getMailReply() {
          199         return mailReply;
          200     }
          201 
          202 }
          203 
          posted on 2011-02-22 11:09 已遷址 閱讀(1326) 評論(0)  編輯  收藏 所屬分類: JavaMail

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


          網站導航:
           
          主站蜘蛛池模板: 磐安县| 禹城市| 黎城县| 出国| 咸丰县| 江油市| 英吉沙县| 汝阳县| 建阳市| 平利县| 永吉县| 将乐县| 宝鸡市| 迭部县| 治多县| 朔州市| 张北县| 义乌市| 丰台区| 阜城县| 博客| 子洲县| 长汀县| 大宁县| 大足县| 利川市| 唐海县| 黄梅县| 盘锦市| 同仁县| 钟祥市| 台南市| 临沭县| 宣汉县| 阿拉善盟| 当雄县| 潞城市| 青冈县| 大埔县| 荔浦县| 康马县|