0% found this document useful (0 votes)
7 views

13.0_Sending_mails_with_JAVA_Mail_API

The document provides instructions on how to send emails using the Java Mail API, including setting up a Gmail account with 2-step verification and creating an App Password. It outlines the necessary steps to download the required .jar files, configure the Gmail account, and write Java code to send emails. The code setup includes configuring email properties, defining credentials, creating an email message, and handling exceptions during the sending process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

13.0_Sending_mails_with_JAVA_Mail_API

The document provides instructions on how to send emails using the Java Mail API, including setting up a Gmail account with 2-step verification and creating an App Password. It outlines the necessary steps to download the required .jar files, configure the Gmail account, and write Java code to send emails. The code setup includes configuring email properties, defining credentials, creating an email message, and handling exceptions during the sending process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Sending mails

With
JAVA Mail API
• To send an email using the Java Mail
API, we need to include the JavaMail
API libraries in our project.

• We can download the JavaMail API from


the Oracle website
Steps :

1) We will use “gmail” server to send mails.


To allow our gmail account to send mails,
we need to activate the 2-step-verification
and create an “App Password”. We will
use this password to send mails
2) Download required “.jar” files (API
libraries)
3) Write JAVA code to send mail
Configuring Gmail Account for
“App” password
Go to “Manage Your Google Account”  Security  2-Step Verification (activate it)
 After activating it, you will get “App Passwords” option  Set the App Password
(we will use this to send mails)
Downloading .Jar Files (API)
Download the following two .JAR files :

 Activation-1.1.jar
 Javax.mail-1.6.1.jar

Download the latest version of both the files, if


possible.
Writing JAVA code
Add .jar reference
Create a new JAVA project in NetBeans.
Add reference to “activation-1.1.jar” and “javax.mail-
1.6.1.jar”
Writing JAVA code
Writing JAVA code
(sendMail.java)
#24  Specify App Password for gmail here.
Writing JAVA code
Break-down of the code
Writing JAVA code

The code given above ….

1) sets up email configuration properties,

2) defines email credentials,

3) creates an email message with sender, recipient,


subject, and content, and then

4) sends the email using the JavaMail API.

5) It also handles exceptions that might occur during the


email sending process.
This code defines a Java package named javaapplication5 and imports classes from
the JavaMail API and related packages, which are required for sending emails.
• These lines configure properties required for sending an email using the
JavaMail API.

• These properties specify the SMTP server, its port, and enable authentication
and TLS (Transport Layer Security) for secure communication.
• This code creates a Session object, which represents a mail session, using the
properties defined earlier.

• An Authenticator is used to provide the email credentials (username and


password) for SMTP server authentication.

• The getPasswordAuthentication method of the Authenticator is overridden


to return the credentials.
• A MimeMessage object is created to represent the email
message.
• The sender's email address is set using message.setFrom().
• The recipient's email address is set using
message.setRecipients().
• The subject of the email is set using message.setSubject().
• The email content (message body) is set using
message.setText().

You might also like