In the DoPost() i am calling the function sendTextMail(strFromUser, strToUsers, strCcUsers, strSubject, strMessageBody, strobjAttachments) in the Try catch block.
Now the code compiles perfectly.
My doubt is how i will write RequestDispatcher method in Dopost()
if it is success then it will go to
RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL ("/help/SuccessApplicationStatus.jsp"));
rd.forward(request,response);
else if it is error then
RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL ("/error/errorApplicationStatus.jsp"));
rd.forward(request,response);
Please look at my code and guide me for the needful.
Thanks in Advance.
Regards
Sumanta Panda
Expand|Select|Wrap|Line Numbers
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- String strFromUser=null;
- String strToUsers=null;
- String strCcUsers=null;
- String strApplnno=null;
- String strSubject=null;
- String strMessageBody=null;
- String []strobjAttachments=null;
- String errorMessage="";
- HashMap result=null;
- strFromUser=request.getParameter("txtMail");
- strToUsers="care.cwaindia@citi.com";
- strCcUsers="care.cwaindia@citi.com";
- strApplnno=request.getParameter("txtApplno");
- strSubject="Application Form Status";
- //strMessageBody=strApplnno.concat("-Application Form Status");
- strMessageBody= "Application Form Status for Application Form no.-".concat(strApplnno);
- try
- {
- sendTextMail(strFromUser, strToUsers, strCcUsers, strSubject, strMessageBody, strobjAttachments);
- //RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL("/help/SuccesspageApplicationStatus.jsp"));
- // rd.forward(request,response);
- }
- catch(MailingException mEx)// Trap MailingException
- {
- try {
- throw mEx;
- } catch (MailingException e) {
- //RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL("/error/ErrorpageApplicationStatus.jsp"));
- //rd.forward(request,response);
- System.out.println("Data2");
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- public void sendTextMail(String strFromUser, String strToUsers, String strCcUsers, String strSubject, String strMessageBody, String[] objAttachments) throws MailingException
- {
- try
- {
- // Get a Properties object
- java.util.Properties objProps = System.getProperties();
- objProps.put("mailhub-ap.ap.ssmb.com",strHost);
- // Get a Session object
- Session objSession = Session.getDefaultInstance(objProps, null);
- // Construct a new message
- MimeMessage objMessage=new MimeMessage(objSession);
- // Set up the parameters for sending the message
- if ( strFromUser != null )
- objMessage.setFrom(new InternetAddress(strFromUser));
- objMessage.setSubject(strSubject);
- if (strToUsers == null)
- throw new MailingException("Email id of recipients not defined");
- else
- {
- if (strToUsers != null)
- objMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse(strToUsers));
- }
- if(objAttachments==null)
- {
- objMessage.setText(strMessageBody);
- }
- else
- {
- Multipart mpMainPart=new MimeMultipart();
- //Creating multi-part message
- BodyPart bpMessageBodyPart=new MimeBodyPart();//this is the Mail Message BodyPart
- bpMessageBodyPart.setText(strMessageBody+"\n");
- mpMainPart.addBodyPart(bpMessageBodyPart);
- for(int intCount=0;intCount<objAttachments.length;intCount++)
- {
- BodyPart bpFileBodyPart=new MimeBodyPart();//this is the Attachment BodyPart
- DataSource dsSource=new FileDataSource(new File(objAttachments[intCount]));
- bpFileBodyPart.setDataHandler(new DataHandler(dsSource));
- bpFileBodyPart.setFileName(objAttachments[intCount]);
- mpMainPart.addBodyPart(bpFileBodyPart);
- }
- objMessage.setContent(mpMainPart);
- }
- // Send the message
- Transport.send(objMessage);
- }
- catch(MessagingException ex)// Trap MessaginException
- { throw new MailingException(ex); }
- catch(MailingException mEx)// Trap MailingException
- { throw mEx; }
- }
- }