Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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:
VB
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:
XML
<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:
C#
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
Posted
Updated 13-Nov-12 1:33am
v2

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?
XML
<smtp deliverymethod="network" from="myaccount@my.website.com">
   <network host="myserver" port="25" defaultCredentials="false" userName="[Domain\username]" password="[password]"/>
</smtp>
 
Share this answer
 
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.
C#
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.
 
Share this answer
 
v4
Comments
Zoltán Zörgő 14-Nov-12 11:37am    
I will check your code, but I am currently struggling with System.Net.Mail.SmtpClient, if that was not clear. And my code looks very much alike yours, except that it is configured from app.config, not code. The problem is, that the SmtpClient class tries to answer to GSSAPI not to NTLM, and I can not force it to do so. I have checked the traffic with NetworkMonitor too, and I have the feeling, that the the code in the framework has some flows.
skydger 14-Nov-12 14:49pm    
This is strange...
I found the similar issue with this here: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/194ddefd-2d21-4b34-b630-96852ae7d03d/
But the subject is Exchange 2007, not 2003... Bit there's no a word about Service Pack 2.
Zoltán Zörgő 16-Nov-12 4:23am    
Yes, I have seen that forum thread, and there is a comment confirming my situation. But that's a comment from year 2007, and we are in 2012! If I would have Exchenge 2007, I would use exchange services and not smtp.
visit here....
C++

[^]
 
Share this answer
 
Comments
Zoltán Zörgő 9-Nov-13 7:04am    
I made my visit there, but nothing useful. Please note the constraints I have mentioned in my post, especially the Exchange version and authentication method, which I can not change.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900