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(
     ConfigHelper.getProperty(HOST_NAME),
     ConfigHelper.getProperty(SERVER_NAME));
    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
    msg.setFrom(loggerId);
    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);
    }
    msg.setSubject(subject);
    MimeBodyPart mbp = new MimeBodyPart();
    mbp.setContent(emailContent, "text/html");
    // create the Multipart and add its parts to it
    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbp);
    //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));
     mbp2.setFileName(fds.getName());
     mp.addBodyPart(mbp2);
}
    // add the Multipart to the message
    msg.setContent(mp);
    //Send the mail
    Transport.send(msg);

or

-  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("mail.smtp.host", "mailserver.com");
Session s = Session.getInstance(props,null);

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

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

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

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

Related:

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

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com 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
www.erpgreat.com or the content authors.