无法使用javamail发送邮件

时间:2012-07-09 06:03:08

标签: java jsp exception sendmail

我正在尝试通过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技术的新手,我将非常感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

看看这两行:

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

您说,您希望获取名为电子邮件地址的参数,而您可能想说String from="smzk07@gmail.com";,请尝试使用。{/ p>

答案 1 :(得分:0)

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

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

(显然有正确的转义码)

您获得空指针异常的原因是您请求的参数名称未设置(请求不存在的参数将为您提供null结果)

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

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

在这种情况下你需要使用:

的request.getParameter( “来自”); 的request.getParameter( “至”);