3 Replies Latest reply: Mar 20, 2013 1:43 AM by Bill Shannon-Oracle RSS

    Not able to send mail through java

    Wecenter Guru
      Hi,
      I am sending an email through java but it gives me exception that Authentication failed exception but I have provided correct credentials
      I have given stack trace as below
      19 Mar 2013 16:32:58,579 614378 [[ACTIVE] ExecuteThread: '4' for queue: 'weblogic.kernel.Default (self-tuning)'] ERROR alfaaesar.portal.pages.common.bo.AfCmnCommonBO  – AfCmnCommonBo SendMail Exception:javax.mail.AuthenticationFailedException
           at javax.mail.Service.connect(Service.java:319)
           at alfaaesar.portal.pages.common.bo.AfCmnCommonBO.sendMail(AfCmnCommonBO.java:939)
           at alfaaesar.portal.pages.literature.action.AfLitetatureRequestAndViewAction.InsertLiteratureReqDtlsInAlfaLitHeaderTbl(AfLitetatureRequestAndViewAction.java:619)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:597)
           at com.sun.el.parser.AstValue.invoke(Unknown Source)
           at com.sun.el.MethodExpressionImpl.invoke(Unknown Source)
           at org.apache.myfaces.trinidad.component.MethodExpressionMethodBinding.invoke(MethodExpressionMethodBinding.java:46)
           at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
           at org.apache.myfaces.trinidad.component.UIXCommand.broadcast(UIXCommand.java:190)
           at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent$1.run(ContextSwitchingComponent.java:92)
           at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent._processPhase(ContextSwitchingComponent.java:361)
           at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent.broadcast(ContextSwitchingComponent.java:96)
           at oracle.adf.view.rich.component.fragment.UIXInclude.broadcast(UIXInclude.java:102)
           at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent$1.run(ContextSwitchingComponent.java:92)
           at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent._processPhase(ContextSwitchingComponent.java:361)
           at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent.broadcast(ContextSwitchingComponent.java:96)
           at oracle.adf.view.rich.component.fragment.UIXInclude.broadcast(UIXInclude.java:96)
           at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
           at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
           at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._invokeApplication(LifecycleImpl.java:889)
           at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executePhase(LifecycleImpl.java:379)
           at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:194)
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
           at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
           at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
           at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
           at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at alfaaesar.portal.pages.filter.AnonymousSessionFilter.doFilter(AnonymousSessionFilter.java:47)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at oracle.portlet.client.adapter.adf.ADFPortletFilter.doFilter(ADFPortletFilter.java:32)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at oracle.webcenter.framework.events.dispatcher.EventDispatcherFilter.doFilter(EventDispatcherFilter.java:44)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at oracle.adf.model.servlet.ADFBindingFilter.doFilter(ADFBindingFilter.java:205)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at oracle.adfinternal.view.faces.webapp.rich.RegistrationFilter.doFilter(RegistrationFilter.java:106)
           at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:446)
           at oracle.adfinternal.view.faces.activedata.AdsFilter.doFilter(AdsFilter.java:60)
           at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:446)
           at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._doFilterImpl(TrinidadFilterImpl.java:271)
           at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl.doFilter(TrinidadFilterImpl.java:177)
           at org.apache.myfaces.trinidad.webapp.TrinidadFilter.doFilter(TrinidadFilter.java:92)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at oracle.wcps.client.PersonalizationFilter.doFilter(PersonalizationFilter.java:75)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at oracle.webcenter.content.integration.servlets.ContentServletFilter.doFilter(ContentServletFilter.java:168)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at oracle.webcenter.lifecycle.filter.LifecycleLockFilter.doFilter(LifecycleLockFilter.java:151)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at oracle.adf.library.webapp.LibraryFilter.doFilter(LibraryFilter.java:179)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at oracle.security.jps.ee.http.JpsAbsFilter$1.run(JpsAbsFilter.java:119)
           at java.security.AccessController.doPrivileged(Native Method)
           at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:315)
           at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformUtil.java:442)
           at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.java:103)
           at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:171)
           at oracle.security.jps.ee.http.JpsFilter.doFilter(JpsFilter.java:71)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at oracle.dms.servlet.DMSServletFilter.doFilter(DMSServletFilter.java:139)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
           at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
           at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
           at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
           at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
           at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
           at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
           at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
           at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
           at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
           at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
      Also given code below in java to send an email .
                      public  Boolean sendMail(String host,String port,String debugFlag,String authFlag, String userId,String password,String fromEmailAddress,String toEmailAddress,String subject,String eMailcontent ) throws MessagingException {
              Boolean sendMailFlag = false;
      
              Properties props = new Properties();
              props.put("mail.smtp.host", host);
              props.put("mail.smtp.port", port);
              props.put("mail.debug", debugFlag); //Set default to false
              props.put("mail.smtp.auth", authFlag); //Set default to true
              props.put("mail.transport.protocol", "smtp");
              props.put("mail.smtp.sendpartial","true"); 
             // props.put("mail.smtp.starttls.enable", "true");
              logger.info("Sending Email.......");
              //System.out.println("Sending Email.......");
              
              try {
                  // Instantiatee a message
      
                  final String mailUserId = userId;
                  final String mailPassword = password;
                        
                 Session mailSession = Session.getDefaultInstance(props, new Authenticator() {
                  protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(mailUserId, mailPassword);
                  }} );
                  
                 MimeMessage msg = new MimeMessage(mailSession);
                   
                  msg.setFrom(new InternetAddress(fromEmailAddress));
               /****************************************************************************************/    
           
                 String[] to = toEmailAddress.split(",");
                 InternetAddress[] addressTo = new InternetAddress[to.length];
                       for (int i = 0; i < to.length; i++)
                       {
                           addressTo[i] = new InternetAddress(to);
      }

      msg.setRecipients(Message.RecipientType.TO, addressTo);

      /******************************************************************************************/

      /******************************************************************************************/
      if(JSFUtils.getFromSession("ccEmailAddress")!=null){

      String ccEmailAddress =(String)JSFUtils.getFromSession("ccEmailAddress");
      JSFUtils.removeFromSession("ccEmailAddress");
      String[] cc = ccEmailAddress.split(",");
      InternetAddress[] addressCC = new InternetAddress[cc.length];
      for (int i = 0; i < cc.length; i++)
      {
      addressCC[i] = new InternetAddress(cc[i]);
      }
      msg.setRecipients(Message.RecipientType.CC, addressCC);

      }
      /******************************************************************************************/
      msg.setSubject(subject,"utf-8");
      msg.setSentDate(new Date());
      //msg.setText(eMailcontent);
      msg.setContent(eMailcontent, "text/html; charset=\"utf-8\"");


      if (toEmailAddress.indexOf(',') > 0)
      msg.setRecipients(Message.RecipientType.TO,
      InternetAddress.parse(toEmailAddress));
      else
      msg.setRecipient(Message.RecipientType.TO,
      new InternetAddress(toEmailAddress));

      Transport transport = mailSession.getTransport();
      transport.connect(host,Integer.parseInt(port),userId.trim(),password.trim());
      transport.send(msg);
      transport.close();
      //Send the message
      // Transport.send(msg);
      sendMailFlag = true;
      // System.out.println("mail sent");
      } catch (MessagingException mex) {
      // Prints all nested (chained) exceptions as well
      //logger.info(mex.getMessage());
      logger.error("AfCmnCommonBo SendMail Exception:"+JSFUtils.getStackTrace(mex));
      //mex.printStackTrace();
      sendMailFlag = false;
      }
      return sendMailFlag;
      }
      Edited by: Wecenter Guru on Mar 19, 2013 5:02 PM