How can I send an email by Java application using GMail, Yahoo, or Hotmail?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When developing a Java application, it's often necessary to include functionality for sending emails. This can be used for notifications, reports, or any other communication needs. JavaMail API is a standard library for sending and receiving emails within Java applications. In this article, we'll explore how to use JavaMail API to send emails through popular services like Gmail, Yahoo, or Hotmail (Outlook).
Prerequisites
Before we start, ensure you have the following:
- JDK installed (Java Development Kit).
- JavaMail API library.
- Access credentials for Gmail, Yahoo, or Hotmail.
JavaMail API Setup
First, you need to download the JavaMail API and include it in your project. You can either add it manually to your classpath or use a dependency management tool like Maven or Gradle.
Maven Dependency
Gradle Dependency
Sending an Email
The general process to send an email involves creating a session, constructing the email message, and sending it through a SMTP server.
General Steps
- Set up Properties: Configure the SMTP properties.
- Create a Session: Use the properties and authenticate with the email provider.
- Compose the Email: Define the message and its content.
- Send the Email: Use the
Transportclass to send the message.
Example Code
Here's an example of how you can send an email using JavaMail API through Gmail:
Notes
- Security: Gmail uses OAuth 2.0 for authentication. You should not use a raw password; instead, use AppPasswords or OAuth tokens.
- Firewall and Less Secure Apps: Ensure that your Google Account allows less secure apps or set up a firewall exception. Note that "Less Secure Apps" should be avoided whenever possible due to security concerns.
Modifications for Yahoo and Hotmail
Yahoo
For Yahoo, the properties will slightly change:
Hotmail (Outlook)
For Hotmail, adjust the properties as follows:
Summary
The table below summarizes the key configurations for each email provider:
| Provider | SMTP Host | SMTP Port | TLS Enable | Auth Required |
| Gmail | smtp.gmail.com | 587 | true | true |
| Yahoo | smtp.mail.yahoo.com | 587 | true | true |
| Hotmail | smtp-mail.outlook.com | 587 | true | true |
Additional Topics
Handling Attachments
If you need to send attachments, use MimeBodyPart and Multipart to construct multi-part messages.
Exception Handling
Implement robust exception handling to catch and troubleshoot MessagingException and other potential issues.
Security Enhancements
Use OAuth 2.0 for a more secure method of authentication instead of email/password pairs.
By setting up the JavaMail API correctly, you can seamlessly integrate email functionality into your Java applications using Gmail, Yahoo, or Hotmail providers. This powerful API allows for a range of email-handling capabilities, making it ideal for enterprise and personal projects alike.

