无法连接到SMTP主机:localhost,端口:25? [英] Could not connect to SMTP host: localhost, port: 25?

查看:729
本文介绍了无法连接到SMTP主机:localhost,端口:25?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

am使用liferay 6并创建了一个自定义类..我想创建邮件通知功能...我在类中编写了以下代码

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");
    }

我在tomcatserver目录的root.xml文件中写了以下内容

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="My@gmail.com" //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

推荐答案

您在root.xml文件中设置的所有属性均未被您的应用程序使用.

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

您需要更改您的应用程序以使用JNDI查找JavaMail会话,而不是使用Session.netInstance自己创建它,或者您需要更改您的应用程序以在用于创建Java的Properties对象上设置所有这些属性.新的会话对象.

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.

不要忘记阅读常见错误的JavaMail常见问题解答. 如何连接到Gmail . (提示:您不需要任何socketFactory属性.)

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.)

这篇关于无法连接到SMTP主机:localhost,端口:25?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆