gpt4 book ai didi

Java EE api邮件发送邮件时出错

转载 作者:行者123 更新时间:2023-12-01 14:53:59 25 4
gpt4 key购买 nike

我创建了一个CLASSPATH环境变量绑定(bind)jaf和javamail:

用户变量的名称(Windows):CLASSPATH

此变量的值:set CLASSPATH=%CLASSPATH%;C:\Java EE\Java SMTP\javamail-1.4.5\mail.jar;C:\Java EE\Java SMTP\jaf-1.1.1\activation .jar

我还将这 2 个 .jar 文件放入我的 WebContent 目录(放入我的 java Web 应用程序中),并将其添加到我的构建路径中。

我正在使用 Tomcat Apache Server 7.0.33。

这是我的 servlet:

    @WebServlet (urlPatterns="/register")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private void sendMailRegistration(String email, int code)
throws ServletException, IOException{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

String msgBody = "Here is the code you have to put for confirmation: "+Integer.toString(code);

try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("dd@supinfo.com", "Admin SupLink"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(email, "User"));
msg.setSubject("[SupLink] Activation");
msg.setText(msgBody);
Transport.send(msg);

} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String emailRegister = request.getParameter("emailRegister");
int code = 1223;
sendMailRegistration(emailRegister, code);

}
}

这是我的错误堆栈跟踪:

    javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.supinfo.miniprojet.servlets.RegisterServlet.sendMailRegistration(RegisterServlet.java:43)
at com.supinfo.miniprojet.servlets.RegisterServlet.doPost(RegisterServlet.java:81)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
... 26 more

有人知道我的源邮件地址 dd@supinfo.com 或我的 CLASSPATH 是否有问题,因为我只是在这里使用了 Google 实现示例 Google java mail

最佳答案

您需要确定要使用的邮件服务器(因为您显然没有在本地主机上运行邮件服务器)。一旦确定,您需要配置 Java Mail 才能使用它。您可以引用下面的代码作为使用 Google Mail 服务器的示例:

public static void main(String[] args) {

final String username = "username@gmail.com";
final String password = "password";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}

注意:这不是我的代码,您可以引用完整的示例 here

关于Java EE api邮件发送邮件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14485291/

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