YouTip LogoYouTip

Jsp Sending Email

Although implementing email sending functionality with JSP is quite simple, it requires the JavaMail API and the JavaBean Activation Framework. * You can download the latest version of (http://www.oracle.com/technetwork/java/javamail/index.html) from the Java website. On the right side of the webpage, there is a **Downloads** link; click it to download. * You can download the latest version of [JAF (version 1.1.1)](http://www.oracle.com/technetwork/articles/java/index-135046.html) from the Java website. You can also use the download links provided on this site: * [JavaMail mail.jar 1.4.5] * [JAF (version 1.1.1) activation.jar] Download and unzip these files. In the root directory, you will see a series of jar packages. Add the `mail.jar` and `activation.jar` packages to the CLASSPATH variable. ## Sending a Simple Email This example demonstrates how to send a simple email from your machine. It assumes that localhost is connected to the network and capable of sending an email. Meanwhile, please confirm once again that the `mail.jar` and `activation.jar` packages have been added to the CLASSPATH variable. Send Email using JSP

Send Email using JSP

Now visit http://localhost:8080/SendEmail.jsp, and it will send an email to abcd@gmail.com and display the following result: Send Email using JSP Result: Sent message successfully.... If you want to send an email to multiple recipients, the following method can be used to specify multiple email addresses: void addRecipients(Message.RecipientType type, Address[] addresses)throws MessagingException The parameters are described as follows: * type: This value will be set to TO (recipient), CC, or BCC. CC stands for Carbon Copy, and BCC stands for Blind Carbon Copy. The example program uses TO. * addresses: This is an array of email addresses. When specifying email addresses, the InternetAddress() method needs to be used. * * * ## Sending an HTML Email This example sends a simple HTML email. It assumes that your localhost is connected to the network and capable of sending an email. Meanwhile, please confirm once again that the `mail.jar` and `activation.jar` packages have been added to the CLASSPATH variable. This example is very similar to the previous one, but in this example, we use the `setContent()` method, passing "text/html" as the second parameter to indicate that the message contains HTML content. <% String result; // Recipient's email address String to = "abcd@gmail.com"; // Sender's email address String from = "mcmohd@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties object Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // Get default Session object. Session mailSession = Session.getDefaultInstance(properties); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(mailSession); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Set HTML message message.setContent("

This is actual message

", "text/html" ); // Send message Transport.send(message); result = "Sent message successfully...."; }catch (MessagingException mex) { mex.printStackTrace(); result = "Error: unable to send message...."; } %>Send HTML Email using JSP

Send Email using JSP

Now you can try using the above JSP file to send an email with an HTML message. * * * ## Including Attachments in an Email This example shows us how to send an email with an attachment. Send Attachement Email using JSP

Send Attachement Email using JSP

* * * ## User Authentication Section If the mail server requires a username and password for user authentication, you can set it up like this: properties.setProperty("mail.user", "myuser"); properties.setProperty("mail.password", "mypwd"); * * * ## Sending Email Using a Form Use an HTML form to receive an email and obtain all email information via the request object: String to = request.getParameter("to");String from = request.getParameter("from");String subject = request.getParameter("subject");String messageText = request.getParameter("body"); After obtaining the above information, you can use the examples mentioned earlier to send the email.
← Jsp JstlJsp Auto Refresh β†’