JSP Email

Discussion in 'JSP' started by virendrachandak, Jun 26, 2010.

  1. virendrachandak

    virendrachandak New Member

    Jun 26, 2010
    Likes Received:
    Trophy Points:

    I am using the following code to send email using JSP through gmail.

    But i am getting error in the following line:
    Session mailSession = Session.getDefaultInstance(props, null);

    String host="", user="", pass="";
    host = smtp_server; //"smtp.gmail.com"; 
    user = jsp_email; //"YourEmailId@gmail.com" // email id to send the emails
    pass = jsp_email_pw; //Your gmail password
    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    String to = "user@gmail.com"; // out going email id
    String from = "user@gmail.com"; //Email id of the recipient
    String subject = "subject";
    String messageText = "body";
    boolean sessionDebug = true;
    Properties props = System.getProperties();
    props.put("mail.host", host);
    props.put("mail.transport.protocol.", "smtp");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
    Session mailSession = Session.getDefaultInstance(props, null);
    Message msg = new MimeMessage(mailSession);
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] address = {new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setContent(messageText, "text/html"); // use setText if you want to send text
    Transport transport = mailSession.getTransport("smtp");
    transport.connect(host, user, pass);
    try {
    transport.sendMessage(msg, msg.getAllRecipients());
    out.println("Email sent");
    WasEmailSent = true; // assume it was sent
    catch (Exception err) {
    WasEmailSent = false; // assume it's a fail

    Does anyone else is also getting similar error?

    Or do you have any better way to send email using jsp.

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