Hi  JavaGurus,

I wish to send a mail using the SMTP server of IIS.

I have the following progs. for sending  mail . But the mail doesnot go. Can
anybody show a way out?

I am including the code for the SendMail.html & SendMail.java ( servlet ).

I am using Tomcat. I have already set the classpaths for the mail.jar &
activation.jar files. I have compiled the servlet.

For sending message thru SMTP, do I have to do any settings in security
manager or properties file or any config file or somewhere else ?

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

First  the   SendMail.java   code
xxxxxxxxxxxxxxxxxxxxxxxxxxx

<HTML>
  <HEAD><TITLE>SendMail</TITLE></HEAD>
    <BODY>
      <H1>SendMail</H1>
        <form action='http://localhost:8080/servlet/SendMail' method='post'>
      <TABLE><tbody align='right'>
        <TR><TD>From:</TD><TD><input type='text' name='from' size='64'></TD>
        </TR><TR><TD>To:</TD><TD><input type='text' name='to'
size='64'></TD></TR>
        <TR><TD>Cc:</TD><TD><input type='text' name='cc'
size='64'></TD></TR>
        <TR><TD>Bcc:</TD><TD><input type='text' name='bcc'
size='64'></TD></TR>
        <TR><TD>Subject:</TD><TD><input type='text' name='subject'
            size='64'></TD></TR>
        <TR><td colspan='2'><textarea name='text' rows='8'
            cols='64'></TEXTAREA></TD></TR>
        <TR><td colspan='2'><input type='submit' value='Send'>&nbsp;<input
            type='reset'></TD></TR>
      </TBODY></TABLE>
    </FORM>
  </BODY>
</HTML>





xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Now  the   SendMail.java   code
xxxxxxxxxxxxxxxxxxxxxxxxxxx

import javax.mail.*;
import javax.mail.internet.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class SendMail extends HttpServlet {

   private String smtpHost;

   // Initialize the servlet with the hostname of the SMTP server
   // we'll be using the send the messages

   public void init(ServletConfig config)
   throws ServletException {
      super.init(config);

      smtpHost = config.getInitParameter("smtpHost");
   }

   public void doPost(
      HttpServletRequest request,
      HttpServletResponse response
   )
   throws ServletException, java.io.IOException {
      String from = request.getParameter("from");
      String to = request.getParameter("to");
      String cc = request.getParameter("cc");
      String bcc = request.getParameter("bcc");
      String subject = request.getParameter("subject");
      String text = request.getParameter("text");

      String status;

      try

         // Create the JavaMail session
         java.util.Properties properties = System.getProperties();
         properties.put("mail.smtp.host", smtpHost);
         Session session =
         Session.getInstance(properties, null);

         // Construct the message
         MimeMessage message = new MimeMessage(session);

         // Set the from address
         Address fromAddress = new InternetAddress(from);
         message.setFrom(fromAddress);

         // Parse and set the recipient addresses
         Address[] toAddresses = InternetAddress.parse(to);
         message.setRecipients(Message.RecipientType.TO,toAddresses);


         Address[] ccAddresses = InternetAddress.parse(cc);
         message.setRecipients(Message.RecipientType.CC,ccAddresses);

         Address[] bccAddresses = InternetAddress.parse(bcc);
         message.setRecipients(Message.RecipientType.BCC,bccAddresses);

         // Set the subject and text
         message.setSubject(subject);
         message.setText(text);

         Transport.send(message);

         status = "Your message was sent.";

         } catch (AddressException e) {
            status = "There was an error parsing the addresses.";
         } catch (SendFailedException e) {
            status = "There was an error sending the message.";
         } catch (MessagingException e) {
         status = "There was an unexpected error.";
         }

      // Output a status message
      response.setContentType("text/html");

      java.io.PrintWriter writer = response.getWriter();

      writer.println("<html><head><title>Status</title></head>");
      writer.println("<body><p>" + status + "</p></body></html>");

      writer.close();
   }
}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX



GURUS, Looking for advise.

Thanks in advance.


Sunil K. Roy
Sunil K. Roy

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to