无法获取IIS拾取目录(从Windows服务)

本文关键字:Windows 服务 获取 IIS | 更新日期: 2023-09-27 18:08:15

我在windows service发送电子邮件时遇到了上述错误。非常感谢您的建议。

SmtpClient smtpClient = new SmtpClient();
MailMessage mailMsg = this.ComposeMailMessage();
smtpClient.Send(mailMsg);

配置

<system.net>
    <mailSettings>
        <smtp from="user1@mycompany.com" deliveryMethod="Network">
            <network host="smtpsvr.mycompany.com" port="25" defaultCredentials="true" />
        </smtp>
    </mailSettings>
</system.net>

无法获取IIS拾取目录(从Windows服务)

也许我的不是你要找的答案,但是试试这个(避免IIS):

try
{
    // To
    MailMessage mailMsg = new MailMessage();
    mailMsg.To.Add(to_Address);
    // From
    MailAddress mailAddress = new MailAddress(from_address);
    mailMsg.From = mailAddress;
    // Subject and Body
    mailMsg.Subject = subject;
    mailMsg.Body    = body;
    // Init SmtpClient and send
    SmtpClient smtpClient = new SmtpClient(smtp_server, port);
    // System.Net.NetworkCredential credentials =
    //    new System.Net.NetworkCredential(smtp_user, smtp_pwd);
    // smtpClient.Credentials = credentials;
    smtpClient.Send(mailMsg);
}
catch (Exception ex)
{
    Console.WriteLine( ex.Message );
}

您可能需要手动设置SmtpClient拾取目录。

Lukas Pokorny的回答中有一个如何做到这一点的例子:

无法获取IIS拾取目录

尝试将以下行添加到程序中。这一行需要IIS管理和SMTP服务在您的桌面上运行。

smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

就变成这样了

SmtpClient smtpClient = new SmtpClient();
MailMessage mailMsg = this.ComposeMailMessage();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtpClient.Send(mailMsg);