細心!用心!耐心!

          吾非文人,乃市井一俗人也,讀百卷書,跨江河千里,故申城一游; 一兩滴辛酸,三四年學業,五六點粗墨,七八筆買賣,九十道人情。

          BlogJava 聯系 聚合 管理
            1 Posts :: 196 Stories :: 10 Comments :: 0 Trackbacks
          ??1 /**
          ??2 ?*?
          ??3 ? */

          ??4 package ?com.stt.doss.common.util.mail;
          ??5
          ??6 import ?java.util. * ;
          ??7
          ??8 import ?javax.activation.DataHandler;
          ??9 import ?javax.activation.FileDataSource;
          ?10 import ?javax.mail.BodyPart;
          ?11 import ?javax.mail.Session;
          ?12 import ?javax.mail.Multipart;
          ?13 import ?javax.mail.Message;
          ?14 import ?javax.mail.Address;
          ?15 import ?javax.mail.Transport;
          ?16
          ?17 import ?javax.mail.internet.MimeMessage;
          ?18 import ?javax.mail.internet.MimeMultipart;
          ?19 import ?javax.mail.internet.MimeBodyPart;
          ?20 import ?javax.mail.internet.InternetAddress;
          ?21
          ?22
          ?23 /**
          ?24 ?*? @author ?zhangjp
          ?25 ?*?
          ?26 ? */

          ?27 public ? class ?EMail? {
          ?28
          ?29 ???? private ? static ?MimeMessage?mimeMsg;? // ?MIME郵件對象
          ?30
          ?31 ???? private ?Session?session;? // ?郵件會話對象
          ?32
          ?33 ???? private ? static ?Properties?props;? // ?系統屬性
          ?34
          ?35 ???? private ? static ? boolean ?needAuth? = ? true ;? // ?smtp是否需要認證
          ?36
          ?37 ???? private ? static ?String?username? = ? "" ;? // ?smtp認證用戶名和密碼
          ?38
          ?39 ???? private ? static ?String?password? = ? "" ;
          ?40
          ?41 ???? private ? static ?Multipart?mp;? // ?Multipart對象,郵件內容,標題,附件等內容均添加到其中后再生成MimeMessage對象
          ?42 ????
          ?43 ???? private ? static ?EMail?themail? = ? null ;
          ?44 ????
          ?45 ???? public ?EMail()? {
          ?46 ????????setSmtpHost(MailUtil.MAILHOST);
          ?47 ????????createMimeMessage();
          ?48 ????}

          ?49
          ?50 ???? public ?EMail(String?smtp)? {
          ?51 ????????setSmtpHost(smtp);
          ?52 ????????createMimeMessage();
          ?53 ????}

          ?54
          ?55 ????
          ?56 ???? public ? void ?setSmtpHost(String?hostName)? {
          ?57 ????????System.out.println( " 設置系統屬性:mail.smtp.host?=? " ? + ?hostName);
          ?58 ???????? if ?(props? == ? null )
          ?59 ????????????props? = ?System.getProperties();? // ?獲得系統屬性對象
          ?60
          ?61 ????????props.put( " mail.smtp.host " ,?hostName);? // ?設置SMTP主機
          ?62 ????}

          ?63
          ?64 ????
          ?65 ???? public ? boolean ?createMimeMessage()? {
          ?66 ???????? try ? {
          ?67 ????????????System.out.println( " 準備獲取郵件會話對象! " );
          ?68 ????????????session? = ?Session.getDefaultInstance(props,? null );? // ?獲得郵件會話對象
          ?69 ????????}
          ? catch ?(Exception?e)? {
          ?70 ????????????System.err.println( " 獲取郵件會話對象時發生錯誤! " ? + ?e);
          ?71 ???????????? return ? false ;
          ?72 ????????}

          ?73
          ?74 ????????System.out.println( " 準備創建MIME郵件對象! " );
          ?75 ???????? try ? {
          ?76 ????????????mimeMsg? = ? new ?MimeMessage(session);? // ?創建MIME郵件對象
          ?77 ????????????mp? = ? new ?MimeMultipart();
          ?78
          ?79 ???????????? return ? true ;
          ?80 ????????}
          ? catch ?(Exception?e)? {
          ?81 ????????????System.err.println( " 創建MIME郵件對象失敗! " ? + ?e);
          ?82 ???????????? return ? false ;
          ?83 ????????}

          ?84 ????}

          ?85
          ?86 ????
          ?87 ???? public ? void ?setNeedAuth( boolean ?need)? {
          ?88 ????????System.out.println( " 設置smtp身份認證:mail.smtp.auth?=? " ? + ?need);
          ?89 ???????? if ?(props? == ? null )
          ?90 ????????????props? = ?System.getProperties();
          ?91
          ?92 ???????? if ?(need)? {
          ?93 ????????????props.put( " mail.smtp.auth " ,? " true " );
          ?94 ????????}
          ? else ? {
          ?95 ????????????props.put( " mail.smtp.auth " ,? " false " );
          ?96 ????????}

          ?97 ????}

          ?98
          ?99
          100 ???? public ? void ?setNamePass(String?name,?String?pass)? {
          101 ????????username? = ?name;
          102 ????????password? = ?pass;
          103 ????}

          104
          105 ???? /**
          106 ?????*? @param ?mailSubject
          107 ?????*????????????String
          108 ?????*? @return ?boolean
          109 ????? */

          110 ???? public ? boolean ?setSubject(String?mailSubject)? {
          111 ????????System.out.println( " 設置郵件主題! " );
          112 ???????? try ? {
          113 ????????????mimeMsg.setSubject(mailSubject);
          114 ???????????? return ? true ;
          115 ????????}
          ? catch ?(Exception?e)? {
          116 ????????????System.err.println( " 設置郵件主題發生錯誤! " );
          117 ???????????? return ? false ;
          118 ????????}

          119 ????}

          120
          121 ????
          122 ???? public ? boolean ?setBody(String?mailBody)? {
          123 ???????? try ? {
          124 ????????????BodyPart?bp? = ? new ?MimeBodyPart();
          125 ????????????bp.setContent(
          126 ???????????????????? " <meta?http-equiv=Content-Type?content=text/html;?charset=gb2312> "
          127 ???????????????????????????? + ?mailBody,? " text/html;charset=GB2312 " );
          128 ????????????mp.addBodyPart(bp);
          129
          130 ???????????? return ? true ;
          131 ????????}
          ? catch ?(Exception?e)? {
          132 ????????????System.err.println( " 設置郵件正文時發生錯誤! " ? + ?e);
          133 ???????????? return ? false ;
          134 ????????}

          135 ????}

          136
          137
          138
          139 ????
          140 ???? public ? boolean ?setFrom(String?from)? {
          141 ????????System.out.println( " 設置發信人! " );
          142 ???????? try ? {
          143 ????????????mimeMsg.setFrom( new ?InternetAddress(from));? // ?設置發信人
          144 ???????????? return ? true ;
          145 ????????}
          ? catch ?(Exception?e)? {
          146 ???????????? return ? false ;
          147 ????????}

          148 ????}

          149
          150 ???? /**
          151 ?????*? @param ?name
          152 ?????*????????????String
          153 ?????*? @param ?pass
          154 ?????*????????????String
          155 ????? */

          156 ???? public ? boolean ?setTo(String?to)? {
          157 ???????? if ?(to? == ? null )
          158 ???????????? return ? false ;
          159
          160 ???????? try ? {
          161 ????????????mimeMsg.setRecipients(Message.RecipientType.TO,?InternetAddress
          162 ????????????????????.parse(to));
          163 ???????????? return ? true ;
          164 ????????}
          ? catch ?(Exception?e)? {
          165 ???????????? return ? false ;
          166 ????????}

          167
          168 ????}

          169
          170 ???? /**
          171 ?????*? @param ?name
          172 ?????*????????????String
          173 ?????*? @param ?pass
          174 ?????*????????????String
          175 ????? */

          176 ???? public ? boolean ?setCopyTo(String?copyto)? {
          177 ???????? if ?(copyto? == ? null )
          178 ???????????? return ? false ;
          179 ???????? try ? {
          180 ????????????mimeMsg.setRecipients(Message.RecipientType.CC,
          181 ????????????????????(Address[])?InternetAddress.parse(copyto));
          182 ???????????? return ? true ;
          183 ????????}
          ? catch ?(Exception?e)? {
          184 ???????????? return ? false ;
          185 ????????}

          186 ????}

          187
          188 ????
          189 ???? public ? boolean ?addFileAffix(String?filename)? {
          190
          191 ????????System.out.println( " 增加郵件附件: " ? + ?filename);
          192
          193 ???????? try ? {
          194 ????????????BodyPart?bp? = ? new ?MimeBodyPart();
          195 ????????????FileDataSource?fileds? = ? new ?FileDataSource(filename);
          196 ????????????bp.setDataHandler( new ?DataHandler(fileds));
          197 ????????????bp.setFileName(fileds.getName());
          198
          199 ????????????mp.addBodyPart(bp);
          200
          201 ???????????? return ? true ;
          202 ????????}
          ? catch ?(Exception?e)? {
          203 ????????????System.err.println( " 增加郵件附件: " ? + ?filename? + ? " 發生錯誤! " ? + ?e);
          204 ???????????? return ? false ;
          205 ????????}

          206 ????}

          207 ????
          208 ????
          209 ????
          210 ???? /**
          211 ?????*? @param ?name
          212 ?????*????????????String
          213 ?????*? @param ?pass
          214 ?????*????????????String
          215 ????? */

          216 ???? public ? static ? boolean ?sendout(Object?obj)? {
          217 ????????EmailMessage?em? = ?(EmailMessage)?obj;
          218 ????????
          219 ????????themail? = ? new ?EMail(em.getMailHost());????????
          220 ????????themail.setNeedAuth(needAuth);
          221 ????????
          222 ????????themail.setSubject(em.getMailTitle());
          223 ????????themail.setBody(em.getMailBody());
          224 ????????themail.setTo(em.getMailToAddress());
          225 ????????themail.setFrom(em.getMailFromAddress());
          226 ????????themail.setNamePass(em.getMailName(),?em.getMailPassword());
          227 ????????
          228 ???????? try ? {
          229 ????????????mimeMsg.setContent(mp);
          230 ????????????mimeMsg.saveChanges();
          231 ????????????System.out.println( " 正在發送郵件. " );
          232
          233 ????????????Session?mailSession? = ?Session.getInstance(props,? null );
          234 ????????????Transport?transport? = ?mailSession.getTransport( " smtp " );
          235 ????????????transport.connect((String)?props.get( " mail.smtp.host " ),?username,
          236 ????????????????????password);
          237 ????????????transport.sendMessage(mimeMsg,?mimeMsg
          238 ????????????????????.getRecipients(Message.RecipientType.TO));
          239
          240 ????????????System.out.println( " 發送郵件成功! " );
          241 ????????????transport.close();
          242
          243 ???????????? return ? true ;
          244 ????????}
          ? catch ?(Exception?e)? {
          245 ????????????System.err.println( " 郵件發送失敗! " ? + ?e);
          246 ???????????? return ? false ;
          247 ????????}

          248 ????}

          249
          250 ????
          251 ???? /**
          252 ?????*? @param ?name
          253 ?????*????????????String
          254 ?????*? @param ?pass
          255 ?????*????????????String
          256 ????? */

          257 ???? public ? static ? boolean ?sendoutAddAffix(Object?obj)? {
          258 ????????EmailMessage?em? = ?(EmailMessage)?obj;
          259 ????????
          260 ????????themail? = ? new ?EMail(em.getMailHost());????????
          261 ????????themail.setNeedAuth(needAuth);
          262 ????????
          263 ????????themail.setSubject(em.getMailTitle());
          264 ????????themail.setBody(em.getMailBody());
          265 ????????themail.setTo(em.getMailToAddress());
          266 ????????themail.setFrom(em.getMailFromAddress());
          267 ????????themail.addFileAffix(em.getMailAffix());
          268 ????????themail.setNamePass(em.getMailName(),?em.getMailPassword());
          269 ????????
          270 ???????? try ? {
          271 ????????????mimeMsg.setContent(mp);
          272 ????????????mimeMsg.saveChanges();
          273 ????????????System.out.println( " 正在發送郵件. " );
          274
          275 ????????????Session?mailSession? = ?Session.getInstance(props,? null );
          276 ????????????Transport?transport? = ?mailSession.getTransport( " smtp " );
          277 ????????????transport.connect((String)?props.get( " mail.smtp.host " ),?username,
          278 ????????????????????password);
          279 ????????????transport.sendMessage(mimeMsg,?mimeMsg
          280 ????????????????????.getRecipients(Message.RecipientType.TO));
          281
          282 ????????????System.out.println( " 發送郵件成功! " );
          283 ????????????transport.close();
          284
          285 ???????????? return ? true ;
          286 ????????}
          ? catch ?(Exception?e)? {
          287 ????????????System.err.println( " 郵件發送失敗! " ? + ?e);
          288 ???????????? return ? false ;
          289 ????????}

          290 ????}

          291
          292 ????
          293 }

          294
          295 ????
          296
          297
          298
          posted on 2007-03-16 15:28 張金鵬 閱讀(243) 評論(0)  編輯  收藏 所屬分類: core java中的一些數據結構的處理
          主站蜘蛛池模板: 长春市| 三门峡市| 无为县| 榆社县| 梅河口市| 信丰县| 南阳市| 宜都市| 儋州市| 赤城县| 洮南市| 西藏| 若羌县| 大邑县| 泰州市| 曲周县| 耒阳市| 永善县| 焦作市| 贵南县| 肇庆市| 宜兴市| 德江县| 常州市| 桑植县| 大邑县| 竹北市| 枞阳县| 来宾市| 仁布县| 正蓝旗| 崇明县| 阜宁县| 诸城市| 格尔木市| 名山县| 略阳县| 乐亭县| 健康| 甘肃省| 都匀市|