- 浏览: 36650 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
mapecun:
您好,我模仿你的代码演示了一下,在最后的现实页面出现OpLog ...
JFreeChart1.0.6的一个通用Web Demo -
iandvisble:
JFreeChart入门资料 -
769034965:
确实不错,很好,谢谢
JBoss下EJB3.0实例教程(黎活明) -
風一樣的男子:
今天发现 Apusic 5 下跑 EL 都报错 org.ope ...
Webservices与JFreeChart的gnujaxp.jar冲突! -
livingbody:
太有才了,我都没看明白你说的是什么
JBPM的MySQL文和迁移到Tomcat的说明
我的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方式为何就不可以呢?
评论
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.
发表评论
-
JBPM的MySQL文和迁移到Tomcat的说明
2008-03-07 13:01 1875以下是3.2的mysql文 create table JBPM ... -
图说 Eclipse下 Seam Project 构建
2008-01-29 10:53 2730Seam Project 构建 环境: wtp-all-in- ... -
JBoss下EJB3.0实例教程(黎活明)
2007-11-16 15:59 4129EJB3.0实例教程,觉得实在不错! -
java中相对路径,绝对路径问题总结
2007-11-02 12:28 2953注意: 1. 此文未经验证; ... -
JFreeChart1.0.6的一个通用Web Demo
2007-10-24 09:43 2833这是我自己做的一个关于统计的WebDemo; 首先建立一个we ... -
JFreeChart入门资料
2007-10-18 09:36 3631这段时间学习JFreeChart,在网上收集了一些资料作为参考 ...
相关推荐
在Java开发中,发送邮件是一项常见的任务,尤其在企业级应用和服务中,它用于通知、确认订单、发送报告等。本篇文章将详细讲解如何利用Java实现邮件发送,并结合Apache的相关库来创建邮件发送模板,以提升效率和用户...
在本文中,我们将深入探讨如何使用Java来创建一个基本的电子邮件客户端。首先,我们需要了解电子邮件的基本原理,电子邮件系统通常基于SMTP(Simple Mail Transfer Protocol)协议发送邮件,而使用IMAP(Internet ...
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格式的邮件,并且可以附加图片或在邮件正文中嵌入图片。这在发送包含丰富格式和视觉元素的营销邮件或者通知邮件时非常有用。记得在实际项目中,根据具体需求调整代码,...
使用HtmlEmail不仅可以发送纯文本,还能创建更丰富的格式化邮件,包括内联样式和图像。 首先,你需要在项目的pom.xml文件中引入Apache Commons Email的依赖: ```xml <groupId>org.apache.commons <artifactId>...
在Android平台上,发送HTML格式的电子邮件可以通过使用第三方库来实现,比如Apache Commons Email库中的`org.apache.commons.mail.HtmlEmail`类。这个类提供了一个方便的方式来构造和发送包含HTML内容的邮件。下面...
Java邮件发送是一个常见的任务,特别是在企业级应用中用于发送通知、确认信息或自动通信。Apache Commons Email库是Java中一个强大的工具,它简化了构建和发送电子邮件的过程。在这个例子中,我们将深入探讨如何使用...
Java Mail 是一个用于处理电子邮件的开源API,它允许Java开发者在应用程序中实现发送、接收和操作邮件的功能。在这个实例中,我们将深入探讨如何使用Java Mail API来发送邮件,特别是涉及中文处理和HTML格式的邮件。...
在Java编程语言中,发送电子邮件是一项常见的任务,尤其在自动化通知、系统消息传递或用户注册验证等场景下。本教程将详细介绍如何使用`commons-email-1.1`库结合Java来实现邮件发送功能。 `commons-email`是Apache...
Java Mail 是一个强大的Java库,用于处理电子邮件的发送和接收。这个库支持多种协议,如SMTP(简单邮件传输协议)和POP3(邮局协议),使得开发者能够在Java应用程序中实现复杂的邮件功能。在这个主题中,我们将深入...
使用Apache的SimpleEmail、HtmlEmail文件,实现了发送简单邮件,Html邮件和发送附件的功能
总结起来,`HtmlEmail`类为我们提供了一种简单且高效的方式来发送HTML格式的邮件,这对于那些需要丰富格式和交互式内容的邮件来说非常有用。通过学习和实践这个小例子,你可以掌握这一关键的编程技巧,为你的应用...
Java发送邮件是一个常见的任务,尤其在企业级应用中,用于通知、验证或其他自动化通信。Apache Commons Email库是Java中一个非常实用的工具,它简化了邮件发送的过程,提供了丰富的功能和错误处理。在这个主题中,...
在IT行业中,发送电子邮件是常见的任务,特别是在自动化脚本、通知系统或服务器...通过`SendMail.java`文件,我们可以学习到如何使用这个库来创建、配置和发送电子邮件,从而在项目中实现更高效、更可靠的邮件通信。
使用 HtmlEmail 发送邮件 简单实例-附件资源
4. **邮件安全字体**:由于跨平台字体支持的不一致性,使用Web字体可能会导致邮件在某些客户端中显示不正常。因此,建议使用广泛支持的系统字体,如Arial、Helvetica或Times New Roman。 5. **图片优化**:邮件中的...
Apache Commons Mail 是一个Java库,它为发送电子邮件提供了一个简单且强大的API。这个库是对JavaMail API的一个方便的封装,简化了通过Java程序发送电子邮件的过程。在JavaMail中,配置和使用邮件服务可能相对复杂...
在Java开发中,发送电子邮件是一项常见的任务,而Apache Commons Email库提供了一个强大的工具来简化这个过程。本篇文章将深入探讨如何使用Commons Email库来实现简单的邮件发送。 Apache Commons Email库是Apache ...
本篇文章将详细讲解如何使用Java实现一个简单的电子邮件发送案例。我们将关注以下关键知识点: 1. **JavaMail API**:JavaMail API 是Java平台上的标准库,用于处理电子邮件。它提供了丰富的接口和类来创建、发送和...