try {
// I'm assuming that the sent folder is called Sent.
// big assumption, I know.
MailFolderVO sentFolder = this.getEmailFolderByName(mailAccountVO.getAccountID(), MailFolderVO.SENT_FOLDER_NAME);
CvFolderVO attachmentFolder = this.getAttachmentFolder(mailAccountVO.getOwnerID());
Address arrayBcc[] = new Address[0];
Address arrayCc[] = new Address[0];
Address arrayTo[] = new Address[0];
//Build the JavaMail message
Properties props = System.getProperties();
if (mailAccountVO.getSmtpServer() != null) {
props.put("mail.smtp.host", mailAccountVO.getSmtpServer());
} else {
logger.error("[sendMessage]: The accountVO doesn't have a smtpserver setup: "+mailAccountVO);
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();
arrayBcc = this.getAddressAddress(bccList);
arrayCc = this.getAddressAddress(ccList);
arrayTo = this.getAddressAddress(toList);
message.addRecipients(Message.RecipientType.TO, arrayTo);
message.addRecipients(Message.RecipientType.CC, arrayCc);
message.addRecipients(Message.RecipientType.BCC, arrayBcc);
message.setFrom(new InternetAddress(fromAddress));
// So we don't require a Reply-To address
if (replyToAddress != null && replyToAddress.length() != 0) {
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());
//End of Build The JavaMail message
messageSent = this.sendSimpleMessage(message, mailAccountVO.getSmtpServer(),
mailAccountVO.getLogin(), mailAccountVO.getPassword(), mailAccountVO.getSmtpPort(),
mailAccountVO.isPopRequiredBeforeSMTP(), mailAccountVO.isAuthenticationRequiredForSMTP(),
mailAccountVO.getAccountType(), mailAccountVO.getMailServer());
//Save Message to Sent Folder
if (messageSent) {
logger.error("[sendMessage]["+dataSource+"]: Mail Message Successfully sent.");
int messageID = this.addMessage(message, "", mailAccountVO.getAccountID(), mailAccountVO.getOwnerID(), sentFolder.getFolderID(), cvdal);
HashMap embededImageMap = new HashMap();
this.saveAttachments(this.getPartContent(message), messageID, mailAccountVO.getAccountID(), mailAccountVO.getOwnerID(), attachmentFolder.getFolderId(), cvdal,embededImageMap);
this.addBody(message, messageID, embededImageMap, cvdal);
embededImageMap = null;
}
} finally {
cvdal.destroy();