Could not connect to SMTP host: localhost, port: 25?
Asked Answered
S

1

0

am using liferay 6 and created one custom class..i want to create mail notification function...I have written following code in my class

private void SendEmail(NotificationObject pNotificatonObj,
            String[] pReciepientAddresses) throws MessagingException {

        log.info("In SendMail");
        Properties props = new Properties();
        props.put("mail.debug", "true");
        props.put("mail.smtp.socketFactory.fallback", "false");
        Session session = Session.getInstance(props);
        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(
                pNotificatonObj.get_From());
        msg.setFrom(addressFrom);
        // InternetAddress addressTo = new
        // InternetAddress(pNotificatonObj.get_To());

        InternetAddress[] addressTo = new InternetAddress[pReciepientAddresses.length];
        log.info("ADDRESS ARRAY LENGTH In Send Mail: - " + pReciepientAddresses.length);
        for (int i = 0; i < pReciepientAddresses.length; i++) {
            log.info("ADDRESS ARRAY LENGTH In Send Mail: - " + pReciepientAddresses[i]);
            addressTo[i] = new InternetAddress(pReciepientAddresses[i]);
        }
        // log.info("INTERNET ADRESS ARRAY LENGTH : - " + addressTo1.length);
        msg.setRecipients(RecipientType.TO, addressTo);

        // msg.addRecipients(Message.RecipientType.TO, addressTo);
        // Setting the Subject and Content Type
        msg.setSubject(pNotificatonObj.get_Subject());
        msg.setContent(pNotificatonObj.get_HtmlString().toString().toString(),
                "text/html");
        Transport.send(msg);
        log.info("Send Mail Leave");
    }

I have written following things in my root.xml file of tomcatserver directory

<Resource
                     name="mail/MailSession"
                     auth="Container"
                     type="javax.mail.Session"
                     mail.imap.host="localhost"
                     mail.pop.host="localhost"
                     mail.store.protocol="imap"
                     mail.transport.protocol="smtp"
                     mail.smtp.host="smtp.gmail.com"
                     mail.smtp.port="465"
                     mail.smtp.auth="true"
                     mail.smtp.starttls.enable="true"
                     mail.smtp.user="[email protected]" //MyEmailId
                     password="*******" //My password
                     mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
   />

But its giving me following error ...can anyone please help me out..where am doing mistake

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
Selfassured answered 8/1, 2013 at 6:19 Comment(0)
D
3

None of those properties you're setting in your root.xml file are being used by your application.

You need to change your application to either look up the JavaMail Session using JNDI instead of creating it yourself using Session.netInstance, or you need to change your application to set all those properties on the Properties object that you use to create the new Session object.

Don't forget to read the JavaMail FAQ for common mistakes and how to connect to Gmail. (Hint: you don't need any of the socketFactory properties.)

Danie answered 8/1, 2013 at 7:31 Comment(2)
i had done the way u said...i had put following code in my portal-ext-porperties file mail.session.jndi.name=mail/MailSession but still same error i gotSelfassured
Sorry, I don't understand what you did. What "following code" did you put where? Maybe you could post your updated code and configuration that you tried?Danie

© 2022 - 2024 — McMap. All rights reserved.