dcsimg
SMTP Server excption while sending (Could not connect to SMTP host)
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   parag_dharmadhikari
Posted On:   Wednesday, March 5, 2003 06:49 AM

Dear All, I am sending a mail using SMTP protocol which is behaving irregularly. It some time works and sometimes not. It gives error as class javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect I m using Tomcat4/Servlet for it. My code is package com.irislogic.bsn.mail.activity; import javax.mail.*; import javax.mail.internet.*; import java.util.*; //application imports import com.irislogic.bsn.mail.controldata.ServerData; impo   More>>

Dear All,



I am sending a mail using SMTP protocol which is behaving irregularly.


It some time works and sometimes not.

It gives error as


class javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

nested exception is:

java.net.ConnectException: Connection refused: connect

I m using Tomcat4/Servlet for it.

My code is



package com.irislogic.bsn.mail.activity;


import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;


//application imports

import com.irislogic.bsn.mail.controldata.ServerData;

import com.irislogic.bsn.mail.controldata.MailData;

import com.irislogic.bsn.waf.exception.MailException;


public class SendMail {

public static void sendMail(ServerData serverData, MailData mailData) {
Properties props = System.getProperties();

// -- Attaching to default Session, or we could start a new one --
System.out.println(
"ip in send: " + serverData.getServerIPAddr().trim());
String smtpServer = serverData.getServerIPAddr().trim();

props.put("mail.smtp.host", smtpServer);

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

session.setDebug(true);
System.out.println("1");
// -- Create a new message --
Message msg = new MimeMessage(session);
System.out.println("2");
// -- Set the FROM and TO fields --
//msg.setFrom(new InternetAddress(mailData.getFrom()));
try {
msg.setFrom(new InternetAddress("parag@bsil.com"));
System.out.println("3");
msg.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(mailData.getToAddress().trim(), false));
System.out.println("4");

// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));

// -- Set the subject and body text --
msg.setSubject(mailData.getSubject().trim());
System.out.println("5");
msg.setText(mailData.getBody().trim());
System.out.println("6");
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
System.out.println("7: "+props.getProperty("mail.smtp.host"));
// -- Send the message --
Transport.send(msg);
//session.notifyAll();

System.out.println("Message sent OK.");

} catch (SendFailedException s) {
System.out.println("senderr: " + s.getMessage());
throw new MailException("Error in SendFailed");
} catch (MessagingException emsg) {
throw new MailException("Error in Message Sending");
}

}

}





The same code worked fine on dos prompt.On tomcat it is weird as sometime it works or else some time gives error as "Could not connect to SMTP host"


Regards
Parag

   <<Less

Re: SMTP Server excption while sending (Could not connect to SMTP host)

Posted By:   iulius_hide  
Posted On:   Thursday, March 13, 2003 12:30 AM

i had the same problem.At the end try this:
Transport transport=session.getTransport("smtp");
transport.connect(outMailServer,user,password);
transport.sendMessage(newMessage,newMessage.getAllRecipients());
transport.close(); <---it'a a must
transport object must be closed
Good luck!!!
Iulius
About | Sitemap | Contact