Current File : /home/obabain/anms_obaba_in/mailmessage.jsp
<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%> 
<%@ page import="javax.servlet.http.*,javax.servlet.*" %> 
<%

    //Creating a result for getting status that messsage is delivered or not!

    String result;

    // Get recipient's email-ID, message & subject-line from index.html page

    final String to = request.getParameter("mail");

    final String subject = request.getParameter("sub");

    final String messg = request.getParameter("mess");

 

    // Sender's email ID and password needs to be mentioned

    final String from = "dsharina073@gmail.com";

    final String pass = "sheri123";

 

    // Defining the gmail host


 

    // Creating Properties object

    Properties props = new Properties();

 

    // Defining properties

    props.put("mail.smtp.host", "smtp.gmail.com");


    props.put("mail.smtp.auth", "true");

    props.put("mail.smtp.starttls.enable", "true");

    props.put("mail.user", from);

    props.put("mail.password", pass);

    props.put("mail.smtp.port", "587");

 

    // Authorized the Session object.

    Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {

        @Override

        protected PasswordAuthentication getPasswordAuthentication() {

            return new PasswordAuthentication(from, pass);

        }

    });
 
    try {
        // Create a MimeMessage object.
        MimeMessage message = new MimeMessage(mailSession);
     
        // Set From: header field
        message.setFrom(new InternetAddress(from));
     
        // Set To: header field
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
     
        // Set Subject:
        message.setSubject(subject);
     
        // Create the message section
        BodyPart messageBodyPart = new MimeBodyPart();
     
        // Fill the message
        messageBodyPart.setText(messg);
        
        Multipart multipart = new MimeMultipart(); 
        multipart.addBodyPart(messageBodyPart); 
        messageBodyPart = new MimeBodyPart(); 
		String path="C:\\Users\\Admin\\Desktop\\Adobe Scan Aug 30, 2021.pdf";
        DataSource source = new FileDataSource(path); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName(path); 
        multipart.addBodyPart(messageBodyPart);   
        message.setContent(multipart );
     
        // Send message
        Transport.send(message);
        String title = "Send Email";
        result = "Sent message successfully....";
       } catch (MessagingException mex) {
        mex.printStackTrace();
        result = "Error: unable to send message....";
       }
    %>
     
<html> 
<head> 
<title>Send email</title> 
</head> 
<body> 
<center> <h1>Send Attachement Email using JSP</h1> </center> 
<p align="center"> 
<% out.println("Result: " + result + "\n"); %> 
</p>
</body>
</html>