Sending Email using Java - TechDoko

Hot

Post Top Ad

Sending Email using Java

To send an email using Java Application, you need JavaMail API and Java Activation Framework (JAF). You can download the latest version of JavaMail API from Java's website. Click here to open the Java's website. For the download of latest version of JAF you can click here.

Just download and extract those files in your machine. You will find number of files but you only need mail.jar and activation.jar. Just add those files in your project CLASSPATH.

Send a Simple E-mail

Before sending a simple email, first you have to connect your machine with the internet. Here is a sample code for sending a simple email. Create a project in any IDE (I am using Eclipse) and create a java file named as SendEmail.java and paste the below code.

// File Name SendEmail.java

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail {
   public static void main(String [] args) { 
      // Recipient's email ID needs to be mentioned.
      String to = "abc@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "xyz@gmail.com";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO, new
         InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");

      } catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

Now just run this file as Java Application and your email will be sent to the recipient's email address.

If you have multiple recipient for sending the email, then you can specify the multiple email id as follows:

void addRecipients(Message.RecipientType type, Address[] addresses)throws MessagingException

Here, type can be set to TO, CC or BCC. CC represents Carbon Copy and BCC represents Black Carbon Copy. Example, Message.RecipientType.TO and addresses is the array of email ID. You have to use InternetAddress() method for specifying email IDs.

User Authentication

If sending email fails due to required authentication to the mail server then you can provide the user authentication by following ways and the rest of the process is as it is.


properties.setProperty("mail.user", "myuser");
properties.setProperty("mail.password", "mypwd");

No comments:

Post a Comment