0
我無法使用以下代碼發送電子郵件。我的圖書館裏有所有需要的罐子。我有一個連接到mysql工作臺的數據庫,我從表單action =「EmailListServlet」和數據庫中的用戶詳細信息中選擇數據。儘管我能夠在netbeans的輸出窗口中接收輸出,但我收到了catch塊中的異常消息。無法從Java Web應用程序發送電子郵件
EmailListServlet
@WebServlet(name = "EmailListServlet", urlPatterns = {"/EmailListServlet"})
public class EmailListServlet extends HttpServlet {
@EJB
private EmailSenderBean sendMail;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
HttpSession session = request.getSession(true);
session.setMaxInactiveInterval(-1);
String url ="";
EntityManager em = DBUtil.getEmFactory().createEntityManager();
User user= (User) session.getAttribute("theUser");
String name = request.getParameter("Name");
System.out.println("Name is"+name);
String fromEmail = request.getParameter("User_Id");
System.out.println("Sender Email is"+fromEmail);
String toEmail = request.getParameter("Friend_Id");
System.out.println("To Email is"+toEmail);
String message = request.getParameter("Message");
System.out.println("Message is"+message);
fromEmail = "'"+fromEmail+"'";
String userPwd = user.getPassword();
System.out.println("Password is" +userPwd);
try{
sendMail.SendEmail(name, fromEmail, userPwd, toEmail, message);
url="/confirmr.jsp";
}
catch(Exception e){
System.out.println("This is an Exception");
}
em.close();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
EmailSenderBean
@Stateless
public class EmailSenderBean {
public void SendEmail(String name, String fromEmail, String userPwd, String toEmail, String message){
Properties props = System.getProperties();
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketDactory.class", "javax.net.ssl.SSLSocketFactory");
//props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Message mailMessage = new MimeMessage(session);
try {
mailMessage.setFrom(new InternetAddress(fromEmail));
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
mailMessage.setContent(message, "text/html");
Transport transport = session.getTransport("smtp");
transport.connect("localhost", fromEmail, userPwd);
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
}
catch (MessagingException ex) {
Logger.getLogger(EmailSenderBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
[EmailListServlet] in context with path [/Ojas_App] threw exception
java.lang.NullPointerException
at org.uncc.nbad.servlet.EmailListServlet.doPost(EmailListServlet.java:83)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2503)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2492)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745).
這指向sendMail.SendEmail(姓名,fromEmail,userPwd,toEmail,消息);
爲了更方便的應答者,或其他有類似問題,請[編輯]添加特定問題陳述 - 「不起作用」可以假設,但*如何*它不起作用?什麼錯誤信息或不正確的行爲是特徵? –
給我們一個堆棧跟蹤? –
也System.out.println(「這是一個例外」); - 這太可怕了 –