回复邮件时无法连接到 SMTP 主机:localhost,端口:25
我有一个代码连接到邮件服务器并搜索特定主题标签并将响应发送回(回复)该特定邮件。
然而,在回复消息时,我收到以下错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定是什么原因导致您的错误。
我只是在这里大胆猜测:不要使用静态 Transport.send() 方法。
获取一个传输对象并调用它的 sendMessage) 方法:
尝试看看这是否有帮助。
干杯。
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:
Try and see if this helps.
cheers.