Loading
Send Email withOut Attachments

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Send Email with Attachments


Ans)

Sending Email without Attachments By using Java Mail:

This MailNotification Class holds a method that sends the emails without attachments
by using Java Mail API.

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

 public class MailNotificationSender  {
  private Session mailConn = null;
  private String msgFromDefault = null;
  private String mailUser = null;
  private String mailPass = null;
  private String mailHost = null;
  private static Logger logger = Logger.getLogger(MailNotificationSender.class.getName());
 
  /**
   * this constrcutor config the message server details
   *
   * @throws NotificationException
   */  
  public MailNotificationSender() throws NotificationException  {
    try {
      Properties mailprops = new Properties();
      MailAuthenticator mailAuth = null;
      msgFromDefault = PropertyReader.readProperty(
                NotificationConstant.MAIL_FROM_DEFAULT);
      mailUser = PropertyReader.readProperty(
                NotificationConstant.MAIL_USER);
      mailPass = PropertyReader.readProperty(
        NotificationConstant.MAIL_PASS);
      mailHost = PropertyReader.readProperty(
        NotificationConstant.MAIL_HOST)!=null?NotificationPropertyReader.readProperty(
        NotificationConstant.MAIL_HOST):
       InetAddress.getLocalHost().getHostName();
      //initialize mail properties
      mailprops.setProperty("mail.smtp.host",mailHost);
      //if username and password provided, create an authenticator
      if(mailUser != null && mailPass != null) {
        mailAuth = new MailAuthenticator(mailUser,mailPass);
      }
      //Create the mail connection
      //mailConn = Session.getDefaultInstance(mailprops,mailAuth);
   mailConn = Session.getDefaultInstance(mailprops,null);
    }catch(Exception e){
      //log the error
      error("init()", e.getMessage());
      e.printStackTrace();
    }
  }
 
 
  /**
   * sends email from SMTP server
   *
   * @param String msgTo
   * @param String msgCCTo
   * @param String msgFrom
   * @param String msgReplyTo
   * @param String msgSubject
   * @param String msgContent
   * @param String msgContentType
   */
  public void sendMail(String msgTo, String msgCCTo, String msgFrom,
    String msgSubject, String msgContent, String msgContentType,
    String msgReplyTo ) {
    try{
      Message msg = new MimeMessage(mailConn);
      //from address
      if(msgFrom == null || msgFrom.length() == 0) msgFrom = msgFromDefault;
      msg.setFrom(new InternetAddress(msgFrom));
      //replyTo address
      if ( msgReplyTo != null && msgReplyTo.length() != 0 ) {
       msg.setReplyTo( InternetAddress.parse(msgReplyTo, false));
   }
      //to address
      checkRecipients(msgTo, msg, false);
      if (msgCCTo != null && msgCCTo.trim().length() > 0){
       checkRecipients(msgCCTo, msg, true );
      }
      //subject
      msg.setSubject(msgSubject);
      //message body
      msg.setContent(msgContent, msgContentType);
      try{
      Transport.send(msg);
      }catch(SendFailedException sfe){
       error("sendMail", "Send Mail Failed:" + sfe.toString());
      }
    }catch(Exception e){
      error("sendMail()",e.toString());
    }
  }
}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Send Email withOut Attachments


Send Email withOut Attachments

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free