Solved

Sending Automated Emails asynchronously using a C# Windows Service

Posted on 2013-06-10
6
1,419 Views
Last Modified: 2013-06-11
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

0
Comment
Question by:forsters
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39234838
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.
0
 

Author Comment

by:forsters
ID: 39234906
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...?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39234924
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?
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:forsters
ID: 39234959
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
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39235144
Hmmm...  Perhaps it's my naiveté, but that code doesn't look like it will give you a meaningful indication as to whether or not the mail succeeded. In SendEmailAsync, the BeginInvoke call will return immediately (i.e. it will not block), so as long as nothing raised an exception prior to that line, then the SendEmailAsync should always return true. I would try modifying the SendEmail (since that's what SendEmailAsync itself calls) to add in some logging--see if there are any exceptions being raised.
0
 

Author Comment

by:forsters
ID: 39235280
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.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp web application 3 55
Create a Dropdown list with sub group 10 42
array not updating 8 41
c# ftp code 3 56
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question