回复邮件时无法连接到 SMTP 主机:localhost,端口:25

发布于 2024-11-02 05:36:30 字数 3691 浏览 0 评论 0原文

我有一个代码连接到邮件服务器并搜索特定主题标签并将响应发送回(回复)该特定邮件。

然而,在回复消息时,我收到以下错误:

Could not connect to SMTP host: localhost, port: 25

我能够直接发送电子邮件,只有当我使用 REPLY/FORWARD 方法时才会出现问题。我正在使用 javamail。

import java.util.*;
import javax.mail.*; 
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;



public class Mail { 

    public static void main(String args[]) throws Exception { 

        Properties properties = System.getProperties();
        Session session = Session.getDefaultInstance(properties);
        Store store = session.getStore("pop3");
        store.connect("smtp.gotouchpoint.com", "username", "password");
        Folder folder = store.getFolder("inbox");

        //Connect to the current host using the specified username and password.



        try {

            //Create a Folder object corresponding to the given name.
            //   Folder folder = store.getFolder("INBOX");
            // Open the Folder.

            folder.open(Folder.READ_WRITE);   
            SearchTerm st1 =new SubjectTerm("**NEW-TICKET**");
            Message[] message = folder.search(st1);


            //Message[] message = folder.getMessages();
            // Create a reply message




            for (int i = 0; i < message.length; i++) {


                System.out.println("------------ Message " + (i + 1) + " ------------");
                System.out.println("SentDate : " + message[i].getSentDate());
                System.out.println("From : " + message[i].getFrom()[0]);
                System.out.println("Subject : " + message[i].getSubject());

                if(message[i].getSubject().equals("**NEW-TICKET**")) {
                    System.out.println("new-ticket");

                    Message forward = new MimeMessage(session);

                    // Fill in header
                    forward.setSubject("Fwd: " + message[i].getSubject());
                    forward.setFrom(new InternetAddress(message[i].getFrom()[0].toString()));
                    forward.addRecipient(Message.RecipientType.TO,
                            new InternetAddress("[email protected]"));

                    // Create your new message part
                    BodyPart messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setText("Oiginal message:\n\n");

                    // Create a multi-part to combine the parts
                    Multipart multipart = new MimeMultipart();
                    multipart.addBodyPart(messageBodyPart);

                    // Create and fill part for the forwarded content
                    messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setDataHandler(message[i].getDataHandler());

                    // Add part to multi part
                    multipart.addBodyPart(messageBodyPart);

                    // Associate multi-part with message
                    forward.setContent(multipart);

                    System.out.println("msg forward ...."  +forward.getSubject());
                    // Send message
                    Transport.send(forward);



                }


            }

            System.out.println();       
            folder.close(true);
            store.close();

        }catch(Exception e) {
            e.printStackTrace();
        }    

    }



}

I have a code which connects to a mailserver and searches for a particular subject tag and sends response back (REPLY) to that particular mail.

However while giving a reply message I get the following error :

Could not connect to SMTP host: localhost, port: 25

I am able to send email directly the problem occurs only if i use REPLY/FORWARD method.I am using javamail.

import java.util.*;
import javax.mail.*; 
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;



public class Mail { 

    public static void main(String args[]) throws Exception { 

        Properties properties = System.getProperties();
        Session session = Session.getDefaultInstance(properties);
        Store store = session.getStore("pop3");
        store.connect("smtp.gotouchpoint.com", "username", "password");
        Folder folder = store.getFolder("inbox");

        //Connect to the current host using the specified username and password.



        try {

            //Create a Folder object corresponding to the given name.
            //   Folder folder = store.getFolder("INBOX");
            // Open the Folder.

            folder.open(Folder.READ_WRITE);   
            SearchTerm st1 =new SubjectTerm("**NEW-TICKET**");
            Message[] message = folder.search(st1);


            //Message[] message = folder.getMessages();
            // Create a reply message




            for (int i = 0; i < message.length; i++) {


                System.out.println("------------ Message " + (i + 1) + " ------------");
                System.out.println("SentDate : " + message[i].getSentDate());
                System.out.println("From : " + message[i].getFrom()[0]);
                System.out.println("Subject : " + message[i].getSubject());

                if(message[i].getSubject().equals("**NEW-TICKET**")) {
                    System.out.println("new-ticket");

                    Message forward = new MimeMessage(session);

                    // Fill in header
                    forward.setSubject("Fwd: " + message[i].getSubject());
                    forward.setFrom(new InternetAddress(message[i].getFrom()[0].toString()));
                    forward.addRecipient(Message.RecipientType.TO,
                            new InternetAddress("[email protected]"));

                    // Create your new message part
                    BodyPart messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setText("Oiginal message:\n\n");

                    // Create a multi-part to combine the parts
                    Multipart multipart = new MimeMultipart();
                    multipart.addBodyPart(messageBodyPart);

                    // Create and fill part for the forwarded content
                    messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setDataHandler(message[i].getDataHandler());

                    // Add part to multi part
                    multipart.addBodyPart(messageBodyPart);

                    // Associate multi-part with message
                    forward.setContent(multipart);

                    System.out.println("msg forward ...."  +forward.getSubject());
                    // Send message
                    Transport.send(forward);



                }


            }

            System.out.println();       
            folder.close(true);
            store.close();

        }catch(Exception e) {
            e.printStackTrace();
        }    

    }



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

数理化全能战士 2024-11-09 05:36:30

不确定是什么原因导致您的错误。
我只是在这里大胆猜测:不要使用静态 Transport.send() 方法。
获取一个传输对象并调用它的 sendMessage) 方法:

    [...]
    Transport transport = session.getTransport("smtp");
    transport.connect(D_HOST, D_PORT, D_USER, D_PASS);
    transport.sendMessage(forward, forward.getAllRecipients());

尝试看看这是否有帮助。

干杯。

Not sure what might be causing your error.
I'm just taking a wild guess here: Don't use the static Transport.send() method.
Get a transport object and invoke it's sendMessage) method:

    [...]
    Transport transport = session.getTransport("smtp");
    transport.connect(D_HOST, D_PORT, D_USER, D_PASS);
    transport.sendMessage(forward, forward.getAllRecipients());

Try and see if this helps.

cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文