- 浏览: 127109 次
- 性别:
- 来自: 成都
最新评论
-
di1984HIT:
学习了!!!
HP-UX中CLOSE_WAIT问题解决 -
swanky_yao:
问兄台两个问题:① 配置NIO和BIO的时候,配置有多大区别? ...
Apusic AS V5.1线程池配置和调优 -
swanky_yao:
^_^ 可以交流...
2006年年度应用服务器中间件产品评述 -
zylucky:
排版太乱了。。
webservice-之使用xfire开发(转自勇哥的BLOG) -
chen4059:
vi /etc/sysctl.conf
添加:
# Decre ...
HP-UX中CLOSE_WAIT问题解决
Apusic上用JavaMail发邮件
0. JavaMail基本介绍
JavaMail是属于J2EE框架中的一部分,主要是为简化Mail部分开发工作。使用JavaMail发送邮件需要以下步骤:
1)初始化Session实例;
在初始化Session实例中有两种方式:使用JNDI初始化和在代码中自行完成初始化。
★ 如果SMTP不需要认证,则不再做其他工作;
★ 如果SMTP需要认证,则确定在Session中提供嵌入认证信息,还是由3)Transport完成认证过程。
2)初始化Message实例,填充相关信息;
3)初始化Transport实例,连接到远程SMTP服务器,发送邮件。
在初始化Transport实例时也有两种情况:
★ 如果SMTP不需要认证,可以直接调用send()函数发送邮件,应用服务器会在后台调用connect()函数完成谁,进行邮件发送;
★ 如果SMTP需要认证,需要调用connect()函数,并提供认证需要的用户名/密码,才可以正确发送邮件。
1. javax.mail.Session的初始化
1.1. 使用JNDI初始化(配置JavaMail的JNDI)
在Apusic的J2EE应用中找到apusic-application.xml文件,增加<mail-session>部分,示例如下:
<apusic-application>
<module uri="web.war">
<web />
</module>
<mail-session>
<jndi-name>javamail/myMail</jndi-name>
<property name="mail.transport.protocol" value="smtp" />
<property name="mail.smtp.host" value="smtp.163.com" />
<property name="mail.smtp.user" value="user" />
<property name="mail.smtp.password" value="password" />
<property name="mail.smtp.auth" value="true" />
<property name="mail.from" value="user@163.com" />
</mail-session>
</apusic-application>
1.1.1. 通过JNDI找到JavaMail
1.1.1.1. 使用远程访问获得JavaMail
Hashtable env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.apusic.naming.jndi.CNContextFactory");
env.put(Context.PROVIDER_URL,"iiop://localhost:6888");
//插入相关验证信息
env.put(Context.SECURITY_CREDENTIALS,"admin" ) ;
env.put(Context.SECURITY_PRINCIPAL,"admin");
Context initCtx=new InitialContext(env);
System.out.println(initCtx.PROVIDER_URL);
//通过RMI 取得
Session myMailSession = (Session) initCtx.lookup("javamail/myMailNoAuth");
1.1.1.2. 使用Apusic默认方式获得JavaMail
Context initCtx = new InitialContext();
Session myMailSession = (Session) initCtx.lookup("javamail/myMailNoAuth");
System.out.println(myMailSession.getProperty("mail.smtp.host"));
1.1.2. 通过资源注入配置JavaMail
@Resource(mappedName = "javamail/myMailAuth", type = javax.mail.Session.class, shareable = true, authenticationType = Resource.AuthenticationType.CONTAINER, description = "my email with auth")
private Session myAuthMailSession;
或者
@Resource(mappedName = "javamail/myMailAuth")
private Session myAuthMailSession;
1.2. 在代码中初始化
1.2.1. 无须认证的初始化
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.host", "localhost");
Session myMailSession = Session.getInstance(props);
1.2.2. 需要认证的初始化(Session实例构造时提供认证支持)
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com");
Session myMailSession = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password");} });
1.2.3. 需要认证的初始化(Session配置参数提供认证支持)
myMailSession.setPasswordAuthentication(
new URLName(props.getProperty("mail.transport.protocol")+"://"+
props.getProperty("mail.smtp.user")+"@"+
props.getProperty("mail.smtp.host")),
new PasswordAuthentication(props.getProperty("mail.smtp.user"),
props.getProperty("mail.smtp.password")));
1.2.4. 需要认证的初始化(Transport配置参数提供认证支持)
// 发送邮件
javax.mail.Transport myTransport = myMailSession.getTransport("smtp");
myTransport.connect("smtp.163.com", "user", "password");
myTransport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
myTransport.close();
1.4. 对Session调试的配置
● 可以在mail-session中加入<property name="mail.debug" value="true" />
● 可以在Session初始化前加入props.put("mail.debug", "true");
● 可以在Session初始化后,通过代码控制myMailSession.setDebug(true);
1.5. Properties的解释
◆ mail.store.protocol:用于检索邮件的协议
◆ mail.transport.protocol:用于传送邮件的协议
◆ mail.host:默认的主机名,默认是本地计算机。
◆ mail.user:默认的用户名。
◆ mail.from:默认的返回地址。
◆ mail.debug:是否输出DEBUG信息。默认为False。
◆ mail.protocol.host:指定协议的主机名,没有指定就用mail.host。例如:mail.smtp.host=smtp.163.com
◆ mail.protocol.user:指定协议的用户名,没有指定就用mail.user。例如:mail.smtp.user=user
◆ mail.protocol.from:指定协议的返回地址,没有指定就用mail.from。例如:mail.smtp.from=user@163.com
◆ mail.smtp.auth:如果是True,就会登录SMTP服务器,获得授权才能发送邮件。默认为False。
2. 通过JavaMail发送邮件
2.1. 通过无须认证的SMTP发邮件
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.host", "localhost");
try {
Session myMailSession = Session.getInstance(props);
myMailSession.setDebug(true); // 打开DEBUG模式
Message msg = new MimeMessage(myMailSession);
msg.setFrom(new InternetAddress("user@163.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("zhuyuanxiang@apusic.com"));
msg.setContent("I have a email test!", "text/plain");
msg.setSentDate(new java.util.Date());
msg.setSubject("Test");
msg.setText("This is a test!\n");
System.out.println("1.Please wait for sending one...");
// 发送邮件
// javax.mail.Transport.send(msg); // 与下面四行的功能一样
javax.mail.Transport myTransport = myMailSession.getTransport("smtp");
myTransport.connect();
myTransport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
myTransport.close();
System.out.println("2.Your message had send!");
} catch (Exception error) {
System.out.println("*.I am sorry to tell you the fail for " + error);
}
注:测试可以使用Microsft IIS SMTP 服务,安装好启动服务后,还需要进入“IIS管理器”,增加一个“远程域”,对于“远程域”的“出站安全性”需要把用户名和密码加上(就是平时发邮件时登录用的用户名/密码),否则邮件发不出去。
2.2. 通过需要认证的SMTP发邮件
2.2.1. 需要认证的初始化(Session实例构造时提供认证支持)
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com");
try {
Session myMailSession = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password");
}
});
myMailSession.setDebug(true); // 打开DEBUG模式
InternetAddress address = new InternetAddress("user@163.com");
String message = "I have a email test!";
Message msg = new MimeMessage(myMailSession);
msg.setFrom(address);
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("zhuyuanxiang@apusic.com"));
msg.setContent(message, "text/plain");
msg.setSentDate(new java.util.Date());
msg.setSubject("Test");
msg.setText("This is a test!\n");
out.println("1.Please wait for sending...");
// 发送邮件
javax.mail.Transport myTransport = myMailSession.getTransport("smtp");
myTransport.connect();
myTransport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
myTransport.close();
// javax.mail.Transport.send(msg); // 注释上四行,打开这行代码,功能一样
out.println("2.Your message had send!");
} catch (Exception error) {
out.println("*.I am sorry to tell you the fail for " + error);
}
2.2.2. 需要认证的初始化(Session配置参数提供认证支持)
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com");
try {
Session myMailSession = Session.getInstance(props);
myMailSession.setDebug(true); // 打开DEBUG模式
myAuthMailSession.setPasswordAuthentication(
new URLName("smtp://user@host", new PasswordAuthentication("user","password"));
InternetAddress address = new InternetAddress("user@163.com");
String message = "I have a email test!";
Message msg = new MimeMessage(myMailSession);
msg.setFrom(address);
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("zhuyuanxiang@apusic.com"));
msg.setContent(message, "text/plain");
msg.setSentDate(new java.util.Date());
msg.setSubject("Test");
msg.setText("This is a test!\n");
out.println("1.Please wait for sending...");
// 发送邮件
javax.mail.Transport myTransport = myMailSession.getTransport("smtp");
myTransport.connect();
myTransport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
myTransport.close();
// javax.mail.Transport.send(msg); // 注释上四行,打开这行代码,功能一样
out.println("2.Your message had send!");
} catch (Exception error) {
out.println("*.I am sorry to tell you the fail for " + error);
}
2.2.3. 需要认证的初始化(Transport配置参数提供认证支持)
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
try {
Session myMailSession = Session.getInstance(props);
myMailSession.setDebug(true); // 打开DEBUG模式
Message msg = new MimeMessage(myMailSession);
msg.setFrom(new InternetAddress("user@163.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("zhuyuanxiang@apusic.com"));
msg.setContent("I have a email test!", "text/plain");
msg.setSentDate(new java.util.Date());
msg.setSubject("Test");
msg.setText("This is a test!\n");
System.out.println("1.Please wait for sending two...");
// 发送邮件
javax.mail.Transport myTransport = myMailSession.getTransport("smtp");
myTransport.connect("smtp.163.com", "user", "password");
myTransport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
myTransport.close();
// javax.mail.Transport.send(msg); // 这行不能使用。
System.out.println("2.Your message had send!");
} catch (Exception error) {
System.out.println("*.I am sorry to tell you the fail for " + error);
}
}
注:资源注入的Session发送邮件时:
无须认证的SMTP,可以参考2.1.
需要认证的SMTP,可以参考2.2.1.
参考:
http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
http://commons.apache.org/email/userguide.html
http://www.blogjava.net/Unmi/archive/2007/06/07/124179.html
http://unmi.blogcn.com/diary,117630488.shtml
Apusic Stduio打包的示例工程。
http://zhuyuanxiang.iteye.com/topics/download/d6f5c3a9-9cd9-30be-9b64-9e311900b304
备注:本文耗时1周完成。
- sendEmail.rar (18.9 KB)
- 描述: Apusic Studio打包的示例工程。
- 下载次数: 23
评论
另:Apusic Studio工程没有更新,如果自己修改代码测试Session配置参数进行认证失败,可以在此继续交流。
对“隔叶黄莺”提供的无私帮助在此表示感谢。
发表评论
-
在Tomcat上开发Web应用如何保证兼容性
2008-08-14 07:43 1495最近,协助伙伴将Tomcat ... -
Apusic Operamasks的资源注入
2008-07-30 23:58 2006★ 什么是资源?开发过程中与业务无关的服务、类的实例或者变量的 ... -
Spring ImageDB在Apusic上部署
2008-07-19 15:30 1575目标:Spring的ImageDB例子 ... -
Spring Countries在Apusic上部署
2008-07-15 13:45 1725目标: Spring的Countries例子在Apusic V ... -
Struts的例子在Apusic V5.1上的部署记录
2008-07-10 23:02 1775目标:将Struts提供的例子部署到Apusic V5.1上, ... -
经典框架在Apusic V5.1下部署文章撰写说明
2008-07-10 23:01 2507为了帮助越来越多的Apusic爱好者和使用者更加顺利的使用Ap ... -
Tomcat V6 Examples移植到Apusic V5.1
2008-07-07 17:14 2405目标:将Tomcat V6的的例 ... -
HP-UX+JDK+Apusic+SQL-Server的调优记录
2008-07-04 17:39 2517最近的项目,面临情况如下:1. 并发压力大。平时并发连接大约在 ... -
Apusic AS V5.1线程池配置和调优
2008-06-25 12:32 2465“没有最优,只有平衡”。因此在配置与调优之前,我总喜欢先介绍理 ... -
HP-UX中CLOSE_WAIT问题解决
2008-06-20 07:58 3858作者一直被某个项目的HP-UX折磨,系统运行一段时间后就会出现 ... -
Apusic AS的Web应用中调用commons-logging的流程
2008-06-14 18:09 1289上文描述了如何在Apusic AS的Web应用中调用Log4J ... -
Apusic AS的Web应用中调用Log4J的流程
2008-06-13 22:50 1189经常碰到项目中用Log4J ... -
J2EE应用服务器Apusic AS在HP-UX上调优时使用的工具列表
2008-05-24 23:10 2254开发人员写程序一般都在Windows下,主要还是方便。但是许多 ... -
2006年年度应用服务器中间件产品评述
2007-06-05 12:09 1496本次评述纯属个人意见,请不要转载,也不要抄袭,谢谢。 BEA ... -
基于Apusic的J2EE学习计划
2007-06-07 19:51 10311. Apusic Studio 等开发工 ... -
Struts_Gossip学习笔记
2007-06-18 14:52 878为了不侵犯别人的版权,因此我就不提供下载的文件了,大家可以直接 ... -
Hibernate_Gossip学习笔记
2007-06-19 09:39 720* 米小國 * 奇想世界☆. ... -
Apusic 5.0正式版发布了
2007-07-12 18:33 1693全球第四家通过Java EE 5.0认证,完整实现标准要求的A ... -
Eclipse精粹学习笔记
2007-07-16 10:23 2578David Carlson著,张欣译, ... -
JRE与JDK在启动应用服务器中的作用.txt
2007-07-23 15:00 1145开始接触Java的时候觉得Java虚拟机很麻烦,不明白 ...
相关推荐
卸载、管理与使用等基本操作,为用户快速使用本产品提供指导。 金蝶 Apusic 应用服务器为复杂应用提供了一个简便、快速的开发和运行平台,对于分 布式的企业级应用,提供了易扩展、可伸缩和高安全性等特性。下面将...
金蝶Apusic应用服务器6.0在5.0的基础上进行了大量扩充和优化,在产品功能、性能、安全性、扩展性及兼容性等方面得到显著提升。金蝶Apusic应用服务器6.0大大简化了创建和管理Java EE应用的任务,并为之提供了可伸缩、...
Apusic密码修改指引
金蝶中间件(Apusic)--license
Apusic 7.0,2014,license,
Apusic5.1域配置,希望对正在使用Apusic5.1服务器的工作团队有所帮助。
金蝶中间件Apusic Eclipse/MyEclipse all-plugins,用于集成在eclipse做调试开发
管理Apusic Web服务器,管理Apusic Web服务器
2021年通用apusic license,适用于apusic9.0以上EAS(EAS8.0以上版本一般都可用),有效期到2021年12月31日。此apusic为5个连接数
EAS Apusic2016年的临时许可
apusic文件,apuasic是国产中间件,隶属金蝶 中国. 数据库连接攻略
Apusic+SQL-Server的调优记录
EAS apusic5.0 许可.许可到期时间2088年!
Apusic OperaMasks很全的JSF的例子,什么都有,如:TREE 、GRID、FORM、BOX、MENU、DIALOG、AJAX。都很漂亮的。
使用Jprofiler监控Apusic应用服务器
linux环境下使用apache和apusic实现负载均衡
Windows2003+Apusic6.0安装部署手册
金蝶Apusic企业服务总线V7.0用户手册
2019年通用apusic license,适用于apusic9.0以上EAS,有效期到2019年12月10日