1
votes

I am trying to develop an add in for Outlook in Visual Studio under .net framework 4.0. I used smtp protocol for sending an email from my Outlook addin. I am not able to find the sent mail in sent folder of Outlook.

How do I store sent mail in the sent folder of Outlook?

Till now I have written this code for sending mail.

public bool SendEMail(){ 
   MailMessage mailNew = new MailMessage();
    var smtp = new SmtpClient("SmtpServer")
    {
     EnableSsl = false,
     DeliveryMethod = SmtpDeliveryMethod.Network
     };
    smtp.Port = 587;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.UseDefaultCredentials = false; 
    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("UserName", "password");
    smtp.EnableSsl = false;
    smtp.Credentials = credentials;
    MailAddress mailFrom = new MailAddress("[email protected]");
    mailNew.From = mailFrom;
    mailNew.To.Add("[email protected]");
    mailNew.Subject = Subject;
    mailNew.IsBodyHtml = Html;
    mailNew.Body = Body;
   smtp.Send(mailNew);
   return true;
}

I want to add coding for storing the sent mail in sent folder of Outlook.

1

1 Answers

0
votes

You will need to create a fake sent item. Note that messages in the Outlook Object Model are created in the unsent state, which cannot be modified. The only exception is the post item. The following VB script creates a poat item in the Sent Item folder, resets the message class, reopens it as a regular MailItem (which is now in the sent state). Note that you cannot set the sender related properties using OOM alone and you cannot set the sent/received dates.

'create PostItem
set msg = Application.Session.GetDefaultFolder(olFolderSentMail).Items.Add("IPM.Post")
msg.MessageClass = "IPM.Note"
msg.Subject = "fake sent email"
msg.Body = "test"
msg.Save
vEntryId = msg.EntryID
set msg = Nothing 'release the mesage
'and reopen it as MailItem
set msg = Application.Session.GetItemFromID(vEntryId)
'make sure PR_ICON_INDEX is right
msg.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x10800003", -1
set vRecip = msg.Recipients.Add("[email protected]")
vRecip.Resolve
msg.Save

If using Redemption is an option, it lets you set the sent state before the first save (MAPI limitation) and allows to set the sender and date properties correctly:

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set msg = Session.GetDefaultFolder(olFolderSentMail).Items.Add("IPM.Note")
msg.Sent = true
msg.Subject = "fake sent email"
msg.Body = "test"
set vRecip = msg.Recipients.Add("[email protected]")
vRecip.Resolve
'dates
msg.SentOn = Now
msg.ReceivedTime = Now
'create fake sender
vSenderEntryID = Session.AddressBook.CreateOneOffEntryID("the sender", "SMTP", "[email protected]", true, true)
set vSender = Session.AddressBook.GetAddressEntryFromID(vSenderEntryID)
msg.Sender = vSender
msg.SentOnBehalfOf = vSender
msg.Save