我的HtmlEmail源文件是BBSCS-8的代码改写而成:
- /**
- * Project name : RCP
- * Package name : com.sclh.rsp.registercenter.service.sendMail
- * @author : fhway
- * DateTime : 2007-11-1上午10:22:57
- * Vision : 1.0
- * Company : SCLH
- * CopyRight (c) 2007
- */
- package com.sclh.rsp.registercenter.service.sendmail;
- import java.io.File;
- import java.io.StringWriter;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Locale;
- import java.util.Map;
- import javax.mail.internet.InternetAddress;
- import org.apache.commons.mail.DefaultAuthenticator;
- import org.apache.commons.mail.EmailException;
- import org.apache.commons.mail.HtmlEmail;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import freemarker.template.Configuration;
- import freemarker.template.Template;
- /**
- * @author fhway
- *
- */
- public class TemplateMail{
- private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(this.getClass());
- private static final String CHARSET = "UTF-8";
- private Configuration tempConfiguration = new Configuration();
- private HtmlEmail htmlEmail;
- private String smtpAuth;
- private String smtpUser;
- private String smtpPasswd;
- private String smtpServer;
- private String smtpPort;
- private String senderEmail;
- private String ftlpath;
- public void init() {
- this.htmlEmail = new HtmlEmail();
- if (this.getSmtpAuth().equals("1")) {
- DefaultAuthenticator defaultAuthenticator = new DefaultAuthenticator(
- this.getSmtpUser(), this.getSmtpPasswd());
- this.getHtmlEmail().setAuthenticator(defaultAuthenticator);
- }
- this.getHtmlEmail().setHostName(this.getSmtpServer());
- this.getHtmlEmail().setSmtpPort(new Integer(this.getSmtpPort()).intValue());
- try {
- this.getHtmlEmail().setFrom(this.getSenderEmail());
- this.getHtmlEmail().setTextMsg("Your email client does not support HTML messages");
- this.getHtmlEmail().setCharset(CHARSET);
- File file = new File(this.getFtlpath());
- System.out.println(file.getAbsolutePath());
- this.getTempConfiguration().setDirectoryForTemplateLoading(new File(this.getFtlpath()));
- // ClassPathResource cpr = new ClassPathResource(this.getFtlpath());
- // System.out.println(cpr.getFile());
- // this.getTempConfiguration().setDirectoryForTemplateLoading(cpr.getFile());
- this.getTempConfiguration().setDefaultEncoding(CHARSET);
- this.getTempConfiguration().setNumberFormat("0.##########");
- } catch (EmailException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (Exception e) {
- log.error(e);
- e.printStackTrace();
- }
- }
- public void sendMailFromTemplate(String to, String subject, String ftlName,
- Map root, Locale locale) {
- try {
- //this.init();
- this.getTempConfiguration().setLocale(locale);
- this.getHtmlEmail().setSubject(subject);
- Template temp = this.getTempConfiguration().getTemplate(ftlName);
- StringWriter sw = new StringWriter();
- temp.process(root, sw);
- this.getHtmlEmail().setHtmlMsg(sw.toString());
- List list=new ArrayList();
- list.add(new InternetAddress(to));
- this.getHtmlEmail().setTo(list);
- this.getHtmlEmail().send();
- sw.flush();
- } catch (Exception e) {
- log.error(e);
- e.printStackTrace();
- }
- }
- public String getSmtpAuth() {
- return smtpAuth;
- }
- public void setSmtpAuth(String smtpAuth) {
- this.smtpAuth = smtpAuth;
- }
- public String getSmtpPasswd() {
- return smtpPasswd;
- }
- public void setSmtpPasswd(String smtpPasswd) {
- this.smtpPasswd = smtpPasswd;
- }
- public String getSmtpPort() {
- return smtpPort;
- }
- public void setSmtpPort(String smtpPort) {
- this.smtpPort = smtpPort;
- }
- public String getSmtpServer() {
- return smtpServer;
- }
- public void setSmtpServer(String smtpServer) {
- this.smtpServer = smtpServer;
- }
- public String getSmtpUser() {
- return smtpUser;
- }
- public void setSmtpUser(String smtpUser) {
- this.smtpUser = smtpUser;
- }
- public HtmlEmail getHtmlEmail() {
- return htmlEmail;
- }
- public void setHtmlEmail(HtmlEmail htmlEmail) {
- this.htmlEmail = htmlEmail;
- }
- public Configuration getTempConfiguration() {
- return tempConfiguration;
- }
- public void setTempConfiguration(Configuration tempConfiguration) {
- this.tempConfiguration = tempConfiguration;
- }
- public String getSenderEmail() {
- return senderEmail;
- }
- public void setSenderEmail(String senderEmail) {
- this.senderEmail = senderEmail;
- }
- public String getFtlpath() {
- return ftlpath;
- }
- public void setFtlpath(String ftlpath) {
- this.ftlpath = ftlpath;
- }
- public static void main(String[] args) throws Exception {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- TemplateMail service = (TemplateMail) context
- .getBean("templateMail");
- Map root = new HashMap();
- root.put("website", "website");
- root.put("title", "website");
- root.put("detail", "website");
- root.put("url", "website");
- service.sendMailFromTemplate("fhway@163.com", "subject", "mailSend.ftl", root, new Locale("zh", "CN"));
- System.out.println("8888");
- }
- }
当我用本文件的main测试时,邮件能够正确发送.但是当我部署到web上时变出现以下错误:
2007-11-02 14:52:55,953 [com.sclh.rsp.registercenter.service.sendmail.SendMailTi
merTask]-[ERROR] sendmail start .....
Hibernate: select oploggerad0_.ID as ID18_, oploggerad0_.MESSAGE as MESSAGE18_,
oploggerad0_.USERID as USERID18_, oploggerad0_.TYPE as TYPE18_, oploggerad0_.DBT
YPE as DBTYPE18_, oploggerad0_.REMOTEADDR as REMOTEADDR18_, oploggerad0_.ISLOOK
as ISLOOK18_, oploggerad0_.SYSTEMTIME as SYSTEMTIME18_, oploggerad0_.DELFLAG as
DELFLAG18_ from RSC.OPLOGGER_ADVICE oploggerad0_ where (delflag='0' ) order by
to_number(id)
Hibernate: select userinfo0_.ID as ID6_3_, userinfo0_.LOGINID as LOGINID6_3_, us
erinfo0_.PASSWORD as PASSWORD6_3_, userinfo0_.NAME as NAME6_3_, userinfo0_.RIGHT
ID as RIGHTID6_3_, userinfo0_.DEPARTID as DEPARTID6_3_, userinfo0_.SYSTEMTIME as
SYSTEMTIME6_3_, userinfo0_.DELFLAG as DELFLAG6_3_, userright1_.ID as ID10_0_, u
serright1_.NAME as NAME10_0_, userright1_.ALIAS as ALIAS10_0_, userright1_.COMM
as COMM10_0_, userright1_.SYSTEMTIME as SYSTEMTIME10_0_, userright1_.DELFLAG as
DELFLAG10_0_, departinfo2_.ID as ID0_1_, departinfo2_.NAME as NAME0_1_, departin
fo2_.URL as URL0_1_, departinfo2_.RELATIONSUMMARY1 as RELATION4_0_1_, departinfo
2_.RELATIONMAN1 as RELATION5_0_1_, departinfo2_.RELATIONTEL1 as RELATION6_0_1_,
departinfo2_.RELATIONEMAIL1 as RELATION7_0_1_, departinfo2_.RELATIONSUMMARY2 as
RELATION8_0_1_, departinfo2_.RELATIONMAN2 as RELATION9_0_1_, departinfo2_.RELATI
ONTEL2 as RELATIO10_0_1_, departinfo2_.RELATIONEMAIL2 as RELATIO11_0_1_, departi
nfo2_.ORGANTYPEID as ORGANTY12_0_1_, departinfo2_.SYSTEMTIME as SYSTEMTIME0_1_,
departinfo2_.DELFLAG as DELFLAG0_1_, organtype3_.ID as ID11_2_, organtype3_.NAME
as NAME11_2_, organtype3_.ALIAS as ALIAS11_2_, organtype3_.COMM as COMM11_2_, o
rgantype3_.SYSTEMTIME as SYSTEMTIME11_2_, organtype3_.DELFLAG as DELFLAG11_2_ fr
om RSC.USERINFO userinfo0_ left outer join RSC.USER_RIGHT userright1_ on userinf
o0_.RIGHTID=userright1_.ID left outer join RSC.DEPARTINFO departinfo2_ on userin
fo0_.DEPARTID=departinfo2_.ID left outer join RSC.ORGANTYPE organtype3_ on depar
tinfo2_.ORGANTYPEID=organtype3_.ID where userinfo0_.ID=?
Hibernate: select oploggerty0_.ID as ID19_0_, oploggerty0_.NAME as NAME19_0_, op
loggerty0_.ALIAS as ALIAS19_0_, oploggerty0_.COMM as COMM19_0_, oploggerty0_.SYS
TEMTIME as SYSTEMTIME19_0_, oploggerty0_.DELFLAG as DELFLAG19_0_ from RSC.OPLOGG
ER_TYPE oploggerty0_ where oploggerty0_.ID=?
Hibernate: select oploggerdb0_.ID as ID20_0_, oploggerdb0_.NAME as NAME20_0_, op
loggerdb0_.ALIAS as ALIAS20_0_, oploggerdb0_.COMM as COMM20_0_, oploggerdb0_.SYS
TEMTIME as SYSTEMTIME20_0_, oploggerdb0_.DELFLAG as DELFLAG20_0_ from RSC.OPLOGG
ER_DBTYPE oploggerdb0_ where oploggerdb0_.ID=?
Hibernate: select departloca0_.ID as ID17_, departloca0_.NAME as NAME17_, depart
loca0_.ORGANTYPEID as ORGANTYP3_17_, departloca0_.URL as URL17_, departloca0_.SY
STEMTIME as SYSTEMTIME17_, departloca0_.DELFLAG as DELFLAG17_ from RSC.DEPART_LO
CALHOST departloca0_ where (delflag='0' )
Hibernate: select departinfo0_.ID as ID0_1_, departinfo0_.NAME as NAME0_1_, depa
rtinfo0_.URL as URL0_1_, departinfo0_.RELATIONSUMMARY1 as RELATION4_0_1_, depart
info0_.RELATIONMAN1 as RELATION5_0_1_, departinfo0_.RELATIONTEL1 as RELATION6_0_
1_, departinfo0_.RELATIONEMAIL1 as RELATION7_0_1_, departinfo0_.RELATIONSUMMARY2
as RELATION8_0_1_, departinfo0_.RELATIONMAN2 as RELATION9_0_1_, departinfo0_.RE
LATIONTEL2 as RELATIO10_0_1_, departinfo0_.RELATIONEMAIL2 as RELATIO11_0_1_, dep
artinfo0_.ORGANTYPEID as ORGANTY12_0_1_, departinfo0_.SYSTEMTIME as SYSTEMTIME0_
1_, departinfo0_.DELFLAG as DELFLAG0_1_, organtype1_.ID as ID11_0_, organtype1_.
NAME as NAME11_0_, organtype1_.ALIAS as ALIAS11_0_, organtype1_.COMM as COMM11_0
_, organtype1_.SYSTEMTIME as SYSTEMTIME11_0_, organtype1_.DELFLAG as DELFLAG11_0
_ from RSC.DEPARTINFO departinfo0_ left outer join RSC.ORGANTYPE organtype1_ on
departinfo0_.ORGANTYPEID=organtype1_.ID where departinfo0_.ID=?
-------to:--------fhway@system.mail
-------subject:--------异常日志警报文件
2007-11-02 14:52:56,218 [com.sclh.rsp.registercenter.service.sendmail.TemplateMa
il]-[ERROR] org.apache.commons.mail.EmailException: Sending the email to the fol
lowing server failed : smtp.163.com:25
org.apache.commons.mail.EmailException: Sending the email to the following serve
r failed : smtp.163.com:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:873)
at org.apache.commons.mail.Email.send(Email.java:898)
at com.sclh.rsp.registercenter.service.sendmail.TemplateMail.sendMailFro
mTemplate(TemplateMail.java:105)
at com.sclh.rsp.registercenter.service.sendmail.SendMailTimerTask.run(Se
ndMailTimerTask.java:66)
at java.util.TimerThread.mainLoop(Timer.java:432)
at java.util.TimerThread.run(Timer.java:382)
Caused by: javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:611)
at javax.mail.Session.getTransport(Session.java:541)
at javax.mail.Session.getTransport(Session.java:484)
at javax.mail.Session.getTransport(Session.java:464)
at javax.mail.Session.getTransport(Session.java:519)
at javax.mail.Transport.send0(Transport.java:155)
at javax.mail.Transport.send(Transport.java:81)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:863)
... 5 more
-------to:--------fhway@system.mail
-------subject:--------异常日志警报文件
2007-11-02 14:52:56,296 [com.sclh.rsp.registercenter.service.sendmail.TemplateMa
il]-[ERROR] org.apache.commons.mail.EmailException: Sending the email to the fol
lowing server failed : smtp.163.com:25
org.apache.commons.mail.EmailException: Sending the email to the following serve
r failed : smtp.163.com:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:873)
at org.apache.commons.mail.Email.send(Email.java:898)
at com.sclh.rsp.registercenter.service.sendmail.TemplateMail.sendMailFro
mTemplate(TemplateMail.java:105)
at com.sclh.rsp.registercenter.service.sendmail.SendMailTimerTask.run(Se
ndMailTimerTask.java:66)
at java.util.TimerThread.mainLoop(Timer.java:432)
at java.util.TimerThread.run(Timer.java:382)
Caused by: javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:611)
at javax.mail.Session.getTransport(Session.java:541)
at javax.mail.Session.getTransport(Session.java:484)
at javax.mail.Session.getTransport(Session.java:464)
at javax.mail.Session.getTransport(Session.java:519)
at javax.mail.Transport.send0(Transport.java:155)
at javax.mail.Transport.send(Transport.java:81)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:863)
... 5 more
2007-11-02 14:52:56,296 [com.sclh.rsp.registercenter.service.sendmail.SendMailTi
merTask]-[ERROR] sendmail end .....
始终不明白为何会出现这种情况.main方法可以通过认证,而web方式为何就不可以呢?