0

I am creating an application where I want to send an email to my clients.When i compiled the below code its ok but when i run it gives me error as follows

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

public class SendEmail
{
   public static void main(String [] args)
   {   

     String to = "prakash_d22@rediffmail.com";    
      String from = "web@gmail.com";    
      String host = "localhost";    
      Properties properties = System.getProperties();    
      properties.setProperty("smtp.gmail.com", host);   
          Session session = Session.getDefaultInstance(properties); 
      try{         
         MimeMessage message = new MimeMessage(session);     
         message.setFrom(new InternetAddress(from));        
         message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));      
         message.setSubject("This is the Subject Line!");        
         message.setText("This is actual message");       
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

error: enter image description here

Please guide me.

prakash_d22
  • 1,153
  • 5
  • 21
  • 34
  • I think you need to set a server property for the message. (I don't know the exact property, which is why this is a comment, not an answer). As you can see, it tries to connect to the mail server on localhost...and unless you've got a mail server running on your own computer, this call can only fail. – ATaylor Aug 17 '12 at 04:52
  • I don't think this is correct `properties.setProperty("smtp.gmail.com", host);`. Check [this answer](http://stackoverflow.com/a/11254276/422353) – madth3 Aug 17 '12 at 04:54
  • May I also suggest, telling us, where your 'Transport' variable comes from? The way I understand the docs, you'd have to pass the session with the mail parameters to that, so it will know, where to send to. – ATaylor Aug 17 '12 at 04:57
  • here is solution that work for me http://stackoverflow.com/questions/3649014/send-email-using-java/14973045#14973045 – Kirit Vaghela Feb 20 '13 at 06:03

2 Answers2

2
            String host = "smtp.gmail.com";

            Properties properties = new Properties();

set following properties

        properties .put("mail.smtp.starttls.enable", "true");
        properties .put("mail.smtp.host", host);
        properties .put("mail.smtp.user", username);
        properties .put("mail.smtp.password", password);
        properties .put("mail.smtp.port", "587");
        properties .put("mail.smtp.auth", "true");
happy
  • 2,550
  • 17
  • 64
  • 109
0

Have you read the Fundamentals of the JavaMail API?

Anyways, from what I can tell the issue is that you're using invalid configuration.

properties.setProperty("smtp.gmail.com", host);

As you can see in the JavaMail API documentation, JavaMail does not support a property named smtp.gmail.com. What you probably intended was actually...

properties.setProperty("mail.smtps.host", host);

I suspect you wish to use Gmail's SMTPS server, not one hosted on localhost as you have it now, so I'd advise changing your code such that...

final String host = "smtp.gmail.com";

You also wish to use authnetication, which JavaMail suggests you can do in their FAQ on Gmail as follows:

properties.setProperty("mail.smtps.auth", "true");

Note that Gmail requires authenticating to send mail as such. It appears another answer suggested you can configure the username/password using the session properties; unfortunately, this is incorrect.

What you want to do is use an Authenticator.

final Session session = Session.getInstance(properties, new Authenticator() {

  static final PasswordAuthentication AUTH = new PasswordAuthentication(USER, PASS);

  protected PasswordAuthentication getPasswordAuthentication() {
    return AUTH;
  }
});
obataku
  • 29,212
  • 3
  • 44
  • 57