How send mail in servlet in eclipse mars

To send an email from a servlet in Eclipse Mars, you can follow these steps:

Step 1: Add the necessary libraries

In your Eclipse project, right-click on the project and select "Properties". Then, go to "Java Build Path" and click on the "Libraries" tab. Click on "Add External JARs" and add the following libraries:

You can download these libraries from the official Oracle website or use the ones provided by your Eclipse installation.

Step 2: Create a JavaMail session

In your servlet, create a JavaMail session using the following code:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

//...

Session session = Session.getDefaultInstance(new Properties(), null);

Step 3: Create a MimeMessage

Create a MimeMessage object to represent the email:

MimeMessage message = new MimeMessage(session);

Step 4: Set the email properties

Set the email properties such as the sender, recipient, subject, and body:

message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setSubject("Test Email");
message.setText("This is a test email sent from a servlet.");

Step 5: Send the email

Use the Transport class to send the email:

Transport.send(message);

Step 6: Add the email sending code to your servlet

Add the email sending code to your servlet's doPost() or doGet() method:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //...

    Session session = Session.getDefaultInstance(new Properties(), null);
    MimeMessage message = new MimeMessage(session);

    message.setFrom(new InternetAddress("[email protected]"));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
    message.setSubject("Test Email");
    message.setText("This is a test email sent from a servlet.");

    Transport.send(message);

    //...
}

Step 7: Run your servlet

Run your servlet by clicking on the "Run" button in Eclipse or by using the "Run As" menu.

That's it! You should now be able to send an email from your servlet in Eclipse Mars.

Here's the complete code:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class EmailServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Session session = Session.getDefaultInstance(new Properties(), null);
        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
        message.setSubject("Test Email");
        message.setText("This is a test email sent from a servlet.");

        Transport.send(message);
    }
}

Remember to replace "[email protected]" and "[email protected]" with your actual email addresses.