Getting a sent MailMessage into the “Sent Folder”

后端 未结 4 873
深忆病人
深忆病人 2021-01-01 22:02

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 addr

4条回答
  •  自闭症患者
    2021-01-01 22:27

    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("john.smith@example.com");
    em.ToRecipients.Add(new Microsoft.Exchange.WebServices.Data.EmailAddress("bob.smith@example.com"));
    
    // Send the email and put it into the SentItems:
    em.SendAndSaveCopy(WellKnownFolderName.SentItems);
    

提交回复
热议问题