www.webdeveloper.com
Results 1 to 5 of 5

Thread: Sending email using JSP

  1. #1
    Join Date
    Nov 2004
    Posts
    34

    Sending email using JSP

    Hi

    I have a web application that requires me to send (not required to receive) emails to others. Is it possible to use JSP to do it and if so, are there any other tools required for it? Is ASP a better alternative for this?

    Thanks

  2. #2
    Join Date
    Nov 2004
    Posts
    1
    HI,
    If IIS server is used than it is very easy to send mails by using CDONT in ASP page.
    enjoy

  3. #3
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Sending mail is pretty simple using the JavaMail bundled with the newer versions (since 1.3 I think). You can do it directly from a JSP but the proper way to do web design is the Model View Controller pattern (JSP2) where the JSP is the View. The form on the JSP would contain a text area for the body of the email and potentially Subject and To inputs. The servlet handling the form would create a mail object, set the to, subject and body then send it (by default using the SMTP server the host system uses).

    The JSP1.5 way to do it would be create a mailer bean that the JSP would use to send the mail in the same way as above except using the the CGI style "post to itself" form handler.

  4. #4
    Join Date
    Aug 2003
    Posts
    74
    Hi,

    I'm trying to send email from my JSP page in HTML format, I don't know how to do it.

    Kindly give me some solution, heres my code

    <%@page contentType="text/html"%>
    <%@ page import="util.*" %>



    <%

    String Email = request.getParameter("Email");
    String FirstName = request.getParameter("FirstName");
    String LastName = request.getParameter("LastName");
    String PhoneNumber = request.getParameter("PhoneNumber");
    String Country = request.getParameter("Country");
    String Occasion = request.getParameter("Occasion");
    String Age = request.getParameter("Age");
    String Gender = request.getParameter("Gender");
    String Interests = request.getParameter("Interests");
    String PriceRange = request.getParameter("PriceRange");
    String Colors = request.getParameter("Colors");
    String Relationship = request.getParameter("Relationship");


    String mailbody = "<b>Your Information<br>";
    mailbody = mailbody + "Email" + Email + "<br>";
    mailbody = mailbody + "First Name" + FirstName + "<br>";
    mailbody = mailbody + "Last Name" + LastName + "<br>";
    mailbody = mailbody + "Phone Number" + PhoneNumber + "<br>";
    mailbody = mailbody + "Country" + Country + "<br>";

    mailbody = mailbody + "<b>Gifting Information<br>";
    mailbody = mailbody + "Occasion" + Occasion + "<br>";
    mailbody = mailbody + "Recipient Age" + Age + "<br>";
    mailbody = mailbody + "Recipient Gender" + Gender + "<br>";
    mailbody = mailbody + "Interests" + Interests + "<br>";
    mailbody = mailbody + "Price Range" + PriceRange + "<br>";
    mailbody = mailbody + "Favorite Colors" + Colors + "<br>";
    mailbody = mailbody + "Relationship to You" + Relationship + "<br>";

    String to = "akrashdi@yahoo.com";
    String subject = "Gift Consultant";

    SendMail sendMail = new SendMail();
    sendMail.sendMail(to, Email, subject, mailbody, "localhost");
    SendMail.setContent(mailbody, "text/html");

    %>

  5. #5
    Join Date
    Dec 2005
    Posts
    38
    Create a javamail bean and use it to send mail.
    See following code

    Code:
    import java.io.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
        
    public class SendEmail {
      
        public static void send(String smtpHost, int smtpPort,
                                String from, String to,
                                String subject, String content)
                    throws AddressException, MessagingException {
    
            // Create a mail session
            java.util.Properties props = new java.util.Properties();
            props.put("mail.smtp.host", smtpHost);
            props.put("mail.smtp.port", ""+smtpPort);
            Session session = Session.getDefaultInstance(props, null);
        
            // Construct the message
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            msg.setSubject(subject);
            msg.setText(content);
        
            // Send the message
            Transport.send(msg);
        }
        
        public static void main(String[] args) throws Exception {
            // Send a test message
            send("hostname", 25, " glen@myserver.com ", " joke@jokellogic.com ",
                 "Hello", "Hello, \n\n How are you ?");
        }
    }
    See Javamail tutorial

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles