Click here to Skip to main content
12,818,964 members (29,957 online)
Rate this:
Please Sign up or sign in to vote.
See more: Java

// File Name

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 = "";

      // Sender's email ID needs to be mentioned
      String from = "";

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

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

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

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

         // 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.
                                  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
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {

error occurring is shown in image[^]

// i have classpath pointing to mail.jar and activation.jar
Posted 6-Feb-13 4:29am
Updated 6-Feb-13 4:49am
ryanb31 6-Feb-13 9:50am
Is SMPT actually configured on localhost? Try this,
CurrentlyBE 6-Feb-13 23:24pm
I refer the given link.
1. On a computer running Windows Server 2003, type Telnet at a command prompt, and then press ENTER.
2. At the telnet prompt, type set LocalEcho, press ENTER, and then type open <"machinename"> 25, and then press ENTER.

output: ESMTP Server (Microsoft Exchange Internet Mail Service 5.5.2651.58) ready

Didn't get this infact got error connection failed. Was it because I'm using winXP or SMTP is not correctly configured. (how to configure it correctly)
CurrentlyBE 13-Feb-13 11:24am
error is occuring on statement " Transport.send(message); "

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

check this tutorial:

Fundamentals of the JavaMail API[^]
All about JavaMail[^]

pretty good, it's all in there.

A web email account like gmail will not work for development, as they let you demand access on a limited base and will ban you when you fail with your login.

If you need a email server try HMail Server:[^] (auto-ban option to be switched off in settings!)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170308.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100