0

I have this code I wrote:

public static void sendemail(string strMsgBody, string strMsgSubject, ref List<queryResults> offenders)
    {
        MailMessage mail = new MailMessage(fromEmailAddr@mycompany.com, myEmailAddress@mycompany.com);
        SmtpClient client = new SmtpClient();
        client.Port = 25;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Host = "hostname";
        mail.Subject = "My Subject Line";
        int intDashCounter = 0;
        string dashMailBody = strMsgSubject + Environment.NewLine;
        foreach (queryResults offender in offenders)
        {
            if (offender.strType == strMsgBody && offender.blnReported == false)
            {
                dashMailBody = dashMailBody + offender.strUniqueID + Environment.NewLine;
                offenders[intDashCounter].blnReported = true;
            }
            intDashCounter++;
        }
        mail.Body = dashMailBody;
        client.Send(mail);
    }

It was working at one point in time but now it does not work. For the life of me I can't track down what I changed to make it so that it doesn't work. Sorry for this one, I know its probably some stupid little thing but can someone give me a nudge in the right direction with this code?

  • What does "does not work" mean? – Paul Abbott Feb 05 '18 at 21:18
  • sorry for leaving that out, it just doesn't do anything. The program goes on but it doesn't send out an email. –  Feb 05 '18 at 21:19
  • Do you not need some credentials somewhere? You have use default turned off, but do not define any to use. – user7396598 Feb 05 '18 at 21:21
  • 1
    The only difference between what you have and what I have in my working code is a USING block around my client, and I never set useDefaultCredentials. If the default for useDefaultCredentials is true then it must be credentials. Is this in a try/catch at some point? I get an exception when I run mine because my local environment isn't allowed to send through our email relay, but my staging and production environments work. But you do not mention getting an exception. – user7396598 Feb 05 '18 at 21:29
  • I'm guessing port 25 has been blocked. That's pretty common these days due to spam problems. (I also don't see how your `new MailMessage` could work with the addresses specified that way -- is this your real code?) – McGuireV10 Feb 05 '18 at 22:38

1 Answers1

0

So, I figured it out but the solution I found doesn't make logical sense. I shortened the value being passed in to the variable strMsgSubject and for some reason the method now sends out emails.