Hi,

Can anyone please help me out .I am trying out to send a mail using JAVAMAIL API,
While executing the jsp code on server it is throwing few exception error.

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;
   // Recipient's email ID needs to be mentioned.
  String to = request.getParameter("to");

   // Sender's email ID needs to be mentioned
   String from = request.getParameter("from");

   // Assuming you are sending email from localhost
   String host = "localhost";

   // Get system properties object
   Properties properties = System.getProperties();

   // Setup mail server
   properties.setProperty("mail.smtp.host", host);

   // Get the default Session object.
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // Create a default MimeMessage object.
      MimeMessage message = new MimeMessage(mailSession);

      // Set From: header field of the header.
      message.setFrom(new InternetAddress(from));

      // Set To: header field of the header.
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));

      // Set Subject: header field
      message.setSubject("This is the Subject Line!");

      // Create the message part 
      BodyPart messageBodyPart = new MimeBodyPart();

      // Fill the message
      messageBodyPart.setText("This is message body");

      // Create a multipar message
      Multipart multipart = new MimeMultipart();

      // Set text message part
      multipart.addBodyPart(messageBodyPart);

      // Part two is attachment
      messageBodyPart = new MimeBodyPart();
      String filename = "file.txt";
      DataSource source = new FileDataSource(filename);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(filename);
      multipart.addBodyPart(messageBodyPart);

      // Send the complete message parts
      message.setContent(multipart );

      // Send message
      Transport.send(message);
      String title = "Send Email";
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%>
<html>
<head>
<title>Send Attachement Email using JSP</title>
</head>
<body>
<center>
<h1>Send Attachment Email using JSP</h1>
</center>
<p align="center">
<% 
   out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>



HTTP Status 500 - An exception occurred processing JSP page /mail.jsp at line 29

type Exception report

message An exception occurred processing JSP page /mail.jsp at line 29

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /mail.jsp at line 29

26:       MimeMessage message = new MimeMessage(mailSession);
27: 
28:       // Set From: header field of the header.
29:       message.setFrom(new InternetAddress(from));
30: 
31:       // Set To: header field of the header.
32:       message.addRecipient(Message.RecipientType.TO,


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    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:728)
root cause

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.mail_jsp._jspService(mail_jsp.java:97)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    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:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.35 logs.

Apache Tomcat/7.0.35

Recommended Answers

All 5 Replies

a few notes here:
1. learn to use servlets, putting snippets in your jsp page itself is bad design.
2. there is an entire JSP subforum under the 'Web Development' section, might be you want to ask your question there.
3. read your error message:

> An exception occurred processing JSP page /mail.jsp at line 29
> 26:       MimeMessage message = new MimeMessage(mailSession);    
> 27: 
> 28:       // Set From: header field of the header.
> 29:       message.setFrom(new InternetAddress(from));

looks to me like your from value might not be initialized. I see you set it like

String from = request.getParameter("from");

tried hardcoding the value and see whether it works then?

WOW! is it really possible to do that??

you thought hotmail/gmail/.... worked on magic?

caaaaat: to which post is that a reply, and what exactly are you trying to show us with that link?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.