我做了一个 CLASSPATH 环境变量绑定 jaf 和 javamail :
用户变量名(windows):CLASSPATH
此变量的值:设置 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。
这是我的小服务程序:
@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 java mail拿了 Google 实现示例?