2

i'm having a little issue with my sendmail script. My mail can't be delivered at one of my internal distribution lists. I'm getting the returned message

RESOLVER.RST.AuthRequired; Authentication required

my c# code looks like this:

public void SendMail(int supplierValue, List<Location> locationList, string meldingsNr, string date, string fromT, string tillT)
    {
    var mailFrom = "mail@mail.com";
    SmtpClient client = new SmtpClient("server ip",25);
    client.UseDefaultCredentials = false;
    NetworkCredential basicAuthInfo = new NetworkCredential("username", "password");
    client.Credentials = basicAuthInfo;
    client.EnableSsl = false;

    // Create some empty vars

    var toList = "";
    var ccList = "";
    var mailSubject = "";
    var mailBody = "";
    List<string> locationListString = new List<string>();
    List<string> locationListString2 = new List<string>();

    // Populate toList with var llString

    ** code removed **

    // switch on supplier Value to fill toList, ccList and mailSubject

    ** code removed **

    // Create mail message

    MailMessage message = new MailMessage();
    MailAddress mailAddress = new MailAddress(mailFrom);
    message.To.Add(toList);
    message.From = mailAddress;
    message.CC.Add(ccList);

    // set subject and encoding

    message.Subject = mailSubject;
    message.SubjectEncoding = Encoding.UTF8;

    // set body and encoding

    message.Body = mailBody;
    message.BodyEncoding = Encoding.UTF8;
    message.IsBodyHtml = true;

    try
    {
        // Send mail 
        client.SendCompleted += (s, e) =>
        {
            client.Dispose();
            message.Dispose();
        };
        client.SendAsync(message, null);

    } catch (SmtpException ex)
    {
        MessageBox.Show(ex.InnerException.Message);

    } catch (System.Exception ex)
    {
       MessageBox.Show(ex.InnerException.Message);
    }
}

I applied this suggestion: How can I make SMTP authenticated in C#

Ensure you set SmtpClient.Credentials after calling SmtpClient.UseDefaultCredentials = false. The order is important as setting SmtpClient.UseDefaultCredentials = false will reset SmtpClient.Credentials to null.

but this didn't work. Is there anything else wrong with my code ?

Community
  • 1
  • 1
Bjorn Morrhaye
  • 687
  • 9
  • 30
  • if you just boil it down to creating "client", sending a body of "hello" and using Send, not SendAsync do you still get the same? – BugFinder Apr 28 '17 at 07:48
  • problem is cannot test this in production. What influence could sending async or a simple html body have on smtp authentication. – Bjorn Morrhaye Apr 28 '17 at 09:40
  • It gives you better diagnostics.. You dont have to test the whole app in production with a dummy email - a simple free exe on its own to send 1 mail should be a decent test – BugFinder Apr 28 '17 at 10:21
  • but it has to be to a distribution list that requires authentication :) I can't use that gives the error. All other email-adresses used in my to and cc fields are receiving the mail, only the one that requires authentication doesn't. – Bjorn Morrhaye Apr 28 '17 at 11:32
  • So, the FROM address that is being used is also one of the TO addresses? – Mad Myche Apr 28 '17 at 11:41
  • one of the cc addresses – Bjorn Morrhaye Apr 28 '17 at 11:43
  • Anyone ? i tried sending with: client.send(message) so without SendAsync and still same issue – Bjorn Morrhaye May 02 '17 at 14:33
  • the username and password that you use in credentials are from a Windows account? I had the same problem and it was because I was using DOMAIN\userName as the username. If you need to specify domain, username and password you need to specify then separately in your NetworkCredentials – Jose Antonio Oct 27 '17 at 07:41
  • Yes windows account. Wow, i'll try this later and will let you know the outcome. thx – Bjorn Morrhaye Oct 27 '17 at 12:20

1 Answers1

0

You can use this as a better alternative for your email sending app:

    Let us suppose you are using OutLook to send the emails:
    SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com", 587); // 587 is 
    the port number for outlook

    smtpClient.Credentials = new 
    System.Net.NetworkCredential("yourActualEmailIDhere", "yourActualPassword");
    smtpClient.UseDefaultCredentials = true;
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.EnableSsl = true;
    MailMessage mail = new MailMessage();

    //Setting From , To and CC
    mail.From = new MailAddress("yourActualEmailIDhere");
    mail.To.Add(new MailAddress("destinationEmailAddress"));       // for 'To' you 
    don't need any password credentials. So relax.

   smtpClient.Send(mail);  // finally send the email

Hope this helps.

Rohan Rao
  • 2,505
  • 3
  • 19
  • 39