How To Send Email In Java Using Gmail SMTP? | Pepipost

the author voluntarily contributed this tutorial as part of the pepipost write-to-contribute program.

intro

Sending email is the most common and necessary requirement for most applications. java provides java mail api: a platform and protocol independent framework for building mail and messaging applications.

The reference implementation of javamail is licensed under the Common Development and Distribution License (CDDL) v1.1 and the GNU General Public License (GPL) v2 with the exception of classpath.

In this guide, you will get detailed steps on how to configure javamail in your java project and implement javamail api to create and send emails in smtp protocol.

requirements

  • eclipse idea
  • java runtime environment

step 1: create a new expert project

  1. click file > new > project > expert > expert project

  1. select create a simple project (skip archetype selection) and click finish.

  1. provide the artifact id and group id to your project.

  1. this will give the default project structure as shown below:

  1. a default pom.xml is created:
  1. add the following dependencies in your pom.xml file.

this will add javax.mail.jar 1.6.2 to your project resources.

final

pom.xml:

step 2: create a java class

  1. right click on the project and create a new package and name it com.sendemail

  1. right click the package and create the sendmail class check to create the main method:

In your code, we would need to import the following packages and classes.

  • import java.util.properties: the properties class represents a persistent set of properties. properties can be saved to a stream or loaded from a stream.
  • import javax.mail.message: This class models an email message. To send a message, an instance of the message subclass (for example, mimemessage) is created, the attributes and content are filled in, and the message is sent using the transport.send method.
  • import javax.mail.messagingexception :this is the base class for all exceptions thrown by messaging classes
  • import javax.mail.passwordauthentication:this class it is simply a repository for a username and password.
  • import javax.mail.session:the session class represents a mail session.
  • import javax.mail.transport:this is an abstract class that models a message transport.
  • import javax.mail.internet.internetaddress:this class represents an internet email address using rfc822 syntax
  • import javax.mail.internet.mimemessage: this class represents a c message mime-style e-mail. implements the message abstract class and the mimepart interface.
See Also:  Mariner Finance check in the mail? Discover more today!

send emails through java using gmail smtp

The following is the complete java code to send emails using the gmail smtp server, with the description of each line:

if you want to send html content to replace message.settext(“this is the actual message”) with the following code:

step 3: test the java code

run the java application and an email will be sent to the recipient. your console will look like this.

You have successfully sent an email using your java code.

Now we are going to send an attachment to your email.

optional steps

Step 4: Send an email with an attachment.

To send an email with an attachment you will have to import a few more classes.

1. import java.io.file and import java.io.ioexceptionthe file class is an abstract representation of file and directory path names and the ioexception class is a general class of exceptions thrown by i/o operations s failed or interrupted.

2. import javax.mail.internet.mimemultipartThis class is an implementation of the abstract multipart class that uses mime conventions for data.

3. import javax.mail.internet.mimebodypartthis class represents a body part mime.mimebodypart uses the internetheaders class to parse and store the headers for that body part.

below is the java code to send an attachment in the email.

the code has two mime body parts, one containing an attachment and one containing text for the email, this is added in multipart, then this multipart is used to set as content of the message.

See Also:  How can i send an email to a google contact group on my ios devices?

here is some running java code to send email attachments:

java code to send email attachments

conclusion

You have now successfully implemented the java mail api and can send email using any smtp server in your project.

Leave a Reply

Your email address will not be published. Required fields are marked *