Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a sent MailMessage into the "Sent Folder"

I'm sending MailMessages with an SmtpClient (being delivered successfully) using an Exchange Server but would like my sent emails to go to the Sent Folder of the email address I'm sending them from (not happening).

using (var mailMessage = new MailMessage("[email protected]", "[email protected]", "subject", "body"))
{
    var smtpClient = new SmtpClient("SmtpHost")
    {
        EnableSsl = false,
        DeliveryMethod = SmtpDeliveryMethod.Network
    };

    // Apply credentials
    smtpClient.Credentials = new NetworkCredential("smtpUsername", "smtpPassword");

    // Send
    smtpClient.Send(mailMessage);
}

Is there a configuration I'm missing that will ensure all of my sent emails from "[email protected]" arrive in their Sent Folder?

like image 991
Robert Reid Avatar asked Mar 18 '10 15:03

Robert Reid


1 Answers

I have done this, so for completeness here's how to do it properly. Using the managed exchange web service ( http://msdn.microsoft.com/en-us/library/dd633709%28EXCHG.80%29.aspx ):

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

// In case you have a dodgy SSL certificate:
System.Net.ServicePointManager.ServerCertificateValidationCallback =
            delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            };

service.Credentials = new WebCredentials("username", "password", "MYDOMAIN");
service.Url = new Uri("https://exchangebox/EWS/Exchange.asmx");

EmailMessage em = new EmailMessage(service);
em.Subject = "example email";
em.Body = new MessageBody("hello world");
em.Sender = new Microsoft.Exchange.WebServices.Data.EmailAddress("[email protected]");
em.ToRecipients.Add(new Microsoft.Exchange.WebServices.Data.EmailAddress("[email protected]"));

// Send the email and put it into the SentItems:
em.SendAndSaveCopy(WellKnownFolderName.SentItems);
like image 86
Rocklan Avatar answered Oct 22 '22 07:10

Rocklan