Click here to Skip to main content
13,139,254 members (80,854 online)
Rate this:
Please Sign up or sign in to vote.
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 = ""
Const strSender = "Nobody <>"
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 ("") = cdoSendUsingPort
objEmail.Configuration.Fields.Item ("") = "" 
objEmail.Configuration.Fields.Item ("") = 25
objEmail.Configuration.Fields.Item ("") = cdoNTLM

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.
      <smtp deliveryMethod="network">
        <network host="" port="25" defaultCredentials="true" />

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:
[__i__]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");
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.

Posted 13-Nov-12 0:58am
Updated 13-Nov-12 1:33am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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("");
    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);
        // No recepients error

I hope this code will be helpful.
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:
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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="">
   <network host="myserver" port="25" defaultCredentials="false" userName="[Domain\username]" password="[password]"/>
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

visit here....
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 29 Apr 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100