if the SMTP host is connecting, then only we can send the mails.
And more over API jars are necessary
public void sendEmailCommPaymentDetails(String filePath, String batchSeqNo) {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "<YOUR SMTP HOST>");
javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Message msg = new MimeMessage(mailSession);
try {
Transport transport = mailSession.getTransport();
InternetAddress from = new InternetAddress("mail.id", "mail.id");
msg.setFrom(from);
//msg.addRecipient(Message.RecipientType.CC, new InternetAddress("mail.id,"mail.id"));
msg.addRecipient(Message.RecipientType.CC, new InternetAddress("mail.id,"mail.id"));
msg.setSubject("subject" );
Multipart multipart = new MimeMultipart("related");
String reportHeader0 = "Dear User,";
String reportHeader1 = "Please find attached Payment Details for the Batches eligible for Commission.";
String reportHeader2 = "Regards,";
String reportHeader4 = "Please don't reply for this message as it is system generated ";
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<html><body>"+
"<font face='calibri' size='4'>"+reportHeader0+"</font><br/><br/>"+
"<font face='calibri' size='4'>"+reportHeader1+"</font><br/><br/>"+
"<font face='calibri' size='4'>"+reportHeader2+"</font><br/><br/>"+
"<font face='calibri' size='4'>"+reportHeader3+"</font><br/>"+
"<font face='calibri' size='4'>"+reportHeader4+"</font><br/><br/>"+
"<body></html>", "text/html");
MimeBodyPart htmlPart1 = new MimeBodyPart();
DataSource source = new FileDataSource(filePath);
htmlPart1.setDataHandler(new DataHandler(source));
htmlPart1.setFileName("Comm Payment Details.csv");
multipart.addBodyPart(htmlPart);
multipart.addBodyPart(htmlPart1);
msg.setContent(multipart);
transport.connect();
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}