Hello,
Can you try with following code. Alternatively if you are using spring framework, then you can try using
org.springframework.mail.javamail.JavaMailSenderImpl
.
public void doSend(MimeMessage msg, String strHost, int intPort, String strUser, String strPass) {
String msgId = null;
Session ses = null;
Transport trans = null;
try {
ses = Session.getInstance(new Properties());
trans = session.getTransport("smtp");
trans.connect(strHost, intPort, strUser, strPass);
if (null == msg.getSentDate()) msg.setSentDate(new Date());
msgId = msg.getMessageID();
msg.saveChanges();
if (null == msgId) msg.setHeader(HEADER_MESSAGE_ID, msgId);
trans.sendMessage(msg, msg.getAllRecipients());
} catch(AuthenticationFailedException ex) {
log.error("Unable to authenticate with the mail server!", ex);
} catch (MessagingException ex) {
log.error("Unexpected error occurred while sending the message!", ex);
} finally {
doCleanup(trans);
if (ses != null) ses = null;
if (trans != null) trans = null;
}
}
private void doCleanup(Transport in) {
try {
if (in != null) in.close();
} catch (MessagingException ex) {
log.error("Unexpected error occurred while closing the transport!", ex);
}
}
Regards,