我正在尝试将Javamail应用程序连接到James服务器,但我正在
javax.mail.MessagingException:无法连接到SMTP主机:localhost,port:4555; 嵌套异常是:java.net.SocketException:无效的arguent:connect
这是代码,它为我创造了一个小问题:
import java.security.Security;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class mail {
public static void main(String[] argts)
{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
String mailHost = "your.smtp.server";
String to = "blue@localhost";
String from = "red@localhost";
String subject = "jdk";
String body = "Down to wind";
if ((from != null) && (to != null) && (subject != null) && (body != null)) // we have mail to send
{
try {
//Get system properties
Properties props = System.getProperties();
props.put("mail.smtp.user", "red");
props.put("mail.smtp.host", "localhost");
props.put("mail.debug", "true");
props.put("mail.smtp.port", 4555);
props.put("mail.smtp.socketFactory.port", 4555);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getInstance(props,null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(to) });
message.setSubject(subject);
message.setContent(body, "text/plain");
message.setText(body);
Transport.send(message);
System.out.println("<b>Thank you. Your message to " + to + " was successfully sent.</b>");
} catch (Throwable t) {
System.out.println("Teri maa ki "+t);
}
}
}
}
提前致谢。 :)
答案 0 :(得分:1)
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", 4555);
这两行是否使您的代码连接到您的localhost(您自己的计算机?),端口4555.我认为有问题的SMTP服务器正在其他主机上运行。将主机名放入第一个属性,将正确的端口(通常为25)放入第二个属性,这样就可以了。
如果这没有帮助,请提供有关您的设置的详细信息:运行SMTP服务器的主机,正在监听的端口,是否使用SSL(根据您的代码,是吗?),您在哪里运行客户,......