aspose file tools*
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
Author

Java Mail

palmari sivasubramanian
Greenhorn

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");
System.out.println("transport"+mailProps.get(TRANSPORT));
}

if (!mailProps.containsKey(MAIL_SMTP_HOST))
{
mailProps.put(MAIL_SMTP_HOST, "mailhost");
System.out.println("MAIL_SMTP_HOST"+mailProps.get(MAIL_SMTP_HOST));
}

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

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

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);
mimeMessage.setSubject(subject);
mimeMessage.setText(message);

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

Transport.send(mimeMessage);
System.out.println("mail sent");

}


i am getting error in
ADDRESS: to address i recevied
javax.mail.MessagingException: Could not connect to SMTP host: 10.12.32.206, port: 25, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1140)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:288)
at javax.mail.Service.connect(Service.java:233)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:138)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at com.Util.sendMail(Util.java:87)
at com.mainClass.main(mainClass.java:18)

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
Bartender

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Java Mail