Sending a mail with java
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   khaliduddin_shaik
Posted On:   Tuesday, May 29, 2007 09:11 AM

Hi all, I'm new to Javamail concept and i got the following exceptions when i tried to send a simple mail. First exception is : using java Could not connect to SMTP host: smtp.gmail.com, port: 25; Second Exception is : java.net.SocketException: Software caused connection abort: connect i'm using Eudora as my mail client and its working fine. what could be the problem with port. Also, Does a mail client should necessarily exist on the local machine to send mail thru java? My code is like this: import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendMail {    More>>

			
Hi all,

I'm new to Javamail concept and i got the following exceptions when i tried to send a simple mail.

First exception is :
using java Could not connect to SMTP host: smtp.gmail.com, port: 25;

Second Exception is :
java.net.SocketException: Software caused connection abort: connect

i'm using Eudora as my mail client and its working fine.
what could be the problem with port.

Also,
Does a mail client should necessarily exist on the local machine to send mail thru java?

My code is like this:

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

public class SendMail
{
public static void main(String[] args)
{
if(args.length!=3)
{
System.out.println("Usage: SendMail SMTPHost ToAddress FromAddress");
System.exit(1);
}

String smtpHost = args[0];
String to = args[1];
String from = args[2];

Properties props = System.getProperties();
props.put("mail.smtp.host",smtpHost);

try{
Session session = Session.getDefaultInstance(props, null);

MimeMessage message = new MimeMessage(session);
message.setText("Hi from the J2EE in 21 Days authors");
message.setSubject("Hi!");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

Transport.send(message);
System.out.println("Message Sent");
}
catch(MessagingException me)
{
System.err.println("Messaging Exception : "+me.getMessage());
}
}
}

Please help me ............!
   <<Less

Re: Sending a mail with java

Posted By:   Robert_Lybarger  
Posted On:   Tuesday, May 29, 2007 02:00 PM

Well at least you got as far as working code and exception ... I was figuring this was another "how does javamail work" question. Anyway, an SMTP server (such as google) can be configured such that you have to have permission to connect to it. One option is SMTP-level authentication. I didn't see you account for that, and google mail help would probably give you clues if it is required. Another option is a scheme called "POP before SMTP" where you login via POP mode to establish your credentials first, and they are cached ever so briefly for a follow-up SMTP session. More sources of headaches are that Google runs some of their mail services on non-standard ports -- I'm not saying it "isn't" port 25, but saying I'm not sure it "is" either. Best way to know is to open a telnet connection to that server on that port and see what happens. Secondary would be to see how your existing mail client is setup -- server name, port, authentication info, etc.
About | Sitemap | Contact