Thursday, 18 April 2013

Sending Email Using JSP Servlet

Step 1: 
Create JSP file 
















Step 2:
Write a Script Code To call Servlet.


function sendmail()
{
waiting_effect_on();
alert("send mail");
$.ajax({ type: "Post", url: "send_message",
data:{mailId:$("#mailId").val(),subject:$("#subject").val(),message:$("#message").val()},
cache:false,
success : function(data){
waiting_effect_off();
alert(data);

}
});
}



Step 3:
Write given code to Servlet.


               Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session mailSession = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("XXXXX@gmail.com", "*****");
}
});
Message simpleMessage = new MimeMessage(mailSession);


       InternetAddress fromAddress = null;
       InternetAddress toAddress = null;
       try {
           fromAddress = new InternetAddress(Email);
           toAddress = new InternetAddress(mailId);
       } catch (AddressException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }

       try {
           simpleMessage.setFrom(fromAddress);
           simpleMessage.setRecipient(RecipientType.TO, toAddress);
           simpleMessage.setSubject(subject);
           simpleMessage.setText(message1);

           Transport.send(simpleMessage);
       } catch (MessagingException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }



Step 4:
Run JSP File and insert input fields















Step 4:
Check Mail To your Account









You Want to attach file into mail then replace code to this code

         
               /* String host = "smtp.gmail.com";//host name
   String from = "xxxx@gmail.com";//sender id
   String to = "xxxxx@gmail.com";//reciever id
   String pass = "*******";//sender's password
   String fileAttachment = "E:\\Exaple.txt";//file name for attachment
   //system properties
   Properties prop = System.getProperties();
   // Setup mail server properties
   prop.put("mail.smtp.gmail", host);
   prop.put("mail.smtp.starttls.enable", "true");
   prop.put("mail.smtp.host", host);
   prop.put("mail.smtp.user", from);
   prop.put("mail.smtp.password", pass);
   prop.put("mail.smtp.port", "465");
   prop.put("mail.smtp.auth", "true");
   //session
   Session session = Session.getInstance(prop, null);
   // Define message
   MimeMessage message = new MimeMessage(session);
   message.setFrom(new InternetAddress(from));
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
   message.setSubject("subject");
   // create the message part
   MimeBodyPart messageBodyPart = new MimeBodyPart();
   //message body
   messageBodyPart.setText("Hi");
   Multipart multipart = new MimeMultipart();
   multipart.addBodyPart(messageBodyPart);
   //attachment
   messageBodyPart = new MimeBodyPart();
   DataSource source = new FileDataSource(fileAttachment);
   messageBodyPart.setDataHandler(new DataHandler(source));
   messageBodyPart.setFileName(fileAttachment);
   multipart.addBodyPart(messageBodyPart);
   message.setContent(multipart);
   //send message to reciever
   Transport transport = session.getTransport("smtp");
   transport.connect(host, from, pass);
   transport.sendMessage(message, message.getAllRecipients());
   transport.close();*/



No comments:

Post a Comment