Code for Java-Mail

I am sending the code for Java-Mail.

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;
import javax.swing.text.*;
import javax.swing.*;
import java.awt.*;

public class SendMail{
     public SendMail(){  }
     private class SMTPAuthenticator extends javax.mail.Authenticator{
              public PasswordAuthentication getPasswordAuthentication(){
                  String username      = "Muni";
                  String password      = "Raj";
                  return new PasswordAuthentication(username, password);
public void postMail() throws MessagingException {

              boolean debug                = false;
               String recipients[]          = {""};
              String recipientsCC[]     = {""};

               String subject                = "Mail";
               String message               = "This is body for the message";
               String from                = "";
              Properties props           = new Properties();
              Authenticator auth       = new SMTPAuthenticator();
              Session session           = Session.getDefaultInstance(props, auth);
              Message msg                = new MimeMessage(session);
              // set the from and to address
              InternetAddress addressFrom = new InternetAddress(from);
              InternetAddress[] addressTo = new InternetAddress[recipients.length];
              for(int i = 0; i < recipients.length; i++){
                      addressTo[i] = new InternetAddress(recipients[i]);
              msg.setRecipients(Message.RecipientType.TO, addressTo);

              InternetAddress[] addressCC = new InternetAddress[recipientsCC.length];
              for(int j = 0; j < recipientsCC.length; j++){
                      addressCC[j] = new InternetAddress(recipientsCC[j]);
              msg.setRecipients(Message.RecipientType.CC, addressCC);

              // Optional : You can also set your custom headers in the Email if you Want
              msg.addHeader("MyHeaderName", "myHeaderValue");
              System.out.println("Mail Successfully Sent");

     }//End of  postMail()

}//End of Class

~~ Muni

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.