论坛首页 Java企业应用论坛

HtmlEmail方式STMP邮件发送为何用java客户端可以,用web方式调用就不行了呢?

浏览 7381 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-11-02  

我的HtmlEmail源文件是BBSCS-8的代码改写而成:

java 代码
  1. /**  
  2.  * Project name : RCP  
  3.  * Package name : com.sclh.rsp.registercenter.service.sendMail  
  4.  * @author      : fhway  
  5.  * DateTime     : 2007-11-1上午10:22:57  
  6.  * Vision       : 1.0  
  7.  * Company      : SCLH  
  8.  * CopyRight    (c) 2007    
  9.  */  
  10. package com.sclh.rsp.registercenter.service.sendmail;   
  11.   
  12. import java.io.File;   
  13. import java.io.StringWriter;   
  14. import java.util.ArrayList;   
  15. import java.util.HashMap;   
  16. import java.util.List;   
  17. import java.util.Locale;   
  18. import java.util.Map;   
  19.   
  20. import javax.mail.internet.InternetAddress;   
  21.   
  22. import org.apache.commons.mail.DefaultAuthenticator;   
  23. import org.apache.commons.mail.EmailException;   
  24. import org.apache.commons.mail.HtmlEmail;   
  25. import org.springframework.context.ApplicationContext;   
  26. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  27.   
  28. import freemarker.template.Configuration;   
  29. import freemarker.template.Template;   
  30.   
  31. /**  
  32.  * @author fhway  
  33.  *  
  34.  */  
  35. public class TemplateMail{   
  36.        
  37.     private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(this.getClass());   
  38.   
  39.     private static final String CHARSET = "UTF-8";   
  40.     private Configuration tempConfiguration = new Configuration();   
  41.     private HtmlEmail htmlEmail;   
  42.   
  43.     private String smtpAuth;   
  44.     private String smtpUser;   
  45.     private String smtpPasswd;   
  46.     private String smtpServer;   
  47.     private String smtpPort;   
  48.     private String senderEmail;   
  49.     private String ftlpath;   
  50.        
  51.     public void init() {   
  52.         this.htmlEmail = new HtmlEmail();   
  53.         if (this.getSmtpAuth().equals("1")) {   
  54.             DefaultAuthenticator defaultAuthenticator = new DefaultAuthenticator(   
  55.                     this.getSmtpUser(), this.getSmtpPasswd());   
  56.             this.getHtmlEmail().setAuthenticator(defaultAuthenticator);   
  57.         }   
  58.   
  59.         this.getHtmlEmail().setHostName(this.getSmtpServer());   
  60.         this.getHtmlEmail().setSmtpPort(new Integer(this.getSmtpPort()).intValue());   
  61.         try {   
  62.             this.getHtmlEmail().setFrom(this.getSenderEmail());   
  63.             this.getHtmlEmail().setTextMsg("Your email client does not support HTML messages");   
  64.             this.getHtmlEmail().setCharset(CHARSET);   
  65.   
  66.             File file = new File(this.getFtlpath());   
  67.   
  68.             System.out.println(file.getAbsolutePath());   
  69.                
  70.             this.getTempConfiguration().setDirectoryForTemplateLoading(new File(this.getFtlpath()));   
  71.                
  72. //      ClassPathResource cpr = new ClassPathResource(this.getFtlpath());   
  73. //      System.out.println(cpr.getFile());   
  74. //      this.getTempConfiguration().setDirectoryForTemplateLoading(cpr.getFile());   
  75.             this.getTempConfiguration().setDefaultEncoding(CHARSET);   
  76.             this.getTempConfiguration().setNumberFormat("0.##########");   
  77.         } catch (EmailException e) {   
  78.             // TODO Auto-generated catch block   
  79.             e.printStackTrace();   
  80.         } catch (Exception e) {   
  81.             log.error(e);   
  82.             e.printStackTrace();   
  83.         }   
  84.     }   
  85.   
  86.     public void sendMailFromTemplate(String to, String subject, String ftlName,   
  87.             Map root, Locale locale) {   
  88.         try {   
  89.             //this.init();   
  90.             this.getTempConfiguration().setLocale(locale);   
  91.   
  92.             this.getHtmlEmail().setSubject(subject);   
  93.             Template temp = this.getTempConfiguration().getTemplate(ftlName);   
  94.             StringWriter sw = new StringWriter();   
  95.             temp.process(root, sw);   
  96.             this.getHtmlEmail().setHtmlMsg(sw.toString());   
  97.             List list=new ArrayList();   
  98.             list.add(new InternetAddress(to));   
  99.             this.getHtmlEmail().setTo(list);   
  100.             this.getHtmlEmail().send();   
  101.             sw.flush();   
  102.         } catch (Exception e) {   
  103.             log.error(e);   
  104.             e.printStackTrace();   
  105.         }   
  106.     }   
  107.        
  108.     public String getSmtpAuth() {   
  109.         return smtpAuth;   
  110.     }   
  111.   
  112.     public void setSmtpAuth(String smtpAuth) {   
  113.         this.smtpAuth = smtpAuth;   
  114.     }   
  115.   
  116.     public String getSmtpPasswd() {   
  117.         return smtpPasswd;   
  118.     }   
  119.   
  120.     public void setSmtpPasswd(String smtpPasswd) {   
  121.         this.smtpPasswd = smtpPasswd;   
  122.     }   
  123.   
  124.     public String getSmtpPort() {   
  125.         return smtpPort;   
  126.     }   
  127.   
  128.     public void setSmtpPort(String smtpPort) {   
  129.         this.smtpPort = smtpPort;   
  130.     }   
  131.   
  132.     public String getSmtpServer() {   
  133.         return smtpServer;   
  134.     }   
  135.   
  136.     public void setSmtpServer(String smtpServer) {   
  137.         this.smtpServer = smtpServer;   
  138.     }   
  139.   
  140.     public String getSmtpUser() {   
  141.         return smtpUser;   
  142.     }   
  143.   
  144.     public void setSmtpUser(String smtpUser) {   
  145.         this.smtpUser = smtpUser;   
  146.     }   
  147.   
  148.     public HtmlEmail getHtmlEmail() {   
  149.         return htmlEmail;   
  150.     }   
  151.   
  152.     public void setHtmlEmail(HtmlEmail htmlEmail) {   
  153.         this.htmlEmail = htmlEmail;   
  154.     }   
  155.   
  156.     public Configuration getTempConfiguration() {   
  157.         return tempConfiguration;   
  158.     }   
  159.   
  160.     public void setTempConfiguration(Configuration tempConfiguration) {   
  161.         this.tempConfiguration = tempConfiguration;   
  162.     }   
  163.   
  164.     public String getSenderEmail() {   
  165.         return senderEmail;   
  166.     }   
  167.   
  168.     public void setSenderEmail(String senderEmail) {   
  169.         this.senderEmail = senderEmail;   
  170.     }   
  171.   
  172.     public String getFtlpath() {   
  173.         return ftlpath;   
  174.     }   
  175.   
  176.     public void setFtlpath(String ftlpath) {   
  177.         this.ftlpath = ftlpath;   
  178.     }   
  179.   
  180.     public static void main(String[] args) throws Exception {   
  181.         ApplicationContext context = new ClassPathXmlApplicationContext(   
  182.                 "applicationContext.xml");   
  183.         TemplateMail service = (TemplateMail) context   
  184.                 .getBean("templateMail");   
  185.   
  186.         Map root = new HashMap();   
  187.         root.put("website""website");   
  188.         root.put("title""website");   
  189.         root.put("detail""website");   
  190.         root.put("url""website");   
  191.            
  192.            
  193.         service.sendMailFromTemplate("fhway@163.com""subject""mailSend.ftl", root, new Locale("zh""CN"));   
  194.            
  195.         System.out.println("8888");   
  196.            
  197.     }   
  198.        
  199. }   

当我用本文件的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方式为何就不可以呢?

   发表时间:2007-11-02  
以下是http://commons.apache.org/email/userguide.html 的原话.然而我在文件中已经做了认证.
Authentication

If you need to authenticate to your SMTP server, you can call the setAuthentication(userName,password) method before sending your email. This will create an instance of DefaultAuthenticator which will be used by the JavaMail API when the email is sent. Your server must support RFC2554 in order for this to work.

You can perform a more complex authentication method such as displaying a dialog box to the user by creating a subclass of the javax.mail.Authenticator object. You will need to override the getPasswordAuthentication() method where you will handle collecting the user's information. To make use of your new Authenticator class, use the Email.setAuthenticator method.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics