`
aflyer
  • 浏览: 36729 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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

    博客分类:
  • JAVA
阅读更多

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

分享到:
评论
1 楼 aflyer 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.

相关推荐

    基于Java的邮件发送模板

    在Java开发中,发送邮件是一项常见的任务,尤其在企业级应用和服务中,它用于通知、确认订单、发送报告等。本篇文章将详细讲解如何利用Java实现邮件发送,并结合Apache的相关库来创建邮件发送模板,以提升效率和用户...

    创建一个Java电子邮件客户端

    在本文中,我们将深入探讨如何使用Java来创建一个基本的电子邮件客户端。首先,我们需要了解电子邮件的基本原理,电子邮件系统通常基于SMTP(Simple Mail Transfer Protocol)协议发送邮件,而使用IMAP(Internet ...

    HtmlEmail发送邮件+HttpClient下载功能

    1.支持发送邮件和远端文件下载两个功能Demo,环境MyEclipse 6.0.1+jdk1.6 2.import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache....

    JAVA 发送HTML格式并带图片的邮件实例

    通过上述步骤,你就可以使用Java成功发送HTML格式的邮件,并且可以附加图片或在邮件正文中嵌入图片。这在发送包含丰富格式和视觉元素的营销邮件或者通知邮件时非常有用。记得在实际项目中,根据具体需求调整代码,...

    Java HtmlEmail 邮件发送的简单实现代码

    使用HtmlEmail不仅可以发送纯文本,还能创建更丰富的格式化邮件,包括内联样式和图像。 首先,你需要在项目的pom.xml文件中引入Apache Commons Email的依赖: ```xml <groupId>org.apache.commons <artifactId>...

    android 下用org.apache.commons.mail.HtmlEmail发送邮件所需的jar包

    在Android平台上,发送HTML格式的电子邮件可以通过使用第三方库来实现,比如Apache Commons Email库中的`org.apache.commons.mail.HtmlEmail`类。这个类提供了一个方便的方式来构造和发送包含HTML内容的邮件。下面...

    java邮件发送例子 apache email demo

    Java邮件发送是一个常见的任务,特别是在企业级应用中用于发送通知、确认信息或自动通信。Apache Commons Email库是Java中一个强大的工具,它简化了构建和发送电子邮件的过程。在这个例子中,我们将深入探讨如何使用...

    java mail(邮件发送实例)

    Java Mail 是一个用于处理电子邮件的开源API,它允许Java开发者在应用程序中实现发送、接收和操作邮件的功能。在这个实例中,我们将深入探讨如何使用Java Mail API来发送邮件,特别是涉及中文处理和HTML格式的邮件。...

    java 发邮件例子

    在Java编程语言中,发送电子邮件是一项常见的任务,尤其在自动化通知、系统消息传递或用户注册验证等场景下。本教程将详细介绍如何使用`commons-email-1.1`库结合Java来实现邮件发送功能。 `commons-email`是Apache...

    java mail 支持text、html正文,多附件,发送、抄送、密送均多人

    Java Mail 是一个强大的Java库,用于处理电子邮件的发送和接收。这个库支持多种协议,如SMTP(简单邮件传输协议)和POP3(邮局协议),使得开发者能够在Java应用程序中实现复杂的邮件功能。在这个主题中,我们将深入...

    Java编写的邮件发送源文件

    使用Apache的SimpleEmail、HtmlEmail文件,实现了发送简单邮件,Html邮件和发送附件的功能

    邮件发送小例子

    总结起来,`HtmlEmail`类为我们提供了一种简单且高效的方式来发送HTML格式的邮件,这对于那些需要丰富格式和交互式内容的邮件来说非常有用。通过学习和实践这个小例子,你可以掌握这一关键的编程技巧,为你的应用...

    java发送邮件

    Java发送邮件是一个常见的任务,尤其在企业级应用中,用于通知、验证或其他自动化通信。Apache Commons Email库是Java中一个非常实用的工具,它简化了邮件发送的过程,提供了丰富的功能和错误处理。在这个主题中,...

    使用Apache-commons-email发送邮件

    在IT行业中,发送电子邮件是常见的任务,特别是在自动化脚本、通知系统或服务器...通过`SendMail.java`文件,我们可以学习到如何使用这个库来创建、配置和发送电子邮件,从而在项目中实现更高效、更可靠的邮件通信。

    使用 HtmlEmail 发送邮件 简单实例-附件资源

    使用 HtmlEmail 发送邮件 简单实例-附件资源

    快速创建适配所有设备和客户端的HTMLEmail模板

    4. **邮件安全字体**:由于跨平台字体支持的不一致性,使用Web字体可能会导致邮件在某些客户端中显示不正常。因此,建议使用广泛支持的系统字体,如Arial、Helvetica或Times New Roman。 5. **图片优化**:邮件中的...

    Apache Commons Mail 封装发送邮件类

    Apache Commons Mail 是一个Java库,它为发送电子邮件提供了一个简单且强大的API。这个库是对JavaMail API的一个方便的封装,简化了通过Java程序发送电子邮件的过程。在JavaMail中,配置和使用邮件服务可能相对复杂...

    使用commos-email实现简单的邮件发送

    在Java开发中,发送电子邮件是一项常见的任务,而Apache Commons Email库提供了一个强大的工具来简化这个过程。本篇文章将深入探讨如何使用Commons Email库来实现简单的邮件发送。 Apache Commons Email库是Apache ...

    java实现发送email小案例

    本篇文章将详细讲解如何使用Java实现一个简单的电子邮件发送案例。我们将关注以下关键知识点: 1. **JavaMail API**:JavaMail API 是Java平台上的标准库,用于处理电子邮件。它提供了丰富的接口和类来创建、发送和...

Global site tag (gtag.js) - Google Analytics