﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-Justin.Lu----Position-文章分类-Java</title><link>http://www.cnitblog.com/Justin/category/625.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 01 Oct 2011 03:25:48 GMT</lastBuildDate><pubDate>Sat, 01 Oct 2011 03:25:48 GMT</pubDate><ttl>60</ttl><item><title>[JAVA100例]048、使用JavaMail發送郵件</title><link>http://www.cnitblog.com/Justin/articles/2598.html</link><dc:creator>Justin</dc:creator><author>Justin</author><pubDate>Sun, 11 Sep 2005 14:33:00 GMT</pubDate><guid>http://www.cnitblog.com/Justin/articles/2598.html</guid><wfw:comment>http://www.cnitblog.com/Justin/comments/2598.html</wfw:comment><comments>http://www.cnitblog.com/Justin/articles/2598.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Justin/comments/commentRss/2598.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Justin/services/trackbacks/2598.html</trackback:ping><description><![CDATA[<P><FONT size=1>import java.util.*;<BR>import java.io.*;<BR>import javax.mail.*;<BR>import javax.mail.internet.*;<BR>import javax.activation.*;</FONT></P>
<P><FONT size=1>/**<BR>&nbsp;* </FONT><FONT size=1>Title: 使用javamail发送邮件</FONT><BR><FONT size=1>&nbsp;* </FONT><FONT size=1>Description: 演示如何使用javamail包发送电子邮件。这个实例可发送多附件</FONT><BR><FONT size=1>&nbsp;* </FONT><FONT size=1>Copyright: Copyright (c) 2003</FONT><BR><FONT size=1>&nbsp;* </FONT><FONT size=1>Filename: Mail.java</FONT><BR><FONT size=1>&nbsp;* @version 1.0<BR>&nbsp;*/<BR>public class Mail {</FONT> <BR><BR><FONT size=1>String to = "";//收件人<BR>String from = "";//发件人<BR>String host = "";//smtp主机<BR>String username = "" ;<BR>String password = "" ;<BR>String filename = "";//附件文件名<BR>String subject = "";//邮件主题<BR>String content = "";//邮件正文<BR>Vector file = new Vector();//附件文件集合<BR>/**<BR>&nbsp;*方法说明：默认构造器<BR>&nbsp;*输入参数：<BR>&nbsp;*返回类型：<BR>&nbsp;*/<BR>public Mail(){<BR>}<BR>/**<BR>&nbsp;*方法说明：构造器，提供直接的参数传入<BR>&nbsp;*输入参数：<BR>&nbsp;*返回类型：<BR>&nbsp;*/<BR>public Mail(String to,String from,String smtpServer,String username,String password,String subject,String content){<BR>&nbsp; this.to = to;<BR>&nbsp; this.from = from;<BR>&nbsp; this.host = smtpServer;<BR>&nbsp; this.username = username;<BR>&nbsp; this.password = password;<BR>&nbsp; this.subject = subject;<BR>&nbsp; this.content = content;<BR>}<BR>/**<BR>&nbsp;*方法说明：设置邮件服务器地址<BR>&nbsp;*输入参数：String host 邮件服务器地址名称<BR>&nbsp;*返回类型：<BR>&nbsp;*/<BR>public void setHost(String host){<BR>&nbsp; this.host = host;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置登录服务器校验密码<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setPassWord(String pwd){<BR>&nbsp; this.password = pwd;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置登录服务器校验用户<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setUserName(String usn){<BR>&nbsp; this.username = usn;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置邮件发送目的邮箱<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setTo(String to){<BR>&nbsp; this.to = to;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置邮件发送源邮箱<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setFrom(String from){<BR>&nbsp; this.from = from;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置邮件主题<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setSubject(String subject){<BR>&nbsp; this.subject = subject;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置邮件内容<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setContent(String content){<BR>&nbsp; this.content = content;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：把主题转换为中文<BR>&nbsp;*<BR>输入参数：String strText <BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public String transferChinese(String strText){<BR>&nbsp; try{<BR>&nbsp;&nbsp;&nbsp; strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");<BR>&nbsp; }catch(Exception e){<BR>&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp; }<BR>&nbsp; return strText;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：往附件组合中添加附件<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void attachfile(String fname){<BR>&nbsp; file.addElement(fname);<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：发送邮件<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：boolean 成功为true，反之为false<BR>&nbsp;*/<BR>public boolean sendMail(){</FONT></P>
<P><BR><FONT size=1>&nbsp;</FONT></P>
<P><FONT size=1>&nbsp; //构造mail session<BR>&nbsp; Properties props = System.getProperties();<BR>&nbsp; props.put("mail.smtp.host",host);<BR>&nbsp; props.put("mail.smtp.auth","true");<BR>&nbsp; Session session=Session.getDefaultInstance(props, new Authenticator(){<BR>&nbsp;&nbsp; public PasswordAuthentication getPasswordAuthentication(){<BR>&nbsp;&nbsp;&nbsp; return new PasswordAuthentication(username,password); <BR>&nbsp;&nbsp; }<BR>&nbsp; });<BR>&nbsp; <BR>&nbsp; try {<BR>&nbsp;&nbsp;&nbsp; //构造MimeMessage 并设定基本的值<BR>&nbsp;&nbsp;&nbsp; MimeMessage msg = new MimeMessage(session);<BR>&nbsp;&nbsp;&nbsp; msg.setFrom(new InternetAddress(from));<BR>&nbsp;&nbsp;&nbsp; InternetAddress[] address={new InternetAddress(to)};<BR>&nbsp;&nbsp;&nbsp; msg.setRecipients(Message.RecipientType.TO,address);<BR>&nbsp;&nbsp;&nbsp; subject = transferChinese(subject);<BR>&nbsp;&nbsp;&nbsp; msg.setSubject(subject);<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; //构造Multipart<BR>&nbsp;&nbsp;&nbsp; Multipart mp = new MimeMultipart();<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; //向Multipart添加正文<BR>&nbsp;&nbsp;&nbsp; MimeBodyPart mbpContent = new MimeBodyPart();<BR>&nbsp;&nbsp;&nbsp; mbpContent.setText(content);<BR>&nbsp;&nbsp;&nbsp; //向MimeMessage添加（Multipart代表正文）<BR>&nbsp;&nbsp;&nbsp; mp.addBodyPart(mbpContent);<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; //向Multipart添加附件<BR>&nbsp;&nbsp;&nbsp; Enumeration efile=file.elements();<BR>&nbsp;&nbsp;&nbsp; while(efile.hasMoreElements()){<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MimeBodyPart mbpFile = new MimeBodyPart();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filename=efile.nextElement().toString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileDataSource fds = new FileDataSource(filename);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbpFile.setDataHandler(new DataHandler(fds));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbpFile.setFileName(fds.getName());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //向MimeMessage添加（Multipart代表附件）<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mp.addBodyPart(mbpFile);</FONT></P>
<P><BR><FONT size=1>&nbsp;</FONT></P>
<P><FONT size=1>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; file.removeAllElements();<BR>&nbsp;&nbsp;&nbsp; //向Multipart添加MimeMessage<BR>&nbsp;&nbsp;&nbsp; msg.setContent(mp);<BR>&nbsp;&nbsp;&nbsp; msg.setSentDate(new Date());<BR>&nbsp;&nbsp;&nbsp; //发送邮件<BR>&nbsp;&nbsp;&nbsp; Transport.send(msg);<BR>&nbsp; <BR>&nbsp; } catch (MessagingException mex) {<BR>&nbsp;&nbsp;&nbsp; mex.printStackTrace();<BR>&nbsp;&nbsp;&nbsp; Exception ex = null;<BR>&nbsp;&nbsp;&nbsp; if ((ex=mex.getNextException())!=null){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ex.printStackTrace();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; return false;<BR>&nbsp; }<BR>&nbsp; return true;<BR>&nbsp;}<BR>/**<BR>&nbsp;*<BR>方法说明：主方法，用于测试<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>&nbsp;public static void main(String[] args){<BR>&nbsp; Mail sendmail = new Mail();<BR>&nbsp; sendmail.setHost("smtp.sohu.com");<BR>&nbsp; sendmail.setUserName("du_jiang");<BR>&nbsp; sendmail.setPassWord("31415926");<BR>&nbsp; sendmail.setTo("</FONT><A href="mailto:dujiang@sricnet.com"><FONT size=1>dujiang@sricnet.com</FONT></A><FONT size=1>");<BR>&nbsp; sendmail.setFrom("</FONT><A href="mailto:du_jiang@sohu.com"><FONT size=1>du_jiang@sohu.com</FONT></A><FONT size=1>");<BR>&nbsp; sendmail.setSubject("你好，这是测试！");<BR>&nbsp; sendmail.setContent("你好这是一个带多附件的测试！");&nbsp;<BR>&nbsp; //Mail sendmail = new Mail("</FONT><A href='http://www.java-cn.com/technology/tech/´mailto:dujiang@sricnet.com","du_jiang@sohu.com","smtp.sohu.com","du_jiang","31415926´'><FONT size=1>dujiang@sricnet.com","du_jiang@sohu.com","smtp.sohu.com","du_jiang","31415926</FONT></A><FONT size=1>","你好","胃，你好吗？");<BR>&nbsp; sendmail.attachfile("c:\\test.txt");<BR>&nbsp; sendmail.attachfile("DND.jar");<BR>&nbsp; sendmail.sendMail();</FONT></P>
<P><FONT size=1>&nbsp; }<BR>}//end</FONT></P>
<P><FONT size=1>import java.util.*;<BR>import java.io.*;<BR>import javax.mail.*;<BR>import javax.mail.internet.*;<BR>import javax.activation.*;</FONT></P>
<P><BR><FONT size=1>&nbsp;</FONT></P>
<P><FONT size=1>/**<BR>&nbsp;* </FONT>
<P><FONT size=1>Title: 使用javamail发送邮件</FONT></P><BR><FONT size=1>&nbsp;* </FONT>
<P><FONT size=1>Description: 演示如何使用javamail包发送电子邮件。这个实例可发送多附件</FONT></P><BR><FONT size=1>&nbsp;* </FONT>
<P><FONT size=1>Copyright: Copyright (c) 2003</FONT></P><BR><FONT size=1>&nbsp;* </FONT>
<P><FONT size=1>Filename: Mail.java</FONT></P><BR><FONT size=1>&nbsp;* @version 1.0<BR>&nbsp;*/<BR>public class Mail { </FONT>
<P></P>
<P><BR><FONT size=1>&nbsp;</FONT></P>
<P><FONT size=1>String to = "";//收件人<BR>String from = "";//发件人<BR>String host = "";//smtp主机<BR>String username = "" ;<BR>String password = "" ;<BR>String filename = "";//附件文件名<BR>String subject = "";//邮件主题<BR>String content = "";//邮件正文<BR>Vector file = new Vector();//附件文件集合<BR>/**<BR>&nbsp;*<BR>方法说明：默认构造器<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public Mail(){<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：构造器，提供直接的参数传入<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public Mail(String to,String from,String smtpServer,String username,String password,String subject,String content){<BR>&nbsp; this.to = to;<BR>&nbsp; this.from = from;<BR>&nbsp; this.host = smtpServer;<BR>&nbsp; this.username = username;<BR>&nbsp; this.password = password;<BR>&nbsp; this.subject = subject;<BR>&nbsp; this.content = content;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置邮件服务器地址<BR>&nbsp;*<BR>输入参数：String host 邮件服务器地址名称<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setHost(String host){<BR>&nbsp; this.host = host;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置登录服务器校验密码<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setPassWord(String pwd){<BR>&nbsp; this.password = pwd;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置登录服务器校验用户<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setUserName(String usn){<BR>&nbsp; this.username = usn;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置邮件发送目的邮箱<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setTo(String to){<BR>&nbsp; this.to = to;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置邮件发送源邮箱<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setFrom(String from){<BR>&nbsp; this.from = from;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置邮件主题<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setSubject(String subject){<BR>&nbsp; this.subject = subject;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：设置邮件内容<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void setContent(String content){<BR>&nbsp; this.content = content;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：把主题转换为中文<BR>&nbsp;*<BR>输入参数：String strText <BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public String transferChinese(String strText){<BR>&nbsp; try{<BR>&nbsp;&nbsp;&nbsp; strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");<BR>&nbsp; }catch(Exception e){<BR>&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp; }<BR>&nbsp; return strText;<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：往附件组合中添加附件<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>public void attachfile(String fname){<BR>&nbsp; file.addElement(fname);<BR>}<BR>/**<BR>&nbsp;*<BR>方法说明：发送邮件<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：boolean 成功为true，反之为false<BR>&nbsp;*/<BR>public boolean sendMail(){</FONT></P>
<P><BR><FONT size=1>&nbsp;</FONT></P>
<P><FONT size=1>&nbsp; //构造mail session<BR>&nbsp; Properties props = System.getProperties();<BR>&nbsp; props.put("mail.smtp.host",host);<BR>&nbsp; props.put("mail.smtp.auth","true");<BR>&nbsp; Session session=Session.getDefaultInstance(props, new Authenticator(){<BR>&nbsp;&nbsp; public PasswordAuthentication getPasswordAuthentication(){<BR>&nbsp;&nbsp;&nbsp; return new PasswordAuthentication(username,password); <BR>&nbsp;&nbsp; }<BR>&nbsp; });<BR>&nbsp; <BR>&nbsp; try {<BR>&nbsp;&nbsp;&nbsp; //构造MimeMessage 并设定基本的值<BR>&nbsp;&nbsp;&nbsp; MimeMessage msg = new MimeMessage(session);<BR>&nbsp;&nbsp;&nbsp; msg.setFrom(new InternetAddress(from));<BR>&nbsp;&nbsp;&nbsp; InternetAddress[] address={new InternetAddress(to)};<BR>&nbsp;&nbsp;&nbsp; msg.setRecipients(Message.RecipientType.TO,address);<BR>&nbsp;&nbsp;&nbsp; subject = transferChinese(subject);<BR>&nbsp;&nbsp;&nbsp; msg.setSubject(subject);<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; //构造Multipart<BR>&nbsp;&nbsp;&nbsp; Multipart mp = new MimeMultipart();<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; //向Multipart添加正文<BR>&nbsp;&nbsp;&nbsp; MimeBodyPart mbpContent = new MimeBodyPart();<BR>&nbsp;&nbsp;&nbsp; mbpContent.setText(content);<BR>&nbsp;&nbsp;&nbsp; //向MimeMessage添加（Multipart代表正文）<BR>&nbsp;&nbsp;&nbsp; mp.addBodyPart(mbpContent);<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; //向Multipart添加附件<BR>&nbsp;&nbsp;&nbsp; Enumeration efile=file.elements();<BR>&nbsp;&nbsp;&nbsp; while(efile.hasMoreElements()){<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MimeBodyPart mbpFile = new MimeBodyPart();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filename=efile.nextElement().toString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileDataSource fds = new FileDataSource(filename);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbpFile.setDataHandler(new DataHandler(fds));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbpFile.setFileName(fds.getName());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //向MimeMessage添加（Multipart代表附件）<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mp.addBodyPart(mbpFile);<BR></FONT><FONT size=1>}<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; file.removeAllElements();<BR>&nbsp;&nbsp;&nbsp; //向Multipart添加MimeMessage<BR>&nbsp;&nbsp;&nbsp; msg.setContent(mp);<BR>&nbsp;&nbsp;&nbsp; msg.setSentDate(new Date());<BR>&nbsp;&nbsp;&nbsp; //发送邮件<BR>&nbsp;&nbsp;&nbsp; Transport.send(msg);<BR>&nbsp; <BR>&nbsp; } catch (MessagingException mex) {<BR>&nbsp;&nbsp;&nbsp; mex.printStackTrace();<BR>&nbsp;&nbsp;&nbsp; Exception ex = null;<BR>&nbsp;&nbsp;&nbsp; if ((ex=mex.getNextException())!=null){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ex.printStackTrace();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; return false;<BR>&nbsp; }<BR>&nbsp; return true;<BR>&nbsp;}<BR>/**<BR>&nbsp;*<BR>方法说明：主方法，用于测试<BR>&nbsp;*<BR>输入参数：<BR>&nbsp;*<BR>返回类型：<BR>&nbsp;*/<BR>&nbsp;public static void main(String[] args){<BR>&nbsp; Mail sendmail = new Mail();<BR>&nbsp; sendmail.setHost("smtp.sohu.com");<BR>&nbsp; sendmail.setUserName("du_jiang");<BR>&nbsp; sendmail.setPassWord("31415926");<BR>&nbsp; sendmail.setTo("</FONT><A href="mailto:dujiang@sricnet.com"><FONT size=1>dujiang@sricnet.com</FONT></A><FONT size=1>");<BR>&nbsp; sendmail.setFrom("</FONT><A href="mailto:du_jiang@sohu.com"><FONT size=1>du_jiang@sohu.com</FONT></A><FONT size=1>");<BR>&nbsp; sendmail.setSubject("你好，这是测试！");<BR>&nbsp; sendmail.setContent("你好这是一个带多附件的测试！");&nbsp;<BR>&nbsp; //Mail sendmail = new Mail("</FONT><A href='http://www.java-cn.com/technology/tech/´mailto:dujiang@sricnet.com","du_jiang@sohu.com","smtp.sohu.com","du_jiang","31415926´'><FONT size=1>dujiang@sricnet.com","du_jiang@sohu.com","smtp.sohu.com","du_jiang","31415926</FONT></A><FONT size=1>","你好","胃，你好吗？");<BR>&nbsp; sendmail.attachfile("c:\\test.txt");<BR>&nbsp; sendmail.attachfile("DND.jar");<BR>&nbsp; sendmail.sendMail();</FONT></P>
<P><FONT size=1>&nbsp; }<BR>}//end</FONT></P><img src ="http://www.cnitblog.com/Justin/aggbug/2598.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Justin/" target="_blank">Justin</a> 2005-09-11 22:33 <a href="http://www.cnitblog.com/Justin/articles/2598.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java Mail 关于验证部分</title><link>http://www.cnitblog.com/Justin/articles/2593.html</link><dc:creator>Justin</dc:creator><author>Justin</author><pubDate>Sun, 11 Sep 2005 11:19:00 GMT</pubDate><guid>http://www.cnitblog.com/Justin/articles/2593.html</guid><wfw:comment>http://www.cnitblog.com/Justin/comments/2593.html</wfw:comment><comments>http://www.cnitblog.com/Justin/articles/2593.html#Feedback</comments><slash:comments>-3</slash:comments><wfw:commentRss>http://www.cnitblog.com/Justin/comments/commentRss/2593.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Justin/services/trackbacks/2593.html</trackback:ping><description><![CDATA[<P>网上很多，不过一般都是没有smtp验证的，下边是一段示例代码：<BR>&nbsp; 不能直接运行的，不过，可以看看里面关于验证的部分。</P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">发送邮件函数</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_86_2159_Open_Image onclick="this.style.display='none'; Codehighlighter1_86_2159_Open_Text.style.display='none'; Codehighlighter1_86_2159_Closed_Image.style.display='inline'; Codehighlighter1_86_2159_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_86_2159_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_86_2159_Closed_Text.style.display='none'; Codehighlighter1_86_2159_Open_Image.style.display='inline'; Codehighlighter1_86_2159_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;boolean&nbsp;sendMail(String&nbsp;mailTo,String&nbsp;mailSubject,String&nbsp;mailBody)</SPAN><SPAN id=Codehighlighter1_86_2159_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_86_2159_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">发送email</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_110_2013_Open_Image onclick="this.style.display='none'; Codehighlighter1_110_2013_Open_Text.style.display='none'; Codehighlighter1_110_2013_Closed_Image.style.display='inline'; Codehighlighter1_110_2013_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_110_2013_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_110_2013_Closed_Text.style.display='none'; Codehighlighter1_110_2013_Open_Image.style.display='inline'; Codehighlighter1_110_2013_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_110_2013_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_110_2013_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">default&nbsp;account&nbsp;information</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;smtpServer&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">smtp.smtpserver.com</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;smtpAuth&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">true</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;smtpUser&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">username</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;smtpPassword&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">password</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;From&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&lt;&nbsp;/FONT&nbsp;&gt;&nbsp;from@yourserver.com</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;To&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;mailTo;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;Subject&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;mailSubject;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;Text&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;mailBody;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;java.util.ResourceBundle&nbsp;resBundle;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resBundle&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;java.util.ResourceBundle.getBundle(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mailinfo</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Locale.SIMPLIFIED_CHINESE);<BR><IMG id=Codehighlighter1_624_928_Open_Image onclick="this.style.display='none'; Codehighlighter1_624_928_Open_Text.style.display='none'; Codehighlighter1_624_928_Closed_Image.style.display='inline'; Codehighlighter1_624_928_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_624_928_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_624_928_Closed_Text.style.display='none'; Codehighlighter1_624_928_Open_Image.style.display='inline'; Codehighlighter1_624_928_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(resBundle&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_624_928_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_624_928_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;smtpServer&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;resBundle.getString(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mail.smtp.host</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;smtpAuth&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;resBundle.getString(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mail.smtp.auth</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;smtpUser&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;resBundle.getString(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mail.smtp.user</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;smtpPassword&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;resBundle.getString(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mail.smtp.password</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;From&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;resBundle.getString(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mail.smtp.from</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Properties&nbsp;props&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Properties();<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Session&nbsp;sendMailSession;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Transport&nbsp;transport;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;props.put(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mail.smtp.host</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;smtpServer);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;props.put(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mail.smtp.auth</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;smtpAuth);<BR><IMG id=Codehighlighter1_1158_1360_Open_Image onclick="this.style.display='none'; Codehighlighter1_1158_1360_Open_Text.style.display='none'; Codehighlighter1_1158_1360_Closed_Image.style.display='inline'; Codehighlighter1_1158_1360_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1158_1360_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1158_1360_Closed_Text.style.display='none'; Codehighlighter1_1158_1360_Open_Image.style.display='inline'; Codehighlighter1_1158_1360_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">true</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">.equals(smtpAuth))&nbsp;</SPAN><SPAN id=Codehighlighter1_1158_1360_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1158_1360_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">smtp服务器需要验证，用MyAuthertiactor来创建mail&nbsp;session</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyAuthenticator&nbsp;myauth&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;MyAuthenticator(smtpUser,&nbsp;smtpPassword);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendMailSession&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;Session.getInstance(props,&nbsp;myauth);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_1373_1435_Open_Image onclick="this.style.display='none'; Codehighlighter1_1373_1435_Open_Text.style.display='none'; Codehighlighter1_1373_1435_Closed_Image.style.display='inline'; Codehighlighter1_1373_1435_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1373_1435_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1373_1435_Closed_Text.style.display='none'; Codehighlighter1_1373_1435_Open_Image.style.display='inline'; Codehighlighter1_1373_1435_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_1373_1435_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1373_1435_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendMailSession&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;Session.getInstance(props);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Debug</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendMailSession.setDebug(</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Message&nbsp;newMessage&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;MimeMessage(sendMailSession);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newMessage.setFrom(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;InternetAddress(From));<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newMessage.setRecipient(Message.RecipientType.TO,<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;InternetAddress(mailTo));<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newMessage.setSubject(Subject);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newMessage.setSentDate(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Date());<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newMessage.setText(Text);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newMessage.saveChanges();<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transport&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;sendMailSession.getTransport(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">smtp</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transport.send(newMessage,&nbsp;newMessage.getAllRecipients());<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transport.close();<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_2044_2138_Open_Image onclick="this.style.display='none'; Codehighlighter1_2044_2138_Open_Text.style.display='none'; Codehighlighter1_2044_2138_Closed_Image.style.display='inline'; Codehighlighter1_2044_2138_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_2044_2138_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2044_2138_Closed_Text.style.display='none'; Codehighlighter1_2044_2138_Open_Image.style.display='inline'; Codehighlighter1_2044_2138_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(Exception&nbsp;mailEx)&nbsp;</SPAN><SPAN id=Codehighlighter1_2044_2138_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_2044_2138_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Send&nbsp;Mail&nbsp;Error:</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;mailEx.getMessage());<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">smtp需要验证时候的验证类</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;MyAuthenticator<BR><IMG id=Codehighlighter1_2245_2559_Open_Image onclick="this.style.display='none'; Codehighlighter1_2245_2559_Open_Text.style.display='none'; Codehighlighter1_2245_2559_Closed_Image.style.display='inline'; Codehighlighter1_2245_2559_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_2245_2559_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2245_2559_Closed_Text.style.display='none'; Codehighlighter1_2245_2559_Open_Image.style.display='inline'; Codehighlighter1_2245_2559_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extends&nbsp;javax.mail.Authenticator&nbsp;</SPAN><SPAN id=Codehighlighter1_2245_2559_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_2245_2559_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;strUser;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;strPwd;<BR><IMG id=Codehighlighter1_2359_2422_Open_Image onclick="this.style.display='none'; Codehighlighter1_2359_2422_Open_Text.style.display='none'; Codehighlighter1_2359_2422_Closed_Image.style.display='inline'; Codehighlighter1_2359_2422_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_2359_2422_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2359_2422_Closed_Text.style.display='none'; Codehighlighter1_2359_2422_Open_Image.style.display='inline'; Codehighlighter1_2359_2422_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;MyAuthenticator(String&nbsp;user,&nbsp;String&nbsp;password)&nbsp;</SPAN><SPAN id=Codehighlighter1_2359_2422_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_2359_2422_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.strUser&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;user;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.strPwd&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;password;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG id=Codehighlighter1_2491_2555_Open_Image onclick="this.style.display='none'; Codehighlighter1_2491_2555_Open_Text.style.display='none'; Codehighlighter1_2491_2555_Closed_Image.style.display='inline'; Codehighlighter1_2491_2555_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_2491_2555_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2491_2555_Closed_Text.style.display='none'; Codehighlighter1_2491_2555_Open_Image.style.display='inline'; Codehighlighter1_2491_2555_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">protected</SPAN><SPAN style="COLOR: #000000">&nbsp;PasswordAuthentication&nbsp;getPasswordAuthentication()&nbsp;</SPAN><SPAN id=Codehighlighter1_2491_2555_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_2491_2555_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;PasswordAuthentication(strUser,&nbsp;strPwd);<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV><img src ="http://www.cnitblog.com/Justin/aggbug/2593.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Justin/" target="_blank">Justin</a> 2005-09-11 19:19 <a href="http://www.cnitblog.com/Justin/articles/2593.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>How To Use Eclipse with Oracle Containers for J2EE (OC4J)</title><link>http://www.cnitblog.com/Justin/articles/2263.html</link><dc:creator>Justin</dc:creator><author>Justin</author><pubDate>Mon, 22 Aug 2005 13:13:00 GMT</pubDate><guid>http://www.cnitblog.com/Justin/articles/2263.html</guid><wfw:comment>http://www.cnitblog.com/Justin/comments/2263.html</wfw:comment><comments>http://www.cnitblog.com/Justin/articles/2263.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Justin/comments/commentRss/2263.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Justin/services/trackbacks/2263.html</trackback:ping><description><![CDATA[<H1>How To Use Eclipse with Oracle Containers for J2EE (OC4J)</H1>
<P><STRONG>Author: Tugdual Grall <BR>Date: 12/05/04</STRONG></P>
<UL>
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#introduction">Introduction</A> 
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#prerequisites">What are the Prerequisites?</A> 
<UL>
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#know">What Should You Know?</A> 
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#requirements">What are the Software Requirements?</A> 
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#notations">What are the Notations?</A> </LI></UL>
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#learn">How to Build the Application</A> 
<UL>
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#section1">Installation and Configuration</A> 
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#section2">Create a Web Module using Lomboz</A> 
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#section3">Deploy the Application into OC4J</A> 
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#issues">Known issues</A> </LI></UL>
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#summary">Summary</A><BR></LI></UL>
<H2><A name=introduction></A>Introduction</H2>
<P>One of the benefits of the J2EE platform is that all the different artifacts are well defined, thereby allowing you to choose any development tools or containers you wish. The Lomboz plug-in for Eclipse comprises a wizard and a set of tools that enable J2EE to develop Servlets, JSPs, and EJBs and to deploy packaged applications into any J2EE-compliant container. </P>
<P>This article explains how to configure Eclipse and Lomboz to develop and deploy applications to OC4J.</P>
<H2><A id=prerequisites name=prerequisites></A>What are the Prerequisites?</H2>
<H3><A id=know name=know></A>What Should You Know? </H3>
<UL>
<LI>Be familiar with J2EE technologies (J2EE packaging and deployment, JSP). 
<LI>Have some experience with Eclipse. </LI></UL>
<H3><A id=requirements name=requirements></A>What are the Software Requirements?</H3>
<UL>
<LI>OracleAS Containers for J2EE 10<I>g</I> (9.0.4 Production or 10.1.3 Developer Preview) available from <A href="http://www.oracle.com/technology/tech/java/oc4j/index.html">OTN</A>. 
<LI>Sun's JDK 1.4_x, available <A href="http://java.sun.com/j2se/" target=_blank>here</A> 
<LI>Eclipse IDE 3.0 available on the <A href="http://eclipse.org/" target=_blank>Eclipse Web site</A> 
<LI>Lomboz 3.0.1, available for download from the <A href="http://www.objectlearn.com/index.jsp" target=_blank>Object Learn Web site</A>. (<A href="http://forge.objectweb.org/project/showfiles.php?group_id=97" target=_blank>download page</A>) 
<LI>Eclipse Modeling Framework (EMF) plug-in, available from the Lomboz <A href="http://forge.objectweb.org/project/showfiles.php?group_id=97" target=_blank>download page</A>. 
<LI>Lomboz Server Definitions for OC4J. To download them, right-click any of the following links, then choose Save link as: 
<UL>
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/etc/oc4j0904.server" target=_blank>Release 10<I>g</I> (9.0.4)</A> 
<LI><A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/etc/oc4j1013dp.server" target=_blank>Release 10<I>g</I> (10.1.3 Developer Preview)</A> <FONT color=#ff0000></FONT></LI></UL></LI></UL>
<H3><A id=notations name=notations></A>What are the Notations?</H3>
<UL>
<LI><B>%OC4J_HOME%</B> - The directory you installed OC4J. Example: <SPAN style="FONT-WEIGHT: bold">c:\oc4j</SPAN><BR style="FONT-WEIGHT: bold">
<LI><B>%JAVA_HOME%</B> - The directory where your JDK is installed 
<LI><SPAN style="FONT-WEIGHT: bold">%ECLIPSE_HOME%</SPAN> - The directory you installed Eclipse.<BR></LI></UL><SPAN style="FONT-WEIGHT: bold; COLOR: rgb(255,0,0)"></SPAN><BR>
<H2><A id=learn name=learn></A>How to Build the Application</H2>
<H3><A id=section1 name=section1></A>Installation and Configuration</H3>
<H4>Eclipse and Lomboz Installation<BR></H4>
<P>After you have downloaded and installed the Eclipse 3.0 IDE, follow these steps: </P>
<OL>
<LI>Unzip EMF in the Eclipse directory ( <STRONG>%ECLIPSE_HOME%</STRONG> ). <BR>
<LI>Unzip Lomboz 3.0.1 in the Eclipse directory. After you install (unzip) the plug-in, the directory structure looks like this: <IMG src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/plugin-files.gif" border=0><BR>
<LI>Copy the server definition files into the <STRONG>%ECLIPSE_HOME%\plugins\com.objectlearn.jdt.j2ee_3.0.1\servers\</STRONG> directory. If you do not plan to use any other Application Server, then remove the different definition files. <BR>
<LI>Run Eclipse. </LI></OL>
<P>The plug-in is now installed. To activate it, follow these steps: </P>
<OL>
<LI>Choose the <SPAN class=CODE>Window &gt; Customize Perspective</SPAN> menu. This screen allows you to select the perspectives you want to be accessible directly from the different menus of the Eclipse IDE. 
<LI>In the Submenus list, select New . Then select all the Lomboz items to add the Lomboz elements to the File &gt;New menu.<BR>Your perspectives screen should look like:<BR><IMG style="WIDTH: 642px; HEIGHT: 491px" alt="custom perspective screen" src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/custperspective.gif" align="center "><BR>
<LI>In the Submenu list, select <SPAN class=CODE>Show View</SPAN>. Then select the<SPAN class=CODE> Lomboz J2EE View</SPAN> to add the Lomboz view in your IDE.<BR>
<LI>In the Commands tab, select<SPAN class=CODE> Lomboz Actions</SPAN> to add the Lomboz button in the Eclipse toolbar. Use this button to switch the Lomboz J2EE View on and off.<BR>
<LI>Click OK. </LI></OL>
<H4>Eclipse Configuration</H4>
<H5>Configure the Label Decoration for Lomboz file</H5>
<P>Lomboz provides J2EE decoration through the Eclipse Label Decorations (button??????). You can show additional information about an item on its label or icon. To activate J2EE decoration, following these steps: </P>
<OL>
<LI>In the Eclipse menu, choose <SPAN class=CODE>Window &gt; Preferences</SPAN>. <BR>
<LI>Open the <SPAN class=CODE>Workbench</SPAN> node. <BR>
<LI>Click <SPAN class=CODE>Label Decorations</SPAN>. <BR>
<LI>Select <SPAN class=CODE>Lomboz J2EE Decorators</SPAN>. </LI></OL>
<H5>Configure theJava Environment</H5>
<P>By default, Eclipse uses a Java Runtime Environment (JRE). However, in the case of J2EE development ¾ especially for JSP compilation ¾ you must configure Eclipse to work with a Java Development Kit (JDK). To configure Eclipse to use the JDK, follow these steps: </P>
<OL>
<LI>In the Eclipse menu, choose <SPAN class=CODE>Window &gt; Preferences</SPAN>. <BR>
<LI>Open the <SPAN class=CODE>Java</SPAN> node. <BR>
<LI>Select the <SPAN class=CODE>Installed JRE</SPAN> node. <BR>
<LI>Click Add. <BR>
<LI>In the Add JRE dialog box, first enter a name. Next, select the JDK home directory. Then click OK. <BR>
<LI>Select the JDK 1.4.2 box to set this JDK as the default Java runtime. The Preferences screen looks like this: <BR><IMG height=309 src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/pref_jdk.gif" width=640 border=0><BR></LI></OL>
<DIV style="TEXT-ALIGN: left">
<H4>Lomboz configuration</H4>
<P>Lomboz is a set of wizards for J2EE development, testing, and deployment that makes it possible for you to deploy J2EE applications in different application servers. In the next steps, you will configure Lomboz and Eclipse to support OC4J deployment. You configure the Lomboz plug-in using the Eclipse preferences screen, used previously to set the JDK. If you have closed this window, click <SPAN class=CODE>Window &gt; Preferences</SPAN>. </P>
<P></P>
<H5>Setting the JDK Tools</H5>
<P>In the Preferences window, click Lomboz to set global settings. </P>
<OL>
<LI>Select the JDK <SPAN class=CODE>tools.jar</SPAN> file, based on the JDK installed on your system. <BR>
<LI>Click Apply. </LI></OL>
<H5>Configuring the Server Definition</H5>
<OL>
<LI>Select Server Definitions. <BR>
<LI>Select "OC4J 10g (10.1.3) Developer Preview" in the server type. <BR>
<LI>Enter the different values corresponding to your application server: 
<UL>
<LI>The OC4J Directory that is, %OC4J_HOME% 
<LI>The username and password 
<LI>The HTTP and ORMI ports 
<LI>The default Web Site used by OC4J <BR></LI></UL>
<LI>Click Apply. </LI></OL>
<P>The Preferences screen looks like this: <BR></P>
<DIV style="TEXT-ALIGN: center">
<DIV style="TEXT-ALIGN: left">
<DIV style="TEXT-ALIGN: center"><IMG alt="Lomboz Preferences Screen" src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/preferenceScreen.gif"><BR></DIV>
<P>The server definition is used to: 
<UL>
<LI>Put the necessary Java libraries in your project for compilation. 
<LI>Run the container locally from Eclipse. 
<LI>Deploy the application to your application server (locally or remotely). </LI></UL>
<P><STRONG>Note: </STRONG>If you plan to deploy your application remotely, be sure that the release of the server you are using in Lomboz is identical to the one you are deploying on. </P></DIV></DIV>
<H3><A id=section2 name=section2></A>Web Module using Lomboz</H3>
<P>First, create a J2EE Project using the Lomboz wizard. </P>
<H4>Create the project </H4>
<OL>
<LI>In the Eclipse Workbench menu, choose File &gt; New &gt; Lomboz J2EE Project. 
<DIV style="TEXT-ALIGN: center"><IMG style="WIDTH: 582px; HEIGHT: 434px" alt="New project" src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/new_project_lomboz.gif"><BR></DIV>
<LI>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>Enter a name for your project for example, <SPAN class=CODE>MyFirstJ2EEProject</SPAN>. <BR><BR></DIV></DIV>
<LI>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>Click Next. Keep all default on the Java Settings.<BR><BR></DIV></DIV>
<LI>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>Click Next. <BR><BR></DIV></DIV>
<LI>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>Click Add to add a new Web Module to your project. <BR><BR></DIV></DIV></DIV></DIV>
<LI>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>Enter the name of your J2EE Web Module for example, <SPAN class=CODE>myWebApplication</SPAN> .<BR><BR></DIV></DIV></DIV></DIV>
<LI>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>Select the Targeted Servers tab.<BR><BR></DIV></DIV></DIV></DIV>
<LI>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>Add the OC4J release you are using. <BR><IMG alt="Select Targeted Servers" src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/add_server_module.gif" align=middle><BR><BR></DIV></DIV></DIV></DIV>
<LI>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>
<DIV style="TEXT-ALIGN: center">
<DIV align=left>Click Finish.<BR></DIV></DIV></DIV></DIV></LI></OL>
<P>The Lomboz J2EE Project wizard creates the J2EE structure for the Web Module, as well as several additional files to build and deploy the application. The structure of the <SPAN class=CODE>myWebApplication</SPAN> Web module looks like this:</P>
<DIV style="TEXT-ALIGN: center">
<DIV style="TEXT-ALIGN: center"><IMG style="WIDTH: 165px; HEIGHT: 230px" alt="Project Structure" src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/project_structure.gif"><BR>
<P align=left>You can now edit the JSP file using the Lomboz JSP Editor. This editor provides code-insight and checks the validity of the code. </P></DIV></DIV>
<H3><A id=section3 name=section3></A>Deploy the Application into OC4J</H3>
<DIV style="TEXT-ALIGN: center">
<DIV style="TEXT-ALIGN: left">
<DIV style="TEXT-ALIGN: center">
<DIV style="TEXT-ALIGN: left">
<P>The next step in the development cycle is to test the application. To run the application inside OC4J, you must first package the application in an EAR file and then deploy it to your OC4J instance. The Lomboz plug-in for Eclipse enables you to package the application using a wizard and also to deploy the application to an OC4J instance. </P>
<H4>Create a EAR Module</H4></DIV></DIV></DIV></DIV>The first think to do is to create an EAR file that contains our Web module. Lomboz provides a wizard to help you to create and select the different modules to include in your EAR file. <BR>
<OL>
<LI>In the Eclipse menu select <SPAN class=code>File &gt; New &gt; Lomboz EAR Module<BR></SPAN>
<LI>Enter the name and the description of your application:<BR><BR>
<DIV style="TEXT-ALIGN: center"><IMG style="WIDTH: 608px; HEIGHT: 585px" alt="EAR File description" src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/ear_description.gif"><BR><BR></DIV>
<LI>Right Click, on the EAR Module and select <SPAN class=code>Add Web Module<BR></SPAN>
<LI>Select the Web Module, <SPAN class=code>myapp.war<BR></SPAN>
<LI>You can change the name of the WAR file, and the context-root.<BR>
<LI>Select the web module you want to add to this EAR file by clicking on the <IMG height=24 src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/add_button.gif" width=26 border=0> button; the screen should look like:<BR>
<DIV style="TEXT-ALIGN: center"><IMG src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/add_web_module.gif" border=0><BR>&nbsp;</DIV>
<LI>Select the Targeted Servers tab, and choose the OC4J release you are using.<BR>
<LI>Click finish </LI></OL>
<H4>Start the application server</H4>
<P>If you are using a remote OC4J instance for deployment, then you do not need to follow these steps. However, you must ensure that the server is up before you deploy the application to it. </P>Lomboz allows you to start the targeted application server. You can do this from the Lomboz J2EE View, by right-clicking the server name and selecting <SPAN class=CODE>Run Server</SPAN> . 
<DIV style="TEXT-ALIGN: center"><IMG alt="Lomboz J2EE View" src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/j2ee_view.gif"><BR>
<DIV style="TEXT-ALIGN: left">If you do not see the Lomboz J2EE view in the Eclipse IDE, you must activate the view by clicking on the Lomboz View icon <IMG style="WIDTH: 49px; HEIGHT: 30px" alt="Lomboz J2EE View Icon" src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/j2ee_view_icon.gif">&nbsp; in the toolbar or using the menu <SPAN class=code>Window &gt; Show View</SPAN>.<BR>
<P>The OC4J instance directly starts from Eclipse, and OC4J uses the Console to print the information:<BR></P>
<DIV style="TEXT-ALIGN: center"><IMG alt="Eclipse Console With OC4J Log Message" src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/start_console.gif"><BR>
<DIV style="TEXT-ALIGN: left">&nbsp;</DIV>
<DIV style="TEXT-ALIGN: left">
<H4>Deploy The Application</H4></DIV>
<DIV style="TEXT-ALIGN: left">
<P>Lomboz enables you to deploy applications directly from Eclipse. The OC4J server definition file provided in this document is configured to support deployment using the OC4J console tool (<SPAN class=CODE>admin.jar </SPAN>). </P>
<P>To deploy the application, right click on the EAR Module in the Lomboz view, named <SPAN class=code>MyLombozApplication</SPAN> in our example, and choose <SPAN class=code>Deploy</SPAN>.</P></DIV>
<P><IMG height=145 src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/deploy_application.gif" width=264 border=0></P>
<P style="TEXT-ALIGN: left">The Lomboz deployment tool packages the Web application in a WAR file, creates the EAR file, and runs the OC4J Deployment tool. The Eclipse Console displays the messages returned by these different steps. 
<P style="TEXT-ALIGN: left">If you encounter a problem during deployment, please take a look to the <A href="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/lomboz-howto-part-1.html#issues">known issues section</A>. 
<H4 style="TEXT-ALIGN: left">Test The Application</H4>
<P style="TEXT-ALIGN: left">You can now use the application from your browser using the URL: <SPAN class=code>http://server:port/myapp</SPAN></P></DIV></DIV></DIV>
<H3><A name=issues></A>Known Issues<BR></H3>
<UL>
<LI><SPAN class=code>J2EEAction.Internal Error</SPAN> when deploying the Lomboz EAR Project: When Lomboz is packaging the EAR file, is validating the application.xml file using the default DTD ( <A href="http://java.sun.com/j2ee/dtds/application_1_2.dtd">http://java.sun.com/j2ee/dtds/application_1_2.dtd</A> ). This error occurs when the DTD is not accessible. 
<UL>
<LI>If you do not have access to the Internet, you must delete the DTD declaration from the file. Using the Eclipse text editor, right-click the file, select Open with &gt; Text Editor, and remove or comment the DOCTYPE declaration. 
<LI>If you have access to the Internet using a proxy, you must set the proxy in the Eclipse preferences. To do this, in the Eclipse menu, click Window &gt; Preferences. Then choose the Install/Update section, and set the proxy hostname and port. </LI></UL>
<LI>Lomboz J2EE View is empty: Force the refresh of the view by resizing the view, or hide/show the view, using the Lomboz toolbar button <IMG height=30 src="http://www.oracle.com/technology/tech/java/oc4j/1013/howtos/how-to-configure-lomboz/doc/images/j2ee_view_icon.gif" width=49 border=0>. </LI></UL>
<P>If you encounter other issues, use the Eclipse log file for debugging. This file is located at: <BR><SPAN class=CODE>%ECLIPSE_HOME%/workplace/.metadata</SPAN></P>
<H2><A name=summary></A>Summary</H2>
<P>In this article you have learned how to: </P>
<UL>
<LI>Install the Lomboz plug-in into the Eclipse IDE. 
<LI>Configure Lomboz to use OC4J. 
<LI>Create a Web module using Lomboz wizards. 
<LI>Package and deploy a J2EE application from Eclipse to OC4J. 
<LI>Run a J2EE application in OC4J, in the context of Eclipse. </LI></UL></DIV><img src ="http://www.cnitblog.com/Justin/aggbug/2263.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Justin/" target="_blank">Justin</a> 2005-08-22 21:13 <a href="http://www.cnitblog.com/Justin/articles/2263.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>