• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Mail

palmari sivasubramanian
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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("mail.to");
if (to == null)
System.out.println("Property mail.to 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) i.next();
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(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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!