我正在用Java編寫Web應用程序。在這個webapp中,我會發送電子郵件給用戶。當我執行應用程序時,我收到一條錯誤消息。我在另一個Java程序中使用相同的源代碼。在那個程序中我沒有任何錯誤。在Java Web應用程序中發送電子郵件
的源代碼:
public class MailUtil {
public static Session getGMailSession(String user, String pass){
final Properties props = new Properties();
//Eigenschaften zum Sende einer Mail mit GMail
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
return Session.getInstance(props, new javax.mail.Authenticator(){
@Override protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication (props.getProperty("mail.pop3.user"),props.getProperty("mail.pop3.password"));
}//endof Methode getPasswordAuthentication
});//endOf getInstance
}//endOf Methode getGMailSession
//Die Methode postMail versendet eine Mail
public static void postMail(String recipient, String subject, String message){
Session session = MailUtil.getGMailSession("user", "password");
try{
Message msg = new MimeMessage(session);
msg.setSubject(subject);
msg.setContent(message, "text/plain");
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
Transport.send(msg);
}
catch(MessagingException e){
System.out.println("Error in this Method");
System.out.println("Cause: " +e.getMessage());
e.printStackTrace();
}
}
}
這裏是錯誤消息:
10:27:50,014 INFO [stdout] (default task-16) **Error in this Method**
10:27:50,014 INFO [stdout] (default task-16) Cause: failed to connect, no user name specified?
10:27:50,015 ERROR [stderr] (default task-16) javax.mail.AuthenticationFailedException: failed to connect, no user name specified?
10:27:50,016 ERROR [stderr] (default task-16) at javax.mail.Service.connect(Service.java:376)
10:27:50,016 ERROR [stderr] (default task-16) at javax.mail.Service.connect(Service.java:226)
10:27:50,016 ERROR [stderr] (default task-16) at javax.mail.Service.connect(Service.java:175)
10:27:50,017 ERROR [stderr] (default task-16) at javax.mail.Transport.send0(Transport.java:253)
10:27:50,017 ERROR [stderr] (default task-16) at javax.mail.Transport.send(Transport.java:124)
10:27:50,017 ERROR [stderr] (default task-16) at de.hskl.lernanalyse.utilities.MailUtil.postMail(MailUtil.java:50)
10:27:50,018 ERROR [stderr] (default task-16) at de.hskl.lernanalyse.controller.TokenGenController.doGeneriereLink(TokenGenController.java:77)
10:27:50,018 ERROR [stderr] (default task-16) at de.hskl.lernanalyse.controller.TokenGenController.doSendeEmail(TokenGenController.java:108)
10:27:50,018 ERROR [stderr] (default task-16) at de.hskl.lernanalyse.controller.TokenGenController$Proxy$_$$_WeldClientProxy.doSendeEmail(Unknown Source)
10:27:50,019 ERROR [stderr] (default task-16) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
10:27:50,025 ERROR [stderr] (default task-16) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
10:27:50,025 ERROR [stderr] (default task-16) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
10:27:50,026 ERROR [stderr] (default task-16) at java.lang.reflect.Method.invoke(Method.java:606)
10:27:50,026 ERROR [stderr] (default task-16) at javax.el.ELUtil.invokeMethod(ELUtil.java:308)
10:27:50,026 ERROR [stderr] (default task-16) at javax.el.BeanELResolver.invoke(BeanELResolver.java:537)
10:27:50,027 ERROR [stderr] (default task-16) at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
10:27:50,027 ERROR [stderr] (default task-16) at com.sun.el.parser.AstValue.invoke(AstValue.java:269)
10:27:50,027 ERROR [stderr] (default task-16) at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
10:27:50,027 ERROR [stderr] (default task-16) at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
10:27:50,028 ERROR [stderr] (default task-16) at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
10:27:50,029 ERROR [stderr] (default task-16) at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
10:27:50,029 ERROR [stderr] (default task-16) at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
10:27:50,030 ERROR [stderr] (default task-16) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
10:27:50,031 ERROR [stderr] (default task-16) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
10:27:50,031 ERROR [stderr] (default task-16) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
10:27:50,032 ERROR [stderr] (default task-16) at javax.faces.component.UICommand.broadcast(UICommand.java:315)
10:27:50,032 ERROR [stderr] (default task-16) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
10:27:50,033 ERROR [stderr] (default task-16) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
10:27:50,033 ERROR [stderr] (default task-16) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
10:27:50,034 ERROR [stderr] (default task-16) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
10:27:50,035 ERROR [stderr] (default task-16) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
10:27:50,035 ERROR [stderr] (default task-16) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
10:27:50,035 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
10:27:50,036 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
10:27:50,037 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
10:27:50,037 ERROR [stderr] (default task-16) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
10:27:50,038 ERROR [stderr] (default task-16) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
10:27:50,039 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
10:27:50,039 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
10:27:50,040 ERROR [stderr] (default task-16) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
10:27:50,041 ERROR [stderr] (default task-16) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
10:27:50,041 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
10:27:50,042 ERROR [stderr] (default task-16) at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
10:27:50,042 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
10:27:50,043 ERROR [stderr] (default task-16) at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
10:27:50,043 ERROR [stderr] (default task-16) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
10:27:50,045 ERROR [stderr] (default task-16) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
10:27:50,045 ERROR [stderr] (default task-16) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
10:27:50,053 ERROR [stderr] (default task-16) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
10:27:50,053 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
10:27:50,054 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
10:27:50,054 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
10:27:50,055 ERROR [stderr] (default task-16) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
10:27:50,055 ERROR [stderr] (default task-16) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:177)
10:27:50,056 ERROR [stderr] (default task-16) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727)
10:27:50,056 ERROR [stderr] (default task-16) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
10:27:50,056 ERROR [stderr] (default task-16) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
10:27:50,057 ERROR [stderr] (default task-16) at java.lang.Thread.run(Thread.java:745)
我用你的答案編輯了程序並更新了屬性。但該錯誤消息仍然出現 –