0
votes

I would like to send smtp send mail from outlook-addin that mail save into outlook sent folder

Note: Save mail item in sent folder from address must be which i specified smtp from address not outlook login username.

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;
}

thanks in advance

1

1 Answers

2
votes

You will need to create a sent item in the Sent Items folder and set all the relevant properties using MailItem.PropertyAccessor. Note that PropertyAccessor will not let you set some sender related properties. In Outlook 2010 or higher you can set the MAilItem.Sender property.

Also note that the sent flag cannot be changed after the message is saved, so to create a sent item using OOM, you will need to create a post item, then change its MessageClass property to IPM.Note.

Also note that ReceivedTime and SentOn properties are reset by OOM every time the message is saved.

If using Redemption is an option, you can do something like the following (off the top of my head):

Redemption.RDOSession session = new Redemption.RDOSession();
session.MAPIOBJECT = Application.Session.MAPIOBJECT;
Redemption.RDOMail message = session.GetDefaultFolder(rdoDefaultFolders.olFolderSentMail).Items.Add("IPM.Note");
message.Sent = true;
message.Subject = "test";
message.Body = "fake sent message";
message.Recipients.AddEx("The Recipient", "[email protected]", "SMTP", olTo);
string senderEntryID = session.AddressBook.CreateOneOffEntryID("Some Name", "SMTP", "[email protected]", false, true);
addressEntry = session.AddressBook.GetAddressEntryFromID(senderEntryID);
message.Sender = addressEntry;
message.SentOnBehalfOf = addressEntry;
message.Save();