Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
// 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 = "abcd@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();
      }
   }
}

error occurring is shown in image
http://postimage.org/image/qesglw7l1/[^]


// i have classpath pointing to mail.jar and activation.jar
Posted
Updated 6-Feb-13 3:49am
v2
Comments
ZurdoDev 6-Feb-13 9:50am    
Is SMPT actually configured on localhost? Try this, http://support.microsoft.com/kb/323350
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: computername.microsoft.com 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

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: http://www.hmailserver.com/[^] (auto-ban option to be switched off in settings!)
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900