"I can't tell from that mess of code where you're recording the email in the
database"
-- If it is a mess can you tell me what makes you feel so and some
suggestions for improvement?
"but if it's in the line that starts with
cmsServ.InsertCommunicationAttachment then that's inside the for loop and
will cause the same item to be inserted once for each attachment in the
email."
-- As you can see if you observe a little closely that saving into the
database is not inside the for loop. The one that is getting saved inside the
loop is to the attachment details table as below:
try
{
CMSService cmsServ = new CMSService();
newCommunicationID =
cmsServ.InsertCommunication(fromContactID, fromEmailID, toAddress, ccAddress,
bccAddress, newItem.Subject, newItem.HTMLBody, Convert.ToInt32(userID),
DateTime.Now, 0, newItem.Body, GUIDFolderName);
//Save attachments if any
if (newItem.Attachments.Count > 0)
{
ftpConnect();
ftp.ChDir("OutlookAttachments");
ftp.MkDir(GUIDFolderName);
ftp.ChDir(GUIDFolderName);
for (int i = 1; i <= newItem.Attachments.Count; i++)
{
newItem.Attachments.SaveAsFile(sourcePath + @"\"
+ newItem.Attachments.FileName);
ftp.Put(sourcePath + @"\" +
newItem.Attachments.FileName, newItem.Attachments.FileName);
File.Delete(sourcePath + @"\" +
newItem.Attachments.FileName);
cmsServ.InsertCommunicationAttachment(GUIDFolderName, newCommunicationID,
GUIDFolderName + @"\" + newItem.Attachments.FileName,
newItem.Attachments.FileName);
}
ftp.Quit();
}
}
catch (Exception ex)
{
string errorInfo = (string)ex.Message.Substring(0, 11);
}
}
Your comment regarding dot operator has been noted. Thanks.
Regards,
Aninda
wrote:
> I can't tell from that mess of code where you're recording the email in the
> database, but if it's in the line that starts with
> cmsServ.InsertCommunicationAttachment then that's inside the for loop and
> will cause the same item to be inserted once for each attachment in the
> email.
> You really should not be using multiple dot operators with the COM
> properties of Outlook items. It ends up creating invisible object variables
> you can't delete or remove. Something like:
> recpt.PropertyAccessor.GetProperty(SchemaAddressType).ToString() == "EX"
> can end up causing you lots of problems, especially if Exchange is involved.
> You should get the PropertyAccessor object, then call the GetProperty()
> method on that object instead of letting an invisible PropertyAccessor
> object be created. The same applies everywhere you are using multiple dot
> operators.
> >
>
> "anindasen_0609" <anindasen0609> wrote in message
> news:CB698B3D-C107-4FF4-B1E4-54041CC620EE@microsoft.com...
> > Here it is:
> > private void m_Items_ItemAdd(object addedItem)
> > {
> > Outlook.MailItem newItem = (Outlook.MailItem)addedItem;
> > toEmailID = userEmailID;
> > string fromEmailID = "";
> > string toAddress = null;
> > string ccAddress = null;
> > string bccAddress = null;
> > if (newItem.To != null)
> > toAddress = "";
> > if (newItem.CC != null)
> > ccAddress = "";
> > if (newItem.BCC != null)
> > bccAddress = "";
> > Outlook.Recipients recpts = newItem.Recipients;
> > if (recpts != null)
> > {
> > for (int ii = 1; ii < (recpts.Count + 1); ii++)
> > {
> > //Dim recipient As Outlook.Recipient =
> > recipients.Item(i)
> > Outlook.Recipient recpt = recpts[ii];
> > {
> > if (recpt != null)
> > {
> > if (recpt.Type == 1)
> > {
> > if
> > (recpt.PropertyAccessor.GetProperty(SchemaAddressType).ToString() == "EX")
> > {
> > toAddress +=
> > recpt.PropertyAccessor.GetProperty(SchemaSMTPAddress).ToString().Replace("'",
> > "") + ";";
> > }
> > else
> > {
> > toAddress +=
> > recpt.Address.ToString().Replace("'", "") + ";";
> > }
> > }
> > else if (recpt.Type == 2)
> > {
> > if
> > (recpt.PropertyAccessor.GetProperty(SchemaAddressType).ToString() == "EX")
> > {
> > ccAddress +=
> > recpt.PropertyAccessor.GetProperty(SchemaSMTPAddress).ToString().Replace("'",
> > "") + ";";
> > }
> > else
> > {
> > ccAddress +=
> > recpt.Address.ToString().Replace("'", "") + ";";
> > }
> > }
> > else if (recpt.Type == 3)
> > {
> > if
> > (recpt.PropertyAccessor.GetProperty(SchemaAddressType).ToString() == "EX")
> > {
> > bccAddress +=
> > recpt.PropertyAccessor.GetProperty(SchemaSMTPAddress).ToString().Replace("'",
> > "") + ";";
> > }
> > else
> > {
> > bccAddress +=
> > recpt.Address.ToString().Replace("'", "") + ";";
> > }
> > }
> > }
> > }
> > }
> > }
> > if (newItem.To != null)
> > toAddress = toAddress.Substring(0, toAddress.Length - 1);
> > if (newItem.CC != null)
> > ccAddress = ccAddress.Substring(0, ccAddress.Length - 1);
> > if (newItem.BCC != null)
> > bccAddress = bccAddress.Substring(0, bccAddress.Length -
> > 1);
> > if (newItem.SenderEmailType == "EX")
> > fromEmailID =
> > (string)newItem.PropertyAccessor.GetProperty(SchemaSMTPAddress);
> > else
> > fromEmailID = newItem.SenderEmailAddress;
> > //If fromEmailID is not in Email format get it from GAL (Global
> > Address List)
> > if (fromEmailID.IndexOf("@") <= 0)
> > {
> > fromEmailID = GetEmailID(ref fromEmailID);
> > }
> > int fromContactID = 0;
> > fromContactID = (int)GetContactID(fromEmailID);
> > //Communication Type: 0 - Incoming 1 - Outgoing
> > string GUIDFolderName = Guid.NewGuid().ToString();
> > int newCommunicationID = 0;
> > ArrayList emailList = new ArrayList();
> > try
> > {
> > CMSService cmsServ = new CMSService();
> > newCommunicationID =
> > cmsServ.InsertCommunication(fromContactID, fromEmailID, toAddress,
> > ccAddress,
> > bccAddress, newItem.Subject, newItem.HTMLBody, Convert.ToInt32(userID),
> > DateTime.Now, 0, newItem.Body, GUIDFolderName);
> > //Save attachments if any
> > if (newItem.Attachments.Count > 0)
> > {
> > ftpConnect();
> > ftp.ChDir("OutlookAttachments");
> > ftp.MkDir(GUIDFolderName);
> > ftp.ChDir(GUIDFolderName);
> > for (int i = 1; i <= newItem.Attachments.Count; i++)
> > {
> > newItem.Attachments.SaveAsFile(sourcePath + @"\"
> > + newItem.Attachments.FileName);
> > ftp.Put(sourcePath + @"\" +
> > newItem.Attachments.FileName, newItem.Attachments.FileName);
> > File.Delete(sourcePath + @"\" +
> > newItem.Attachments.FileName);
> > cmsServ.InsertCommunicationAttachment(GUIDFolderName, newCommunicationID,
> > GUIDFolderName + @"\" + newItem.Attachments.FileName,
> > newItem.Attachments.FileName);
> > }
> > ftp.Quit();
> > }
> > }
> > catch (Exception ex)
> > {
> > string errorInfo = (string)ex.Message.Substring(0, 11);
> > }
> > }
> > Regards,
> > Aninda
>