We help IT Professionals succeed at work.
Get Started

Sending Email - Urgent Help Needed

477 Views
Last Modified: 2012-05-11
Dear experts,

I have written an application which will send out email notifications to my users.

I noticed that sometimes when i trigger a event which will sends out email, my application will load for a very long time to complete but sometimes, it will send almost immediately.

My exchange server is seperate server from my web server, but both of them is within the same network.

I tried to add asynchronous mailing but the random long loading time still persist.

I have also thought about putting the mail into a pickup directory so my web application will not load that long, but i am not sure how to configure it.

Can anyone please kindly advice? Please help....

Thanks.

public static void sendMail(List<User> users, string subject, string body)
        {
            try
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(from);

                for (int i = 0; i < users.Count(); i++)
                {
                    message.To.Add(users[i].Email);
                }

                message.Subject = subject;
                message.Body = body;
                message.IsBodyHtml = true;

                SmtpClient emailClient = new SmtpClient();
                emailClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

                 
                Object userState = message;

                try
                {
                    //Send the email asynchronously
                    emailClient.SendAsync(message, userState);
                }
                catch (SmtpException smtpEx)
                {
                    MailMessage errorMailAlert = new MailMessage();
                    errorMailAlert.From = new MailAddress(from);
                    errorMailAlert.To.Add(new MailAddress("randomise@scri.edu.sg"));

                    errorMailAlert.Subject = "Send Mail Failure";
                    errorMailAlert.Body = "system has failed to a mail. Details of the error is stated below and can be viewed" +
                        " in the MailLog under the rand log folder.<br/><br/>Exception details:<br/>" + smtpEx.ToString();
                    errorMailAlert.IsBodyHtml = true;

                    //try sending message to the admin
                    try
                    {
                        //Send the email asynchronously
                        emailClient.SendAsync(errorMailAlert, userState);
                    }
                    catch (SmtpException smtpEx2)
                    {
                        //Error handling here
                        ExceptionUtility.LogMailException(smtpEx2);
                    }
                    //Error handling here
                    ExceptionUtility.LogMailException(smtpEx);

                }
                catch (Exception ex)
                {
                    //Error handling here
                    ExceptionUtility.LogMailException(ex);
                }
            }
            catch
            {
                throw;
            }
        }

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE