使用 JSP 发送电子邮件

2024-01-04

这个问题快把我逼疯了。我有以下代码:

<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] /cdn-cgi/l/email-protection";
String from = "[email protected] /cdn-cgi/l/email-protection";
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 服务器在那里。我可以很好地远程登录到它。我的 Exchange 服务器设置为不需要身份验证。我有一个在 C#/.NET 中运行良好的程序,而且它运行良好,所以我知道问题不在 Exchange 服务器中。我在这里做错了什么?


我会立即说尝试连接时存在身份验证问题。您无需提供任何用户名或密码,除非您的交换服务器不需要用户名和密码。


更新:如果使用 JDK 7,请参阅以下帖子,它解决了此问题:

缺陷 - 使用 VPN 时 JDK7 权限被套接字拒绝 http://www.java.net/node/703177

“进一步挖掘,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 = "[email protected] /cdn-cgi/l/email-protection";
        String from = "[email protected] /cdn-cgi/l/email-protection";
        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 + ".");

    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 JSP 发送电子邮件 的相关文章

随机推荐