小弟不才,提问JavaMail问题,100相送各路大侠!

AlexGL 2004-12-12 10:21:28
我的机子能通过ADSL上网,现配置好J2SE、Resin,JSP普通程序能正常运行,现想写利用JavaMail进行收发电子邮件的Web程序,已经将Mail.jar、activation.jar拷贝到WebInf的Lib文件夹中程序调试了很长时间,参考了很多文档(除了英文文档),未能写成普通收发电邮的WEB程序,现向各位请教。

Send.htm代码如下:

<html>

  <BODY>

  <FORM action="sendmail2.jsp" method="post">

   <TABLE align="center">

    <TR>

     <TD width="50%">

      收件人:<BR><INPUT name="to" size="25">

     </TD>

     <TD width="50%">

      寄信人:<BR><INPUT name="from" size="25">

     </TD>

    </TR>

    <TR>

     <TD colspan="2">

      主题:<BR><INPUT name="title" size="50">

     </TD>

    </TR>

    <TR>

     <TD colspan="2">

      <p>邮件正文:<BR><TEXTAREA name="content" rows=25 cols=85></TEXTAREA></p>

     </TD>

    </TR>

    </TABLE>

    <INPUT type="submit" name="cb_submit" value="发送">

    <INPUT type="reset" name="cb_reset" value="重写">

   </FORM>

  </BODY>

  </HTML>

sendmail2.jsp程序代码如下:

<%@ page import=" javax.mail.*, javax.mail.internet.*, javax.activation.* "%>
<%@ page import="java.util.*" %>
<body>
<%
try{

//从html表单中获取邮件信息
String tto=request.getParameter("to");
String ttitle=request.getParameter("title");
String tcontent=request.getParameter("content");

Properties props=new Properties();//也可用Properties props = System.getProperties();
props.put("mail.smtp.host","smtp.163.com");//存储发送邮件服务器的信息
props.put("mail.smtp.auth","true");//同时通过验证
Session s=Session.getInstance(props);//根据属性新建一个邮件会话
s.setDebug(true);

MimeMessage message=new MimeMessage(s);//由邮件会话新建一个消息对象

//设置邮件
InternetAddress from=new InternetAddress("");
message.setFrom(from);//设置发件人
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
message.setSubject(ttitle);//设置主题
message.setText(tcontent);//设置信件内容
message.setSentDate(new Date());//设置发信时间

//发送邮件
message.saveChanges();//存储邮件信息
Transport transport=s.getTransport("smtp");
transport.connect("smtp.163.com","","");//以smtp方式登录邮箱
transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有
//已设好的收件人地址
transport.close();

%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<p><a href="recmail.jsp">去看看我的信箱</a><br/>
<br/>
<a href="index.htm">再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>



  </BODY>

  </HTML>

错误如下:


java.lang.NoClassDefFoundError: javax/activation/DataSource
at _sendmail2__jsp._jspService(/jmail/sendmail2.jsp:19)
at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
at com.caucho.jsp.Page.subservice(Page.java:497)
at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:182)
at com.caucho.server.http.Invocation.service(Invocation.java:315)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:246)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:164)
at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
at java.lang.Thread.run(Thread.java:534)

很不理解(我已经把activation.jar加载了啊),望各路大侠能不吝赐教!!
另,很多网站的Email服务器都需要密码验证,我的程序是否需要修改一下?在哪改啊,我是菜鸟,还情多帮忙啊。
...全文
116 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pcdll 2004-12-13
  • 打赏
  • 举报
回复
将activation.jar copy到你的resin下的lib目录或是设置到classpath下
jackkui 2004-12-13
  • 打赏
  • 举报
回复
给你一个例子。
package jmail;
import com.sun.mail.imap.Utility;

import javax.mail.internet.*;
import javax.mail.Session;
import javax.mail.MessagingException;
import javax.mail.Message;
import javax.mail.Transport;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.Properties;
import java.util.Date;
import java.util.ArrayList;
import java.io.UnsupportedEncodingException;


/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2004/12/01
* Time: 13:27:40
* To change this template use File | Settings | File Templates.
*/
public class SendJavaMail {

public SendJavaMail() {
}
public static void main(String[] args){
try {
Properties properties = System.getProperties();
properties.put("mail.smtp.host","172.16.8.3");
Session sendSession = Session.getInstance(properties,null);
System.out.println(sendSession.getProperty("mail.smtp.host"));
InternetAddress[] mailTo = InternetAddress.parse("huangjq@dhc.com.cn");
Message message = new MimeMessage(sendSession);
message.setFrom(new InternetAddress("huangjq@dhc.com.cn"));
message.setRecipients(Message.RecipientType.TO,mailTo);
message.setSubject("This message is sent by javamail");
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent("This is a mail with file","text/html;charset=GB2312");
multipart.addBodyPart(bodyPart);
ArrayList attachment = new ArrayList();
attachment.add(0,"picture2.jpg");
attachment.add(1,"picture3.jpg");
attachment.add(2,"picture4.jpg");
attachment.add(3,"picture5.jpg");
message.setContent(multipart);
message.setSentDate(new Date());
Transport transport = sendSession.getTransport("smtp");

transport.connect("server","","");
System.out.println(transport.toString());
transport.sendMessage(message,mailTo);
transport.close();
System.out.println("The mail has sent");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException me){
me.printStackTrace();
}
}
}
CALM 2004-12-13
  • 打赏
  • 举报
回复
楼上两位说的很全了,再给你个例子
http://www.javaresearch.org/article/showarticle.jsp?column=2&thread=12137
Idora 2004-12-13
  • 打赏
  • 举报
回复
你用google查关键字:javamail+authenticator就可以找到你要的解决方案了,需要将你的用户名和密码加进去
ivorstar 2004-12-12
  • 打赏
  • 举报
回复
不好意思,写错地方了
ivorstar 2004-12-12
  • 打赏
  • 举报
回复
建议转贴吧

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧