通过默认帐户发送Outlook电子邮件

时间:2018-03-13 14:10:19

标签: c# outlook

您好

如何在配置多个邮件帐户时通过默认邮件帐户以编程方式在C#中发送Outlook邮件?

感谢

1 个答案:

答案 0 :(得分:0)

默认帐户是什么意思? 如果您想使用该程序通过该帐户发送,并且您有一台交换服务器,您可以使用:

    string exchangeServer;
    public void SendMail(string recipient, string subject, string body)
    {
        var service = CreateService(recipient);      
        EmailMessage message = new EmailMessage(service);
        message.Subject = subject;
        message.Body = body;
        message.ToRecipients.Add(recipient);
        message.SendAndSaveCopy();
    }

    private ExchangeService CreateService(string recipient)
    {
        var service = new ExchangeService(ExchangeVersion.Exchange2010);
        service.Url = new Uri(exchangeServer);
        service.Credentials = new WebCredentials(Credential.impersonateUser, Credential.impersonatePassword);
        service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, recipient);
        service.HttpHeaders.Add("X-AnchorMailbox", recipient);
        return service;
    }