How To Send Email Using Java Mail

Can someone please help with the following code
If (a===b)
//then send email using java mail API

How do I code above?

Look thru java mail API's there are methods like
public void sendMail() etc.

You need to follow a few steps as given below:

//Create an instance of properties
    Properties props = new Properties();
    //Setup mail server
    Authenticator auth = new PopupAuthenticator();
    //Set the Servername and hostname of email Server
    props.put("mail.smtp.auth", "true");

    //Get session
    Session session = Session.getInstance(props, auth);
    //Create Message instance for email content
    Message msg = new MimeMessage(session);
    //senders mail id
    Address loggerId = new InternetAddress(sender, sender);
    //Set the from email id
    multipleEmailsSplit(receiver, msg, Message.RecipientType.TO);
    if ((receiverCC != null) && !receiverCC.equals("null")) {
     multipleEmailsSplit(receiverCC, msg, Message.RecipientType.CC);
    if ((receiverBCC != null) && !receiverBCC.equals("null")) {
     multipleEmailsSplit(receiverBCC, msg, Message.RecipientType.BCC);
    MimeBodyPart mbp = new MimeBodyPart();
    mbp.setContent(emailContent, "text/html");
    // create the Multipart and add its parts to it
    Multipart mp = new MimeMultipart();
    //attach files to e-mail
    for (int i = 0; i < fileList.size(); i++) {
     String file = (String) fileList.get(i);
     MimeBodyPart mbp2 = new MimeBodyPart();
     FileDataSource fds = new FileDataSource(file);
     mbp2.setDataHandler(new DataHandler(fds));
    // add the Multipart to the message
    //Send the mail


-  Create the mail session object  (or get/lookup the session from your app server using JNDI)
-  Compose message, set/add recipient(s)
-  Send using Transport's static send method

Properties props = new Properties();
props.put("", "");
Session s = Session.getInstance(props,null);

InternetAddress from = new InternetAddress("mail@...");
InternetAddress to = new InternetAddress(recepeint@...");

MimeMessage message = new MimeMessage(s);
message.addRecipient(Message.RecipientType.TO, to);

message.setSubject("Your subject");
message.setText("Your text");

If you lookup up the session using JNDI you configure mail server (smtp host) in your app server configuration

Quick Links:
Have Java Problem
Do you have a Java Question?

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Best Regards,
Java Programming Hints and Tips

All the site contents are Copyright © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of or the content authors.