How to Send an Email Message Using Java Mail APIThere can be many situations when the need of sending email to the user/client arises while using the application for example when a user registers on a site, generally a confirmation mail or a password verification mail is sent to him. So it is very common while performing an e-commerce based transaction to send or receive data through emails. In a J2EE based web application Java Mail API is an easy method to send and receive email. Here we will discuss the procedure of sending email using Java Mail API. For accomplishing this we need Java Activation Framework(JAF) and JavaMail API to be downloaded on our system. There are three basic steps to be performed for sending the mail. First of all the j2ee component which we are using to do the task( eg. Servlet) needs to create a session. Then next step should be of developing the email message. There are different protocols which define the standard way for formatting an email message. For example we have MIME, SMTP or POP3. Each of these have different features and capabilities of formatting and transport of email message. Client and dedicated servers can use any of the protocol and JavaMail API needs to interact with these protocols for performing its task. After creating the email, third step is to send the email. Consider the following example for understanding this three step process in detail: import javax.mail.*; public class SendingMail In the above described example, we have described three String objects: frm,to and myhost. frm is assigned the email address of sender and to has the recipient’s mail address. myhost is assigned the smtp host. Here we are assuming that email formatting is done by using MIME (Multipurpose Internet Mail Extension), so MimeMessage object is created. put() method is used to set the smtp host. After setting the host, session is created using getDefaultInstance() method which is referred as sess1. As clear from the example MimeMessage object is referred as mesg. After this ‘frm’ and ‘to’ objects are converted to InternetAddress objects from String objects. Then we have used setFrom() and addRecipient() methods where these frm and to objects are passed as arguments. This is done in order to set the email address. After this we have used setSubject() method for passing the subject line of message and then setText() method to set the email body. Finally send () method of Transport object is used to send the email message.
| All About Different Types of XML Editors | Generating XML Document Using JSP | How to Retrieve an Email Message Using Java Mail API | How to Send an Email Message Using Java Mail API | Transforming XML Data with XSLT | Understanding Basic Components of a JMS Program | Understanding Basic Technology of ebXML | Understanding Deployment Descriptor in Reference with Servlets | Understanding eXtensible Access Control Markup Language (XACML) | Understanding the Design Goals of XML | Understanding XML Common Biometric Format | |
|
|||
|
||||
“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”
Copyright - © 2004 - 2019 - All Rights Reserved.