SendEmail.java
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:4k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

JavaScript

  1. package com.chinacannel.common;
  2. import java.util.*;
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5. import org.apache.commons.logging.*;
  6. /**
  7.  * <p>Title: </p>
  8.  *
  9.  * <p>Description: </p>
  10.  *
  11.  * <p>Copyright: Copyright (c) 2007</p>
  12.  *
  13.  * <p>Company: </p>
  14.  *
  15.  * @author not attributable
  16.  * @version 1.0
  17.  */
  18. public class SendEmail extends Thread {
  19.     private static Log log = LogFactory.getLog(SendEmail.class);
  20.     private static String url="http://www.easy-office.com.cn";
  21.     private String host = "";
  22.     private String account = "";
  23.     private String password = "";
  24.     private List targets;
  25.     private String subject;
  26.     private String htmlMsg;
  27.     private String textMsg;
  28.     public SendEmail(String subject, String htmlMsg, String textMsg,
  29.                      List targets) {
  30.         this.subject = subject;
  31.         this.setHtmlMsg(htmlMsg);
  32.         this.textMsg = textMsg;
  33.         this.targets = targets;
  34.     }
  35.     public SendEmail(String subject, String htmlMsg, String textMsg,
  36.                      String target) {
  37.         this.subject = subject;
  38.         this.setHtmlMsg(htmlMsg);
  39.         this.textMsg = textMsg;
  40.         this.targets = new ArrayList();
  41.         this.targets.add(target.trim());
  42.     }
  43.     public void run() {
  44.         if (targets != null) {
  45.             log.info("run------------>start");
  46.             Iterator iter = targets.iterator();
  47.             while (iter.hasNext()) {
  48.                 String item = (String) iter.next();
  49.                 try {
  50.                     this.send(item);
  51.                     Thread.sleep(5);
  52.                     log.info("run------------>end");
  53.                 } catch (Exception ex) {
  54.                     log.info("run------------>exception");
  55.                     log.error(ex.getMessage(), ex);
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     protected void send(String toAddress) throws Exception {
  61.         //HtmlEmail!
  62.         Properties props = System.getProperties();
  63.         props.put("mail.smtp.host", this.host);
  64.         props.put("mail.smtp.auth","true");
  65.         Session session = Session.getDefaultInstance(props);
  66.         Message msg = new MimeMessage(session);
  67.         MimeMultipart mmp=new MimeMultipart("related");
  68.         MimeBodyPart mbp = new MimeBodyPart();
  69.         mbp.setContent( this.htmlMsg, "text/html" );
  70.         mmp.addBodyPart(mbp);
  71.         msg.setContent(mmp);
  72.         try {
  73.             msg.setFrom(new InternetAddress(this.account));
  74.             msg.setRecipients(Message.RecipientType.TO,
  75.                               InternetAddress.parse(toAddress, false));
  76. // -- We could include CC recipients too --
  77.             msg.setSubject(subject);
  78. //            msg.setText(this.htmlMsg);
  79. // -- Set some other header information --
  80. //            msg.setHeader("X-Mailer", "LOTONtechEmail");
  81. //            msg.setSentDate(new Date());
  82. // -- Send the message --
  83.             Transport transport = session.getTransport("smtp");
  84.             transport.connect(this.host,25,this.account,this.password);
  85.             transport.sendMessage(msg,msg.getAllRecipients());
  86.             log.info("Message sent OK.");
  87.         } catch (MessagingException ex) {
  88.             log.error(ex.getMessage(),ex);
  89.         }
  90.         log.info("send------------> " + toAddress);
  91.     }
  92.     public void setHost(String host) {
  93.         if (host != null) {
  94.             this.host = host.trim();
  95.         }
  96.     }
  97.     public void setAccount(String account) {
  98.         if (account != null) {
  99.             this.account = account.trim();
  100.         }
  101.     }
  102.     public void setPassword(String password) {
  103.         this.password = password;
  104.     }
  105.     public void setHtmlMsg(String htmlMsg) {
  106.         String src="src="";
  107.         String href="href="";
  108.         htmlMsg=htmlMsg.replaceAll(src,src+url);
  109.         htmlMsg=htmlMsg.replaceAll(href,href+url);
  110. //        int n=-1;
  111. //        while((n=htmlMsg.indexOf(src))>=0){
  112. //            htmlMsg = htmlMsg.substring(0, n+5) + url +
  113. //                   htmlMsg.substring(n+6 + 1);
  114. //        }
  115. //        while((n=htmlMsg.indexOf(href))>=0){
  116. //            htmlMsg = htmlMsg.substring(0, n+6) + url +
  117. //                   htmlMsg.substring(n+7 + 1);
  118. //        }
  119.         this.htmlMsg = htmlMsg;
  120.     }
  121. }