Link to home
Start Free TrialLog in
Avatar of forsters
forstersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Sending Automated Emails asynchronously using a C# Windows Service

Hi Experts,

I've been following the linked tutorial which provides instruction on how to set up a service to send emails asynchronously using names, email-addresses & dates stored in a database.
I have built the solution and installed the service on my local machine, the only significant difference between my project and the authors is that I want to send my mail through our exchange server whereas the author has used a gmail account.

If We look at the main email Method (below) I am seeing the Message Sent SUCCESS message in the event log for the service and it has correctly picked up my email address and name from the database as 'CustomerEmail' and 'CustomerName', So I presume that I am 99% there, what I don't get is the actual physical email arrive in my Inbox.....can anyone suggest where I might begin troubleshooting?

Someone else seemed to have similar trouble following the tutorial and the author suggested an SMTP problem and to check ports and settings...however all that is set in the project itself is a sever name and credentials - in our case we use these credentials to send out batch html emails via a console app so I am fairly confident that these are correct.

I am wondering if the fact that I am using a Windows service to send the email is seen as a security threat by our exchange server or something...but I've never done this before so really haven't a clue...

private void ServiceEmailMethod()
        {
            eventLogEmail.WriteEntry("In Sending Email Method");

            EmailComponent.GetEmailIdsFromDB getEmails = new EmailComponent.GetEmailIdsFromDB();
            getEmails.connectionString = ConfigurationManager.ConnectionStrings["CustomerDBConnectionString"].ConnectionString;
            getEmails.storedProcName = "GetBirthdayBuddiesEmails";
            System.Data.DataSet ds = getEmails.GetMailIds();

            EmailComponent.Email email = new EmailComponent.Email();

            email.fromEmail = "example@gmail.com";
            email.fromName = "example Name";
            email.subject = "Birthday Wishes";
            email.smtpServer = "smtp.gmail.com";
            email.smtpCredentials = new System.Net.NetworkCredential("example@gmail.com", "example password");

            foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
            {
                email.messageBody = "<h4>Hello " + dr["CustomerName"].ToString() + "</h4><br/><h3>We Wish you a very Happy" +
                                    "Birthday  to You!!! Have a bash...</h3><br/><h4>Thank you.</h4>";

                bool result = email.SendEmailAsync(dr["CustomerEmail"].ToString(), dr["CustomerName"].ToString());

                if (result == true)
                {
                    eventLogEmail.WriteEntry("Message Sent SUCCESS to - " + dr["CustomerEmail"].ToString() +
                                             " - " + dr["CustomerName"].ToString());
                }
                else
                {
                    eventLogEmail.WriteEntry("Message Sent FAILED to - " + dr["CustomerEmail"].ToString() +
                                             " - " + dr["CustomerName"].ToString());
                }

            }
        }

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

I am wondering if the fact that I am using a Windows service to send the email is seen as a security threat by our exchange server or something
Exchange has no idea what application sent the mail--it's just data received on some port so far as Exchange is concerned. Do you have any antivirus running on the windows service machine? AVs will often block traffic on port 25 in order to prevent machines from being turned into spam bots.
Avatar of forsters

ASKER

Hi Kaufmed,

Many thanks for your comment, we have just tested this theory by sending a mail from the service machine via telnet, which we are able to do without error, presumably that means port 25 is not being blocked...?
presumably that means port 25 is not being blocked...?
That should be the case.

Where did the class EmailComponent.Email come from? Did you write that in-house, or is it a 3rd party library?
It's part of a class library that you have to build in PART I of the tutorial - you will see the instructions if you look about halfway down this link of the tutorial
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have isolated the problem to the class, it looks to me like the toEmail and toName are not actually being assigned, if I ignore the Email.cs from the class library and set my toEmail direct, then I seem to be in business.

Many thanks for your help, you narrowed down my search.