SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: JSP Email

  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JSP Email

    Hi,

    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);


    Code:
    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);
    mailSession.setDebug(sessionDebug);
    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.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
    }
    transport.close();

    Does anyone else is also getting similar error?

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

  2. #2
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What sort of error are you getting?
    baby steps... baby steps...

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the line
    Session mailSession = Session.getDefaultInstance(props, null);

    I am getting the following error:

    Session cannot be resolved to a type


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •