
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
                	


public class Mail_Process extends HttpServlet
{

   public String StrHtml = "";
   PrintWriter PWriter;
   
   public String PubHiddenMailTo,PubHiddenMailToAll,PubStrMailCc,PubStrMailBcc,PubStrMailSubject,PubStrFirstFileName,PubStrSecondFileName,PubStrMailText;
   
   protected void service(HttpServletRequest HSReq,HttpServletResponse HSRes) throws ServletException,IOException
   {
   
   
                PWriter=new PrintWriter(HSRes.getOutputStream());
                HSRes.setContentType("text/Html");
                	
                        ServletOutputStream Sout = HSRes.getOutputStream();
                           
                        StrHtml += "<Html> \n";
                        StrHtml += "<Head> \n";
                        StrHtml += "<Title>Contact Directory - Email</Title> \n";
                         
                        StrHtml += "</Head> \n";

                        StrHtml += "<Body><Form Name = FrmProcessMail > \n";
                        StrHtml += "<Center><B><Font Color=Blue><H2><U>Contact Directory - MAIL</U></H2></Font></B></Center> \n";
                        StrHtml += "<Br> \n";
                	
                        StrHtml += "<Center><Font Color = Red Face = Arial Size = 2><B>Sending Mails......</B></Font><Center> \n";
                	
                        SendMessage(HSReq,HSRes,Sout);
                	
                        StrHtml += "<Center><Font Color = Blue Face = Arial Size = 2><B>Mails Sent Successfully......</B></Font><Center> \n";

                        //// Ended Processing the Mail.......           	
        	
        	
                        StrHtml += "</Form> \n";
                        StrHtml += "<Center> \n";
                        StrHtml += "</Body> \n";
                        StrHtml += "</Html> \n";
                	
                        PWriter.println(StrHtml);
                        PWriter.flush();
                        StrHtml = "";
        	
   
   }
   
   
   private void SendMessage(HttpServletRequest request , HttpServletResponse response , ServletOutputStream Soutput) throws IOException
   {
                String StrTo = "vedulas@corporate.ge.com";
                String StrFrom = "karl@servletguru.com" ;
                String StrHost = "3.47.28.20";
        	
            String StrFilename = "C:\Test.txt";
                        	
                String StrMsgText = "This is a test mail";
                String StrSubject = "Hai Subba";
                Session session = null;
                MimeMessage DataMessage = null;
                MimeBodyPart FirstMessageBodyPart = null;
                MimeBodyPart SecondMessageBodyPart = null;
                FileDataSource FileAttach = null;
                Part p = null;
        	
                try
                {
                        // create some properties and get the default Session
                        Properties props = System.getProperties();
                        props.put("mail.smtp.host", StrHost);
                                	
                        session = Session.getDefaultInstance(props, null);
                    session.setDebug(false);
                                  
                    // create a message
                    DataMessage = new MimeMessage(session);
                    DataMessage.setFrom(new InternetAddress(StrFrom));
                    InternetAddress[] address = {new InternetAddress(StrTo)};
                                        	
                    DataMessage.setRecipients(Message.RecipientType.TO, address);
                    DataMessage.setSubject(StrSubject);
                        	
                    // create and fill the first message part
                                            
                        FirstMessageBodyPart = new MimeBodyPart();
                    FirstMessageBodyPart.setText(StrMsgText);
                        	
                        	
                        // create the second message part
                        SecondMessageBodyPart = new MimeBodyPart();
                                        	
                        // attach the file to the message
                                	
                                FileAttach = new FileDataSource(StrFilename);
                        SecondMessageBodyPart.setDataHandler(new DataHandler(FileAttach));
                                SecondMessageBodyPart.setFileName(StrFilename);
                                        	
                        // create the Multipart and its parts to it
                                	
                        Multipart MessgeMultiPart = new MimeMultipart();
                    MessgeMultiPart.addBodyPart(FirstMessageBodyPart);
                    MessgeMultiPart.addBodyPart(SecondMessageBodyPart);
        	
                    p = MessgeMultiPart.getBodyPart(0);
                        int i;
                        String str = p.getContentType();
                                	
                        ContentType ct = new ContentType(str);
                	
                        response.setContentType(ct.getBaseType());
                                        	
                        InputStream instream = p.getInputStream();
                        while((i = instream.read()) != -1)
                        {
                                Soutput.write(i);
                                Soutput.flush();
                        }
                	
                                        	
                    // add the Multipart to the message
                    DataMessage.setContent(MessgeMultiPart);
                        	
                    // set the Date: header
                    DataMessage.setSentDate(new Date());
                                            
                    // send the message
                    Transport.send(DataMessage);
                        Soutput.println("Message Sent Successfully \n");
                        Soutput.close();
                }
                catch (MessagingException mex) 
                {
                    Soutput.println(mex.getMessage());
                        Soutput.println(mex.toString());
                    Exception ex = null;
                    if ((ex = mex.getNextException()) != null) 
                        {
                                ex.printStackTrace();
                                Soutput.close();
                        }
                        Soutput.close();
                }
        	
}
}

