" + title + "
n" + "" + res + "
n" + ""); }catch (MessagingException mex) { mex.printStackTrace(); } } } Now let us compile the above Servlet and create the following entries in the web.xml file: ```xml .... SendEmail SendEmail SendEmail /SendEmail .... Now call this Servlet by the URL http://localhost:8080/SendEmail. This will send an email to the given email ID _abcd@gmail.com_ and will display the following response: ## Send Email Successfully sent message... If you want to send an email to multiple recipients, then use the following method to specify multiple email IDs: ```java void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException Here is the description of the parameters: * **type:** This is required to be set to TO, CC, or BCC. Here CC represents Carbon Copy and BCC represents Black Carbon Copy. For example _Message.RecipientType.TO_. * **addresses:** This is the array of email ID. You need to use InternetAddress() method while specifying email IDs. The following example will send an HTML email from your computer. It is assumed that your **localhost** is connected to the internet and supports sending emails. Also, ensure that all the jar files for the Java Email API package and JAF package are available in the CLASSPATH. This example is very similar to the previous one, but here we are using setContent() method to set the content with the second parameter as "text/html", which specifies that HTML content is contained in the message. Using this example, you can send HTML content of any size. ```java // File Name SendEmail.java import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendEmail extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned. String from = "web@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); // Set response content type response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // 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 the actual message message.setContent("This is actual message
", "text/html" ); // Send message Transport.send(message); String title = "Send Email"; String res = "Successfully sent message..."; String docType = " n"; out.println(docType + "n" + "" + title + "
n" + "" + res + "
n" + ""); }catch (MessagingException mex) { mex.printStackTrace(); } } } Compile and run the above Servlet to send an HTML message to the given email ID. The following example will send an email with an attachment from your computer. It is assumed that your **localhost** is connected to the internet and supports sending emails. Also, ensure that all the jar files for the Java Email API package and JAF package are available in the CLASSPATH. ```java // File Name SendEmail.java import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendEmail extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned. String from = "web@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); // Set response content type response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // 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!"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText("This is message body"); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart ); // Send message Transport.send(message); String title = "Send Email"; String res = "Successfully sent email..."; String docType = " n"; out.println(docType + "n" + "" + title + "
n" + "" + res + "
n" + ""); }catch (MessagingException mex) { mex.printStackTrace(); } } } Compile and run the above Servlet to send a message with a file attachment to the given email ID. If you need to provide a user ID and password for authentication to the email server, then you can set the following properties: ```java props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd"); The rest of the email sending mechanism remains the same as explained above.
YouTip