通过Exchange Server发送邮件 [英] Sending mail via Exchange Server

查看:115
本文介绍了通过Exchange Server发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了通过我们的Exchange服务器发送电子邮件的麻烦。



Exchange版本是6.5.7654.12(2003 SP2)。

Auth支持: 250-AUTH GSSAPI NTLM



我有一个专用的域帐户。我在同一个帐户下运行这两个进程,我想使用NTLM。 VBS代码正在运行,.NET 4.0代码不是: 5.7.3客户端无权向此服务器提交邮件。



VBS代码:

  Const  strDefaultEmailAddress =   address@my.com 
Const strSender = 无人< ApplicationPostman-CSV@my.com>
Const cdoNTLM = 2
Const cdoSendUsingPort = 2

设置 objEmail = CreateObject( CDO.Message
objEmail.From = strSender
objEmail。 To = strDefaultEmailAddress
objEmail.Subject = TEST!
objEmail.HTMLBody = < h1> TEST< / h1>
objEmail.Configuration.Fields.Item( < span class =code-string> http://schemas.microsoft.com/cdo/configuration/sendusing)= cdoSendUsingPort
objEmail.Configuration.Fields.Item( http://schemas.microsoft.com/cdo/configuration/smtpserver)= mailserver.my.com
objEmail.Configuration.Fields.Item( http://schemas.microsoft.com/cdo/configuration/smtpserverport)= 25
objEmail.Configuration.Fields.Item( http://schemas.microsoft.com/cdo/configuration / smtpauthenticate )= cdoNTLM
objEmail.Configuration.Fields.Update
objEmail.Send





好​​的,这些字段可以是在System.Web.MailMessage实例中设置,但是这是阻碍,所以我想使用System.Net.MailMessage,但是没有字段可以设置。



我尝试了几件事。

首先:

<前lang =xml> < < span class =code-leadattribute> system.net >
< mailSettings >
< smtp deliveryMethod = network >
< span class =code-keyword>< network host = mailserver.my.com port = 25 defaultCredentials = true / >
< / smtp >
< / mailSettings >
< / system.net >



我尝试将defaultCredentials设置为false,并添加一个NetworkCredential实例,其中包含用户名,密码和指定的域。 />


以及谷歌发现的其他建议,例如:

  MyMail.Credentials = CredentialCache.DefaultNetworkCredentials .GetCredential( new  Uri( string  .Format(  smtp:// {0},MyMail.Host)),  NTLM);  

MyMail.Credentials = CredentialCache.DefaultCredentials.GetCredential( new Uri( string .Format( smtp:// {0},MyMail.Host)), NTLM);

所有提出的相同,除了离子。



c#和.net 4中上述vbs代码的正确等价物是什么?实际上即使存储凭据我也会满意,但我必须使用服务器允许的身份验证模型,更改它们不是一个选项。



提前谢谢。



ZorgoZ

解决方案

很奇怪,因为你似乎已经应用了显然可用的所有变通办法。

我注意到了一个小小的区别:你是否尝试在smtp标签中设置from属性?

 <   smtp     deliverymethod   =  network   来自  =  myaccount@my.website.com  >  
< network host = myserver port = 25 < span class =code-attribute> defaultCredentials = false userName = [Domain\username] password = [密码] / >
< / smtp >


您好,尝试使用System.Net.Mail。 SmtpClient。我使用以下代码

此代码甚至可以在带有域凭据的.NET 2.0中使用。一些参数来自配置文件。

  string  mail_server = Properties.Settings.Default.MailServer; 
int mail_port = Properties.Settings.Default.MailPort;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(mail_server,mail_port);
string smtp_login = my_login;
string smtp_pwd = my_pwd;
smtp.Credentials = new System.Net.NetworkCredential(smtp_login,smtp_pwd);
// 创建邮件
System.Net.Mail.MailMessage消息= new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress( my_login@my_domain.my);
int i = 0 ;
// 从配置文件中获取收件人列表
if (Properties.Settings.Default.Recipients.Count > 0 ){
foreach string mail_addr in Properties.Settings.Default.Recipients){
if (mail_addr.Length > 0 )message.To.Add( new System.Net.Mail.MailAddress(mail_addr));
}
message.Subject = 我的留言;
message.Priority = System.Net.Mail.MailPriority.Normal;
message.Body = 这是邮件正文消息!:);

// 添加附件
string [] attachments = new string [ 2 ] { my_attachment1 my_attachment2};
foreach 字符串附件 in 附件){
System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(附件);
message.Attachments.Add(att);
}
smtp.Send(message);
} 其他 {
// 没有收件人错误
// ...
}



我希望这段代码会有所帮助。


访问这里....

<预lang =c ++>

[ ^ ]


I am experiencing troubles sending email via our Exchange server.

Exchange version is 6.5.7654.12 (2003 SP2).
Auth support: 250-AUTH GSSAPI NTLM

I have a dedicated domain account. I am running both processes under the same account, and I want to use NTLM. The VBS code is working, the .NET 4.0 code is not: 5.7.3 Client does not have permission to submit mail to this server.

VBS code:

Const strDefaultEmailAddress = "address@my.com"
Const strSender = "Nobody <ApplicationPostman-CSV@my.com>"
Const cdoNTLM = 2
Const cdoSendUsingPort = 2

Set objEmail = CreateObject("CDO.Message")
objEmail.From = strSender
objEmail.To = strDefaultEmailAddress
objEmail.Subject = "TEST!" 
objEmail.HTMLBody = "<h1>TEST</h1>"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailserver.my.com" 
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoNTLM
objEmail.Configuration.Fields.Update
objEmail.Send



Ok, these fields can be set in a System.Web.MailMessage instance, but that's obsolote, so I want to use System.Net.MailMessage, but tis one has no fields to be set.

I have tried several things.
First:

<system.net>
    <mailSettings>
      <smtp deliveryMethod="network">
        <network host="mailserver.my.com" port="25" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>


Than I tried to set defaultCredentials to false, and add a NetworkCredential instance with username, password and domain specified.

And other suggestions found with google too, like:

MyMail.Credentials = CredentialCache.DefaultNetworkCredentials.GetCredential(new Uri(string.Format("smtp://{0}",MyMail.Host)), "NTLM");
or
MyMail.Credentials = CredentialCache.DefaultCredentials.GetCredential(new Uri(string.Format("smtp://{0}",MyMail.Host)), "NTLM");

All raised the same exception.

What is the proper equivalent of the above vbs code in c# and .net 4? Actually I would be satisfied even with storing credentials, but I have to use the authentication models allowed by the server, changing them is not an option.

Thank you in advance.

ZorgoZ

解决方案

Strange as you seem to have applied all workarounds apparently available.
I noted a small difference, though: did you try to set the "from" attribute in "smtp" tag?

<smtp deliverymethod="network" from="myaccount@my.website.com">
   <network host="myserver" port="25" defaultCredentials="false" userName="[Domain\username]" password="[password]"/>
</smtp>


Hi, try to use System.Net.Mail.SmtpClient. I used the following code
This code works even in .NET 2.0 with domain credentials. Some parameters has been taken from configuration file.

string mail_server = Properties.Settings.Default.MailServer;
int mail_port = Properties.Settings.Default.MailPort;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(mail_server, mail_port);
    string smtp_login = "my_login";
    string smtp_pwd = "my_pwd";
    smtp.Credentials = new System.Net.NetworkCredential(smtp_login, smtp_pwd);
    //Creating a mail message
    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    message.From = new System.Net.Mail.MailAddress("my_login@my_domain.my");
    int i = 0;
    // Getting recipient list from configuration file
    if( Properties.Settings.Default.Recipients.Count > 0 ){
        foreach( string mail_addr in Properties.Settings.Default.Recipients ){
            if (mail_addr.Length > 0) message.To.Add(new System.Net.Mail.MailAddress(mail_addr));
        }
        message.Subject = "My message";
        message.Priority = System.Net.Mail.MailPriority.Normal;
        message.Body = "This is a mail body message! :)";

        // Adding attachments
        string[] attachments = new string[2]{"my_attachment1", "my_attachment2"};
        foreach (string attachment in attachments){
            System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(attachment);
            message.Attachments.Add(att);
        }
        smtp.Send(message);
    }else{
        // No recepients error
        //...
    }


I hope this code will be helpful.


visit here....

[^]


这篇关于通过Exchange Server发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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