Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 6-Feb-13 3:29am
Edited 6-Feb-13 3:49am
RyanDev111.4K
v2
Comments
ryanb31 at 6-Feb-13 9:50am
   
Is SMPT actually configured on localhost? Try this, http://support.microsoft.com/kb/323350
CurrentlyBE at 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 at 13-Feb-13 11:24am
   
error is occuring on statement " Transport.send(message); "

1 solution

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

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

  Print Answers RSS
0 Gihan Liyanage 332
1 Sergey Alexandrovich Kryukov 230
2 ClimerChinna 222
3 vikinghunter 178
4 OriginalGriff 161
0 Sergey Alexandrovich Kryukov 8,373
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,025
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2014
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