这个问题让我疯了。我有以下代码:
<html>
<body>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%
String host = "exchsrv2";
String to = "alan@domain.com";
String from = "apeince@domain.com";
String subject = "test";
String messageText = "body test";
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", "25");
Session mailSession = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");
%>
</body>
</html>
好的,问题是,我收到以下错误:
javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: exchsrv2, port: 25;
nested exception is:
java.net.SocketException: Permission denied: connect
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
org.apache.jsp.alan_jsp._jspService(alan_jsp.java:113)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
我知道Exchange服务器在那里。我可以telnet到它就好了。我的Exchange服务器设置为不需要身份验证。我有一个在C#/ .NET中工作正常的程序,它工作正常,所以我知道问题不在Exchange服务器中。我在这里做错了什么?
答案 0 :(得分:9)
我会说尝试连接时存在身份验证问题。除非您的Exchange服务器不需要用户名和密码,否则您不提供任何用户名或密码。
更新:如果使用JDK 7,请参阅以下帖子,它解决了这个问题:
Defect - JDK7 Permission Denied with Sockets when using VPN
“更多的挖掘并且似乎VPN客户端已禁用IPv6,这导致JDK7出现问题。如果我使用以下标志-Djava.net.preferIPv4Stack = true我不再看到错误。这是预期的解决方法还是这个问题?“
public class MailTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws MessagingException {
String host = "smtp.gmail.com";
String to = "myEmail@gmail.com";
String from = "myEmail@gmail.com";
String subject = "test";
String messageText = "body test";
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", "25");
// If using authentication, otherwise comment out
props.put("mail.smtp.auth", "true");
// Gmail requires TLS, your server may not
props.put("mail.smtp.starttls.enable", "true");
Session mailSession = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport transport = mailSession.getTransport("smtp");
//connect with authentication
//transport.connect(host,"myUsername" , "myPassword");
//connect without authentication
transport.connect();
transport.sendMessage(msg, address);
transport.close();
System.out.println("Mail was sent to " + to);
System.out.println(" from " + from);
System.out.println(" using host " + host + ".");
}
}
答案 1 :(得分:1)
如果您不想将首选IP堆栈更改为IPv4,则另一种解决方案可能是使用主机的IPv6地址而不是DHCP名称,例如:
String host = "fe80::b84d:1ed2:5329:de87%10";
错误的原因确实是从JDK 7开始在Windows上默认使用IPv6。当您尝试连接到IPv4地址时,它将使用IPv4映射的IPv6地址(请参阅this blog from the Oracle website )。但是,如果您的Exchange服务器支持IPv6,通常这应该没问题。但是,在切换到JDK 7之后,我通过MS Exchange 2010 Server从Java代码发送邮件时遇到了类似的问题。奇怪的是,根据this link on MS Technet,它支持IPv6。由于我不想将完整应用程序的IP堆栈默认为IPv4,因此在启动应用程序时通过设置标志-Djava.net.preferIPv4Stack = true来接受解决方案不是一种选择。因此,在我的情况下,将主机名更改为有效的IPv6是更好的解决方案。我不能100%清楚为什么这样做而不是DHCP名称。
答案 2 :(得分:-1)
<%
String host = "smtp.gmail.com";
String from="send-from@gmail.com";//Your E-mail-Id
String pass="xxxxxx"; //your e-mail account password
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to = "send-to@gmail.com"; //recipient E-mail-Id
String from = "send-from@gmail.com"; // Your E-mail Id
String subject ="test mail";
String messageText = "hello abc";
boolean sessionDebug = true;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");//port number 465 for Secure (SSL) and we can also use port no 587 for Secure (TLS)
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(messageText, "text/html");
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);
try {
transport.sendMessage(msg, msg.getAllRecipients());
out.println("Send Success");
boolean WasEmailSent = true; // assume it was sent
}
catch (Exception err) {
boolean WasEmailSent = false;
}
transport.close();
%>