已遷址

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

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            28 隨筆 :: 0 文章 :: 1 評(píng)論 :: 0 Trackbacks
          開發(fā)前要先下載mail.jar,如果用Myeclipse開發(fā)會(huì)有沖突,因myeclipse中自帶有mail.jar,但不好用,解決辦法:刪除相關(guān)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"; //密碼,不用說(shuō)了吧
          10     protected PasswordAuthentication getPasswordAuthentication() {
          11         return new PasswordAuthentication(MAIL_USER, MAIL_PASSWORD);
          12     }
          13     
          14 }
          郵件發(fā)送類 
            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;    //郵件回復(fù)地址
           21     private String smtpHost = null;    //SMTP服務(wù)器
           22     private boolean debug = false;    
           23     private String subject;            //郵件主題
           24     private String msgContent;        //郵件內(nèi)容
           25     private String messageContentMimeType = "text/html;charset=gb2312";    //郵件內(nèi)容格式
           26     
           27     /**
           28      * 填充郵件相關(guān)信息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();    //模擬信息所需郵件內(nèi)容方法的參數(shù)
           38         if (mailReply != null) {
           39             msg.setFrom(new InternetAddress(mailReply));
           40             System.out.println("發(fā)送人Mail地址:" + mailReply);
           41         } else {
           42             System.out.println("沒有指定發(fā)送人郵件地址!");
           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();    //設(shè)置內(nèi)容編碼和內(nèi)容
           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來(lái)創(chuàng)建一個(gè)session對(duì)象,其屬性值就是發(fā)送郵件的主機(jī)
           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);    //創(chuàng)建session對(duì)象
           77         session.setDebug(debug);
           78         MimeMessage msg=new MimeMessage(session);    //創(chuàng)建模擬郵件信息
           79         Transport trans= null;                //創(chuàng)建發(fā)送對(duì)象
           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("連接郵件服務(wù)器錯(cuò)誤:");
           91                 return 3;
           92             } catch (MessagingException e) {
           93                 e.printStackTrace();
           94                 System.out.println("連接郵件服務(wù)器錯(cuò)誤:");
           95                 return 3;
           96             }
           97             trans.send(msg);    //發(fā)送郵件
           98             trans.close();
           99         } catch (MessagingException  e) {
          100             System.out.println("發(fā)送郵件失敗:");
          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("發(fā)送郵件成功!");
          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方法測(cè)試
          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("測(cè)試郵件,請(qǐng)注意查收,如有問題,請(qǐng)聯(lián)系開發(fā)人員!");
          135         sm.setSubject("郵件標(biāo)題");
          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) 評(píng)論(0)  編輯  收藏 所屬分類: JavaMail

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 丘北县| 海原县| 吉安市| 台东县| 全州县| 万全县| 吉首市| 南雄市| 册亨县| 镇赉县| 万载县| 桓仁| 呼伦贝尔市| 舟曲县| 清河县| 柳林县| 寿阳县| 蛟河市| 富阳市| 福建省| 西乌珠穆沁旗| 武平县| 大宁县| 夏河县| 当阳市| 松溪县| 灵武市| 泰顺县| 奇台县| 怀集县| 若羌县| 福州市| 榕江县| 开鲁县| 随州市| 兴义市| 祁连县| 天峻县| 囊谦县| 灵武市| 和田县|