props.put("mail.smtp.host", mailmessage.getSmtpserver());
}else{
return(false);
}
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
int accountid = mailmessage.getAccountID();
ArrayList bcc = mailmessage.getBcc();
//System.out.println("bcc"+bcc);
//System.out.println("bcc.size()"+bcc.size());
String arraybcc[] = new String[bcc.size()];
for (int i = 0; i < arraybcc.length; i++)
{
MailAddress ma = (MailAddress) bcc.get(i);
arraybcc[i] = ma.getAddress();
//System.out.println("arraybcc["+i+"]"+arraybcc[i]);
}
ArrayList cc = mailmessage.getCc();
//System.out.println("cc"+cc);
//System.out.println("cc.size()"+cc.size());
String arraycc[] = new String[cc.size()];
for (int i = 0; i < arraycc.length; i++)
{
MailAddress ma = (MailAddress) cc.get(i);
arraycc[i] = ma.getAddress();
//System.out.println("arraycc["+i+"]"+arraycc[i]);
}
ArrayList to = mailmessage.getTo();
// System.out.println("to"+to);
//System.out.println("to.size()"+to.size());
String arrayto[] = new String[to.size()];
for (int i = 0; i < arrayto.length; i++)
{
MailAddress ma = (MailAddress)to.get(i);
arrayto[i] = ma.getAddress();
//System.out.println("arrayto["+i+"]"+arrayto[i]);
}
long l = (new java.util.Date()).getTime();
java.sql.Timestamp messagedate = (java.sql.Timestamp) new java.sql.Timestamp(l);
String subject = mailmessage.getSubject();
String body = mailmessage.getBody();
String from = mailmessage.getMailFrom();
String replyTo = mailmessage.getReplyTo();
if (replyTo != null && replyTo.length() >0)
{
InternetAddress[] ia = new InternetAddress[1];
ia[0] = new InternetAddress(replyTo);
message.setReplyTo(ia);
}
// extra headers that may be used for some action in to CV
// eg - Activity send email, Adding individual throough email
HashMap hhm = mailmessage.getHeadersHM();
String allHeaderString = null;
if (hhm != null)
{
allHeaderString = new String();
Iterator it = hhm.keySet().iterator();
while (it.hasNext())
{
String hmKey = (String) it.next();
String hmValue = (String) hhm.get(hmKey);
allHeaderString = allHeaderString.concat(hmKey + "=" + hmValue + "::");
// message.addHeader(hmKey, hmValue);
}
}
//cvdl = new CVDal(dataSource);
cvdl.setSql("email.savedraft1");
cvdl.setString(1, from);
cvdl.setString(2, subject);
cvdl.setString(3, replyTo);
cvdl.setString(4, body);
cvdl.setString(5, allHeaderString);
cvdl.setInt(6, accountid);
cvdl.setInt(7, userId);
cvdl.executeUpdate();
messageid = cvdl.getAutoGeneratedKey();
message.addHeader("X-centraviewID", "" + messageid);
cvdl.clearParameters();
cvdl.setSql("email.savedraft4");
cvdl.setInt(1, messageid);
cvdl.setString(2, "NO");
cvdl.setInt(3, accountid);
cvdl.executeUpdate();
cvdl.clearParameters();
if (arrayto.length != 0)
{
for (int i = 0; i < arrayto.length; i++)
{
cvdl.setSql("email.savedraft3");
cvdl.setInt(1, messageid);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(arrayto[i]));
cvdl.setString(2, arrayto[i]);
cvdl.setString(3, "TO");
cvdl.setString(4, "NO");
cvdl.executeUpdate();
cvdl.clearParameters();
}
}
if (arraybcc.length != 0)
{
for (int i = 0; i < arraybcc.length; i++)
{
cvdl.setSql("email.savedraft3");
cvdl.setInt(1, messageid);
cvdl.setString(2, arraybcc[i]);
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(arraybcc[i]));
cvdl.setString(3, "BCC");
cvdl.setString(4, "NO");
cvdl.executeUpdate();
cvdl.clearParameters();
}
}
if (arraycc.length != 0)
{
for (int i = 0; i < arraycc.length; i++)
{
//System.out.println("arraycc[i]"+arraycc[i] );
cvdl.setSql("email.savedraft3");
cvdl.setInt(1, messageid);
cvdl.setString(2, arraycc[i]);
message.addRecipient(Message.RecipientType.CC, new InternetAddress(arraycc[i]));
cvdl.setString(3, "CC");
cvdl.setString(4, "NO");
cvdl.executeUpdate();
cvdl.clearParameters();
}
}
message.setFrom(new InternetAddress(from));
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
//Added by Ryan Grier <ryan@centraview.com>
//Need to send email as plain text if
//it contains no html content.
String messageContext = "text/plain";
// Modified by Deepa
// User Preferences value will decide content type of the email. ie. Plain text or Html text
InitialContext ic = CVUtility.getInitialContext();
PreferenceLocalHome home = (PreferenceLocalHome)ic.lookup("local/Preference");
PreferenceLocal localPref = home.create();
localPref.setDataSource(this.dataSource);
UserPrefererences userPref = localPref.getUserPreferences(userId);
if(userPref != null)
if (userPref.getContentType() != null && userPref.getContentType().equals("PLAIN"))
{
messageContext = "text/plain";
}
else if ( userPref.getContentType() != null && userPref.getContentType().equals("HTML"))
{
messageContext = "text/html";
}
if(mailmessage.getContentType() != null && mailmessage.getContentType().equals("HTML") ){
messageContext = "text/html";
}
/* if ((body.indexOf("<") > -1) && (body.indexOf(">") > -1))
{
messageContext = "text/html";
} //end of if statement ((body.indexOf("<") > -1) && (body.indexOf(">") > -1))
else if ((body.indexOf("<") > -1) && (body.indexOf(">") > -1))
{
messageContext = "text/html";
} //end of if statement ((body.indexOf("<") > -1) && (body.indexOf(">") > -1))
*/
messageBodyPart.setContent(body, messageContext);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
HashMap attchmentids = mailmessage.getAttachFileIDs();
if ((attchmentids != null) && (attchmentids.size() != 0))
{
Set col = attchmentids.keySet();
Iterator itt = col.iterator();
int i = 0;
while (itt.hasNext())
{
String fileid = (String) itt.next();
String name = (String) attchmentids.get(fileid);
cvdl.setSql("email.savedraftattchment");
cvdl.setInt(1, messageid);
cvdl.setString(2, name);
cvdl.setInt(3, Integer.parseInt(fileid));
cvdl.executeUpdate();
cvdl.clearParameters();
i++;
CvFileFacade cvfile = new CvFileFacade();
CvFileVO cvfilevo = cvfile.getEmailAttachment(userId, Integer.parseInt(fileid), this.dataSource);
String path = cvfilevo.getPhysicalFolderVO().getFullPath(null, true) + cvfilevo.getName();
DataSource source = new FileDataSource(path);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(name);
multipart.addBodyPart(messageBodyPart);
} //end of while loop(itt.hasNext())
} //end of if statement ((attchmentids != null) && (attchmentids.size() != 0))
// delete mail from drafts
if (mailmessage.getMessageID() != 0)
{
String mailIdList[] = {"" + mailmessage.getMessageID()};
InitialContext icPref = CVUtility.getInitialContext();
EmailManageLocalHome emailManageHome = (EmailManageLocalHome) icPref.lookup("local/EmailManage");
EmailManageLocal local = emailManageHome.create();
local.setDataSource(this.dataSource);
int result = local.emailDeleteTrash(mailmessage.getFolder(), mailIdList);
} //end of if statement (mailmessage.getMessageID() != 0)
message.setContent(multipart);
try
{
Transport.send(message);
}catch(SendFailedException sendFailed){
System.out.println("\n\n\nSendFailedException occurred!!!\n");