}else{
throw new SendFailedException("<error>The SMTP Server has not been setup.</error>");
}
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
Collection bccList = mailMessageVO.getBccList();
Collection ccList = mailMessageVO.getCcList();
Collection toList = mailMessageVO.getToList();
Collection attachments = mailMessageVO.getAttachedFiles();
String subject = mailMessageVO.getSubject();
String body = mailMessageVO.getBody();
String fromAddress = mailMessageVO.getFromAddress();
String replyToAddress = mailMessageVO.getReplyTo();
String headers = mailMessageVO.getHeaders();
String messageType = mailMessageVO.getContentType();
//if you don't specify the from email address we will enter the administrator email address
if(fromAddress == null){
fromAddress = adminEmailAddress;
}
message.setFrom(new InternetAddress(fromAddress));
if (replyToAddress != null && !replyToAddress.equals("")){
message.setReplyTo(new Address[] {new InternetAddress(replyToAddress)});
}
//Add raw headers to message object
StringTokenizer tokenizer = new StringTokenizer(headers, System.getProperty("line.separator", "\n"));
while (tokenizer.hasMoreTokens()) {
message.addHeaderLine(tokenizer.nextToken());
}
//Most email clients add this line with the name of
//their software and the version
message.addHeader("X-Mailer", "Centraview v. " + CentraViewConfiguration.getVersion());
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body, messageType);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Handle attachments
if (attachments != null) {
message.setContent(multipart);
Iterator attachmentIterator = attachments.iterator();
while (attachmentIterator.hasNext()) {
messageBodyPart = new MimeBodyPart();
CvFileVO thisAttachment = (CvFileVO) attachmentIterator.next();
String path = thisAttachment.getPhysicalFolderVO().getFullPath(null, true) + thisAttachment.getName();
DataSource source = new FileDataSource(path);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(thisAttachment.getTitle());
multipart.addBodyPart(messageBodyPart);
}
}
message.setSentDate(new Date());
String mailSendList = "";
String mailFailedList = "";
//End of Build The JavaMail message
// We must have to send seperate message to individual.
// We must have to keep track of message sent to list and failed while send message to particular individual.
if (toList != null) {
Iterator toIterator = toList.iterator();
int count = 0;
while (toIterator.hasNext()) {
String toAddress = (String) toIterator.next();
message.setRecipients(Message.RecipientType.TO, toAddress);
try{
messageSent = this.sendSimpleMessage(message, smtpServer, username, password, smtpPort, connectToPopFirst, smtpAuthenticationRequired, serverType, serverAddress);
} catch(SendFailedException sfe) {
// we will catch the invalid Address and by this way we will know that recipient will not receive the mail and we add individual in the failing list.
Address[] invalidAddress = sfe.getInvalidAddresses();