javamail api in jsp

Discussion in 'JSP' started by greenhorn568, Dec 23, 2007.

Thread Status:
Not open for further replies.
  1. greenhorn568

    greenhorn568 New Member

    Dec 23, 2007
    Likes Received:
    Trophy Points:
    Can some one please tell me how to make my JSP's use JavaMail to send
    1). Please edit the code in such a way that, it should receive parameter values from the user form (request.getParameter())
    2). parameters to be received are To address, From address, emailsubject, emailText.

    The below code successfully sends emails to the emailList. Can someone help me to edit this code for the above requirements.
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;
    public class SendMailUsingAuthentication
      private static final String SMTP_HOST_NAME = "";
      private static final String SMTP_AUTH_USER = "drarreddy49_hyd";
      private static final String SMTP_AUTH_PWD  = "enclave";
      private static final String emailMsgTxt      = "Online Order Confirmation Message. Also include the Tracking Number.";
      private static final String emailSubjectTxt  = "Order Confirmation Subject";
      private static final String emailFromAddress = "";
      // Add List of Email address to who email needs to be sent to
      private static final String[] emailList = {"", ""};
      public static void main(String args[]) throws Exception
        SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
        smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
        System.out.println("Sucessfully Sent mail to All Users");
      public void postMail( String recipients[ ], String subject,
                                String message , String from) throws MessagingException
        boolean debug = false;
         //Set the host smtp address
         Properties props = new Properties();
         props.put("", SMTP_HOST_NAME);
         props.put("mail.smtp.auth", "true");
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);
        // create a message
        Message msg = new MimeMessage(session);
        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++)
            addressTo[i] = new InternetAddress(recipients[i]);
        msg.setRecipients(Message.RecipientType.TO, addressTo);
        // Setting the Subject and Content Type
        msg.setContent(message, "text/plain");
    * SimpleAuthenticator is used to do simple authentication
    * when the SMTP server requires it.
    private class SMTPAuthenticator extends javax.mail.Authenticator
        public PasswordAuthentication getPasswordAuthentication()
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
    Last edited by a moderator: Dec 24, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Jul 12, 2004
    Likes Received:
    Trophy Points:
    Duplicate of javamail api in jsp. Thread closed. Please do not create the same thread more then once.
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice