boolean messageSent = false;
if (mailMessageVO == null) {
throw new SendFailedException("<error>Message cannot be null. Please set up the message.</error>");
}// end if (mailMessageVO == null)
CVDal cvdal = new CVDal(this.dataSource);
try{
String smtpServer = null;
String username = null;
String password = null;
int smtpPort = 25;
boolean connectToPopFirst = false;
boolean smtpAuthenticationRequired = false;
String serverType = null;
String serverAddress = null;
String adminEmailAddress = "";
String query = "SELECT smtpserver, username, password, authentication, smtpport FROM emailsettings";
cvdal.setSqlQuery(query);
Collection results = cvdal.executeQuery();
if (results != null) {
Iterator iter = results.iterator();
if (iter.hasNext()) {
HashMap folderInfo = (HashMap)iter.next();
smtpPort = ((Number)folderInfo.get("smtpport")).intValue();
smtpServer = (String)folderInfo.get("smtpserver");
username = (String)folderInfo.get("username");
password = (String)folderInfo.get("password");
String authentication = (String)folderInfo.get("authentication");
if (authentication != null && authentication.equals("YES")){
smtpAuthenticationRequired = true;
}
adminEmailAddress = (String)folderInfo.get("adminemailaddress");
}
}
Address arrayBcc[] = new Address[0];
Address arrayCc[] = new Address[0];
Address arrayTo[] = new Address[0];
//Build the JavaMail message
Properties props = System.getProperties();
if (smtpServer != null) {
props.put("mail.smtp.host", smtpServer);
}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();
if (invalidAddress != null){
mailFailedList += " "+ toAddress + "<br>";
messageSent = false;
} else {
throw new SendFailedException(sfe.getMessage());
}
}
if (messageSent){
// if the message is sent successfully to individual then add to the send list
mailSendList += " "+ toAddress + "<br>";
}
}
}
// If suppose the mailFailedList is not blank. then we have to throw error stating the succesfully send list and failed list
if ((mailFailedList != null && !mailFailedList.equals(""))){
//Format the message for failed user and successfully sent individual's
// TODO: remove this HTML from the EJB layer!!!!!!! *sigh*
String errorMessage = "<failed> <font color=\"#008000\"> Successfully Sent to : <br>"+mailSendList
+"</font>"
+"<font color=\"#FF0000\"><br> Failed : <br>"+mailFailedList
+"</failed></font>";
throw new SendFailedException(errorMessage);
}
} finally {
cvdal.destroy();
}
return messageSent;
}// end of boolean simpleMessage(int individualID, Message message)