Sending gmail or mail in java

Saturday, January 30th, 2016 (0 Comment)

There are many guides about sending mail in java or other languages on the internet. In this article, we’ll learn the most basic to send mail using java language. We will focus on two parts. The first is sending mail using gmail and the second is sending mail using its own host (your corporate mail)

To send an mail in java is very simple. You need to have JavaMail API (I use mail-1.4.3.jar library). You can download library activation.jar and mail.jar then add into your project to use.

1. Import necessary libraries for mail in java

2. Sending mail by gmail

Example basic for sending mail using host mail of google

  • Line 3: declare host mail of google
  • Line 5 – 6: username is your gmail and your password
  • Line 14 – 20: declare some configuration of javax.mail
  • Line 23 – 27: create a session with authentication

3. Sending mail by own host mail

The second example: using own host mail (your company mail)

  • Line 3: declare own host mail
  • Line 5: username to login host mail
  • line 20: for server email use mail SSL

If you want to send to multiple mail, you can use function below:

Address[] addresses : this is array email. You need use InternetAdderss() method to confirm Email ID.

Tags: ,

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Leave a Reply

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