SendEmail.java
资源名称:shihua.rar [点击查看]
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:4k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
JavaScript
- package com.chinacannel.common;
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- import org.apache.commons.logging.*;
- /**
- * <p>Title: </p>
- *
- * <p>Description: </p>
- *
- * <p>Copyright: Copyright (c) 2007</p>
- *
- * <p>Company: </p>
- *
- * @author not attributable
- * @version 1.0
- */
- public class SendEmail extends Thread {
- private static Log log = LogFactory.getLog(SendEmail.class);
- private static String url="http://www.easy-office.com.cn";
- private String host = "";
- private String account = "";
- private String password = "";
- private List targets;
- private String subject;
- private String htmlMsg;
- private String textMsg;
- public SendEmail(String subject, String htmlMsg, String textMsg,
- List targets) {
- this.subject = subject;
- this.setHtmlMsg(htmlMsg);
- this.textMsg = textMsg;
- this.targets = targets;
- }
- public SendEmail(String subject, String htmlMsg, String textMsg,
- String target) {
- this.subject = subject;
- this.setHtmlMsg(htmlMsg);
- this.textMsg = textMsg;
- this.targets = new ArrayList();
- this.targets.add(target.trim());
- }
- public void run() {
- if (targets != null) {
- log.info("run------------>start");
- Iterator iter = targets.iterator();
- while (iter.hasNext()) {
- String item = (String) iter.next();
- try {
- this.send(item);
- Thread.sleep(5);
- log.info("run------------>end");
- } catch (Exception ex) {
- log.info("run------------>exception");
- log.error(ex.getMessage(), ex);
- }
- }
- }
- }
- protected void send(String toAddress) throws Exception {
- //HtmlEmail!
- Properties props = System.getProperties();
- props.put("mail.smtp.host", this.host);
- props.put("mail.smtp.auth","true");
- Session session = Session.getDefaultInstance(props);
- Message msg = new MimeMessage(session);
- MimeMultipart mmp=new MimeMultipart("related");
- MimeBodyPart mbp = new MimeBodyPart();
- mbp.setContent( this.htmlMsg, "text/html" );
- mmp.addBodyPart(mbp);
- msg.setContent(mmp);
- try {
- msg.setFrom(new InternetAddress(this.account));
- msg.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(toAddress, false));
- // -- We could include CC recipients too --
- msg.setSubject(subject);
- // msg.setText(this.htmlMsg);
- // -- Set some other header information --
- // msg.setHeader("X-Mailer", "LOTONtechEmail");
- // msg.setSentDate(new Date());
- // -- Send the message --
- Transport transport = session.getTransport("smtp");
- transport.connect(this.host,25,this.account,this.password);
- transport.sendMessage(msg,msg.getAllRecipients());
- log.info("Message sent OK.");
- } catch (MessagingException ex) {
- log.error(ex.getMessage(),ex);
- }
- log.info("send------------> " + toAddress);
- }
- public void setHost(String host) {
- if (host != null) {
- this.host = host.trim();
- }
- }
- public void setAccount(String account) {
- if (account != null) {
- this.account = account.trim();
- }
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public void setHtmlMsg(String htmlMsg) {
- String src="src="";
- String href="href="";
- htmlMsg=htmlMsg.replaceAll(src,src+url);
- htmlMsg=htmlMsg.replaceAll(href,href+url);
- // int n=-1;
- // while((n=htmlMsg.indexOf(src))>=0){
- // htmlMsg = htmlMsg.substring(0, n+5) + url +
- // htmlMsg.substring(n+6 + 1);
- // }
- // while((n=htmlMsg.indexOf(href))>=0){
- // htmlMsg = htmlMsg.substring(0, n+6) + url +
- // htmlMsg.substring(n+7 + 1);
- // }
- this.htmlMsg = htmlMsg;
- }
- }