我有一个连接到邮件服务器并搜索特定主题标签的代码,并将响应发送回(REPLY)到该特定邮件。
然而,在给出回复消息时,我收到以下错误:
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("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();
}
}
}
答案 0 :(得分:0)
不确定可能导致错误的原因。 我只是 在这里采取疯狂猜测 :不要使用静态Transport.send()方法。 获取传输对象并调用它的sendMessage方法:
[...]
Transport transport = session.getTransport("smtp");
transport.connect(D_HOST, D_PORT, D_USER, D_PASS);
transport.sendMessage(forward, forward.getAllRecipients());
尝试看看这是否有帮助。
欢呼声。