Java Send Email

You are free to send e-mail using your Java program. Before start with sending email in Java, make sure that JavaMail API and Java Activation Framework (JAF) is installed on your platform properly

Java Send Email Example

Here is an example, sends simple email in Java. This is file:

/* Java Program Example - Java Sending Email

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

public class JavaSimpleEmail
   public static void main(String [] args)
      String to = "";
      String from = "";
      String host = "localhost";
      Properties properties = System.getProperties();
      properties.setProperty("", host);
      Session session = Session.getDefaultInstance(properties);
         MimeMessage msg = new MimeMessage(session);
         msg.setFrom(new InternetAddress(from));
         msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
         msg.setSubject("This is the Subject Line");
         msg.setText("This is the actual message");
         System.out.println("Message sent successfully....");
      catch(MessagingException mex)

Now compile and run this program to send a simple e-mail:

$ java JavaSimpleEmail
Message sent successfully....

Java Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube