gpt4 book ai didi

jsp - 使用 JSP 发送电子邮件

转载 作者:行者123 更新时间:2023-12-01 09:06:03 25 4
gpt4 key购买 nike

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

<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 服务器在那里。我可以远程登录它就好了。我的 Exchange 服务器设置为不需要身份验证。我有一个在 C#/.NET 中运行良好的程序,它运行良好,所以我知道问题不在 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 + ".");

}
}

关于jsp - 使用 JSP 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7477712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com