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

时间:2022-01-29 23:14:19

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.

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

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.

我能够直接发送电子邮件,只有当我使用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("techsupport@touchpointindia.com"));

                    // 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();
        }    

    }



}

1 个解决方案

#1


0  

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.send()方法。获取传输对象并调用它的sendMessage方法:

    [...]
    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.

#1


0  

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.send()方法。获取传输对象并调用它的sendMessage方法:

    [...]
    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.