-1

我正在尝试通过 java api 发送邮件,我已经在 web-inf/lib 中复制了 activation.jar 和 mail.jar,这已经解决了所有问题,但我仍然遇到了一组异常,并且无法运行程序,请看一看

**JSP CODE**

   <html>
   <head>
   <title>JSP JavaMail Example </title>
   </head>
   <body>
   <%@ page import="java.util.*" %>
   <%@ page import="javax.mail.*" %>
   <%@ page import="javax.mail.internet.*" %>
   <%@ page import="javax.activation.*" %>
   <%
   String host = "mail.google.com";
   String to = request.getParameter("smzk07@gmail.com");
   String from = request.getParameter("heady.syed@gmail.com");
   String subject = request.getParameter("Create test description");
   String messageText = request.getParameter("hello sir");
   boolean sessionDebug = false;
// Create some properties and get the default Session.
   Properties props = System.getProperties();
   props.put("mail.host", host);
   props.put("mail.transport.protocol", "smtp");
   Session mailSession = Session.getDefaultInstance(props, null);

// Set debug on the Session
// Passing false will not echo debug info, and passing True will.

   mailSession.setDebug(sessionDebug);

// Instantiate a new MimeMessage and fill it with the 
// required information.

   Message msg = new MimeMessage(mailSession);
   msg.setFrom(new InternetAddress(from));
   InternetAddress[] address = {new InternetAddress(to)};
   msg.setRecipients(Message.RecipientType.TO, address);
   msg.setSubject(subject);
   msg.setSentDate(new Date());
   msg.setText(messageText);

// Hand the message to the default transport service
// for delivery.

   Transport.send(msg);
   out.println("Mail was sent to " + to);
   out.println(" from " + from);
                out.println(" using host " + host + ".");
   %>
  </table>
  </body>
  </html>

例外

org.apache.jasper.JasperException: java.lang.NullPointerException
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

根本原因

java.lang.NullPointerException
    javax.mail.internet.InternetAddress.parse(InternetAddress.java:673)
    javax.mail.internet.InternetAddress.parse(InternetAddress.java:633)
    javax.mail.internet.InternetAddress.<init>(InternetAddress.java:111)
    org.apache.jsp.send_0020mail_jsp._jspService(send_0020mail_jsp.java:97)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

我是 JSP 和 java 技术的新手,非常感谢您的帮助。

4

2 回答 2

2

看看这两行:

String to = request.getParameter("smzk07@gmail.com");
String from = request.getParameter("heady.syed@gmail.com");

您说的是,您想要获取命名为电子邮件地址的参数,相反,您可能想说String from="smzk07@gmail.com";,改为尝试。

于 2012-07-09T06:58:29.513 回答
0

您确定要将 request.getParameter 与您正在使用的参数一起使用吗?根据您发布的代码,您的请求将类似于:

bob.com/mail?smzk07@gmail.com=someone@bob.com

(显然有正确的转义码)

你得到一个空指针异常的原因是你请求的参数名称没有设置(请求一个不存在的参数会给你一个空结果)

您可能正在尝试执行以下操作:

bob.com/mail?from=smzk07@gmail.com&to=....

在这种情况下,您需要使用:

request.getParameter("来自"); request.getParameter("to");

等等

于 2012-07-09T06:57:53.083 回答