這個問題使我瘋狂。我有以下代碼:使用JSP發送電子郵件
<html>
<body>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%
String host = "exchsrv2";
String to = "[email protected]";
String from = "[email protected]";
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服務器中。我在這裏做錯了什麼?
如果您認爲這不是Exchange服務器問題,也許嘗試使用例如gmail帳戶發送郵件 - http://www.mkyong.com/java/javamail-api-sending-email-via- gmail-smtp-example/ – mmatloka
'Permission denied' _usually_意味着您嘗試連接的端口被防火牆擋住。 –