File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes Java Mail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail" Watch "Java Mail" New topic

Java Mail

palmari sivasubramanian

Joined: Mar 07, 2006
Posts: 6
Hi All,

I am working with Java mail using mail.jar and activation.jar.
This is the part of my code:

public static void sendMail(String subject, String message, Properties mailProps)
throws MessagingException

if (!mailProps.containsKey(TRANSPORT))
mailProps.put(TRANSPORT, "smtp");

if (!mailProps.containsKey(MAIL_SMTP_HOST))
mailProps.put(MAIL_SMTP_HOST, "mailhost");

if (!mailProps.containsKey(MAIL_FROM))
mailProps.put(MAIL_FROM, "from address ");
System.out.println("mail from"+mailProps.get(MAIL_FROM));

String to = mailProps.getProperty("");
if (to == null)
System.out.println("Property is not set, unable to send mail.");

String[] toArray = StringUtil.split(to, DELIMITERS);
javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(mailProps, null);
MimeMessage mimeMessage = new MimeMessage(mailSession);
Address[] toAddresses = new Address[toArray.length];
for(int i = 0; i < toArray.length; i++)
System.out.println("ADDRESS: " + toArray[i]);
toAddresses[i] = new InternetAddress(toArray[i]);

mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, toAddresses);

Set propKeys = mailProps.keySet();
for(Iterator i = propKeys.iterator(); i.hasNext())
String propKey = (String);
if (propKey.startsWith("mail.header."))
mimeMessage.setHeader(propKey.substring("mail.header.".length()), mailProps.getProperty(propKey));

System.out.println("mail sent");


i am getting error in
ADDRESS: to address i recevied
javax.mail.MessagingException: Could not connect to SMTP host:, port: 25, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(
at com.sun.mail.smtp.SMTPTransport.protocolConnect(
at javax.mail.Service.connect(
at javax.mail.Service.connect(
at javax.mail.Service.connect(
at com.sun.mail.smtp.SMTPTransport.connect(
at javax.mail.Transport.send0(
at javax.mail.Transport.send(
at com.Util.sendMail(
at com.mainClass.main(

I try to ping the host name. it is pinging. I have tried that host name with ip address also. Still i am getting this exception. If anyone tells me what i am wrong in this code, it will be useful for me.

Thanks in advance.
[ June 08, 2007: Message edited by: palmari sivasubramanian ]
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Ping doesn't use SMTP, so it is a mechanism to find out if a server exists and is responsive, but wont tell you if you can connect via SMTP. Are you behind a firewall? Do you access your SMTP server via a proxy? Can you telnet to the SMTP server?
[ June 08, 2007: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link:
subject: Java Mail
It's not a secret anymore!