2

No answer found even after searching many sites

i am using VS2010(Frame work 4.0) and SQL 2012 , we are using exchange server....same mail configuration is working in java application but not working in c#

button click code is :

    try
    {         
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "mail.myorganization.com"
        smtp.Port = "587";
        smtp.Credentials = new System.Net.NetworkCredential("abc@myorganization.com", "password");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;        
            smtp.EnableSsl = true;
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("abc@myorganization.com");                  
        msg.To.Add(new MailAddress("receiver@something.com"));         
        msg.Subject = "Test";
        msg.Body = "Test mail";
        smtp.Timeout = 60000;
        smtp.Send(msg);
        result = true;
    }
    catch (Exception ex)
    {

    }

My exception error is

System.Net.Mail.SmtpException was caught
  HResult=-2146233088
  Message=Authentication failed.
  Source=System
  StackTrace:
       at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
       at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
       at System.Net.Mail.SmtpClient.GetConnection()
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at MailConfiguration.TestMail() in
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Kiran
  • 41
  • 1
  • 6
  • can you do telnet mail.myorganization.com 587 and see what is the result – Shetty Oct 12 '15 at 10:08
  • could you please suggest how to do it ?? @Shetty – Kiran Oct 12 '15 at 10:38
  • in command prompt type "telnet mail.myorganization.com 587". With correct name of the server – Shetty Oct 12 '15 at 11:21
  • i did, it says -- 'telnet' is not a recognized as a internal or external command, operable program or batch file – Kiran Oct 12 '15 at 11:40
  • Control Panel>Programs>Turn Windows features on or off. Then, check "Telnet Client" and save the changes. You might have to wait about a few minutes before the change could take effect. – Shetty Oct 12 '15 at 11:42
  • thankyou shetty. i did above steps. next ? – Kiran Oct 12 '15 at 12:09
  • type "telnet mail.myorganization.com 587" and tell me what is the response. here mail.myorganization.com is the smtp server name. And I am assuming you are absolutely surre your user name password is correct. – Shetty Oct 12 '15 at 12:11
  • 220 mail1.myorganization.com ESMTP Postfix – Kiran Oct 12 '15 at 12:22
  • can you use smtpClient.UseDefaultCredentials = false; before the line "smtp.Credentials" and try. – Shetty Oct 12 '15 at 12:34
  • Thanks for your reply. smtp.UseDefaultCredentials = false; i used this before credentials, but no use, mail sending failed. – Kiran Oct 12 '15 at 12:37
  • Are you still getting the erros meeage as Authentication Failed? Can you confirm again with your system admin if the credentials have changed? You can also try by changing smtp server name as "smtp.gmail.com", port 587 and username password as ur gmail username password. Just to test your code. http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp – Shetty Oct 12 '15 at 12:40
  • By using gmail and yahoo mail, it is working good. we configured same credentials in java service application , it's working good........client is using exchange server – Kiran Oct 12 '15 at 12:43
  • then possibly the username password has changed :) – Shetty Oct 12 '15 at 12:46
  • but it is working on java service with same credentials (just tested).......thanks in advance. – Kiran Oct 12 '15 at 12:54
  • host\port\credentials\ssl settings all are correct. I used same credentials in java application. It is working in Java App, but failed in failed in .Net App. Any help ?? – Kiran Nov 18 '15 at 13:04

1 Answers1

0

I had a very similar problem. Try to use wireshark to understand what is going wrong. In my case I solved the problem using System.Web.Mail. I know it is deprecated but your siituation is similar to mine, I think there no other possibility to solve the problem.

my solution

System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
    msg.Body = message.Body;

    string smtpServer = "mail.business.it";
    string userName = "username";
    string password = "password";
    int cdoBasic = 1;
    int cdoSendUsingPort = 2;
    if (userName.Length > 0)
    {
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
    }
    msg.To = message.Destination;
    msg.From = "me@domain.it";
    msg.Subject = message.Subject;
    msg.BodyFormat = MailFormat.Html;//System.Text.Encoding.UTF8;
    SmtpMail.SmtpServer = smtpServer;
    SmtpMail.Send(msg);
Community
  • 1
  • 1
silviagreen
  • 1,679
  • 1
  • 18
  • 39