Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
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());
}
}
}
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.