在Powershell中使用SmtpClient进行FQDN [英] FQDN with SmtpClient in Powershell

查看:54
本文介绍了在Powershell中使用SmtpClient进行FQDN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Powershell中使用.Net类 System.Net.Mail.SmtpClient 发送带有一些附加文档的邮件.在我们的测试环境中,这一切都很好,但是在生产中却失败了.

问题是,生产中需要完整指定的域名才能发送邮件.但是据我所知,SmtpClient不允许我指定它.我在网上阅读过,您可以在asp.net中使用web.config设置来指定域名,但是我似乎无法在Powershell中使用它.

是否可以解决此问题,还是必须用另一种语言来实现?

编辑:这是我正在使用的代码

  $ SmtpClient =新对象System.Net.Mail.SmtpClient"mail.mailserver.com",25$ Mail =新对象System.Net.Mail.MailMessage#用于创建NetworkCredential对象的自定义(工作)Cmdlet$ SmtpClient.Credentials =(GetCredential-FromFile)$ Mail.Subject =新导入"$ Mail.Body ="$ Mail.From ="myaddress@mailserver.com"$ Mail.To.Add("myaddress@mailserver.com")$ Mail.Attachments.Add(新对象System.Net.Mail.Attachment"file.txt")$ SmtpClient.Send($ Mail) 

已设置SMTP服务器,但我需要设置客户端(使用脚本的计算机)的FQDN.

解决方案

.NET Framework实现中的错误(或缺少功能,取决于您的查看方式).默认情况下,HELO命令中使用主机的NetBIOS名称,并且 SmtpClient 类没有用于在程序/脚本中配置HELO主机名的属性.请参阅这里详细讨论该问题.您需要在客户端上创建/更改配置文件以覆盖默认设置.

powershell.exe 所在的目录中编辑文件 powershell.exe.config .如果文件不存在,则创建它.确保它至少包含以下几行:

 <?xml version ="1.0" encoding ="utf-8"吗?<配置>< system.net>< mailSettings>< smtp deliveryMethod ="network">< network clientDomain ="mail.example.com"/></smtp></mailSettings></system.net></configuration> 

用客户的FQDN替换 mail.example.com .

对于.NET Framework 2.0,您首先需要应用补丁(或升级到.NET Framework 4.0或更高版本),然后才能配置 clientDomain 属性.

有关SMTP客户端网络设置的详细信息,请参见此处./p>

如果您从一开始就发布了代码和错误消息,我们将能够更早地提供此答案.

I am using the .Net class System.Net.Mail.SmtpClient in Powershell to send a mail with some attached documents. It all works great in our test environment, but fails in production.

The problem is, that a fully specified domain name is required in production to send the mails. However the SmtpClient does not allow me to specify this, as far as I know. I've read online, that you can use web.config settings with asp.net to specify the domain name, but I don't seem to be able to use it with Powershell.

Is there a way around this or do I have to implement it in another language?

Edit: This is the code I'm using

$SmtpClient = New-Object System.Net.Mail.SmtpClient "mail.mailserver.com", 25
$Mail = New-Object System.Net.Mail.MailMessage

# Custom (working) Cmdlet for creating a NetworkCredential object
$SmtpClient.Credentials = (GetCredential-FromFile)

$Mail.Subject = "New Import"
$Mail.Body = ""
$Mail.From = "myaddress@mailserver.com"
$Mail.To.Add("myaddress@mailserver.com")
$Mail.Attachments.Add(New-Object System.Net.Mail.Attachment "file.txt")

$SmtpClient.Send($Mail)

The SMTP-Server is set, but I need to set the FQDN of the client (the machine using the script).

解决方案

This is a bug (or missing feature, depending on how you want to look at it) in the .NET Framework implementation. By default the NetBIOS name of the host is used in the HELO command, and the SmtpClient class doesn't have a property for configuring the HELO hostname in the program/script. See here for a detailed discussion of the problem. You need to create/change a configuration file on the client to override the default.

Edit the file powershell.exe.config in the same directory where powershell.exe is located. Create the file if it doesn't exist. Make sure it contains at least the following lines:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="network">
        <network clientDomain="mail.example.com" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

Replace mail.example.com with your client's FQDN.

For .NET Framework 2.0 you first need to apply a patch (or upgrade to .NET Framework 4.0 or newer) before you can configure the clientDomain attribute.

For more information on SMTP client network settings see here.

Had you posted your code and error message right from the start we would've been able to provide this answer much earlier.

这篇关于在Powershell中使用SmtpClient进行FQDN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆