Send Email Async - c#

This is what I've tried for sending email using the SendAsync() method. When passing the bool to send regular email it works fine. When sending with the SendAsync method no dice. Just looking for some tips if you see something wrong here. Thanks in advance.
private static void SendEmail(System.Net.Mail.MailMessage m, Boolean Async)
{
using (var smtpClient = new System.Net.Mail.SmtpClient(EmailList.SMTP_GOOGLE, 587))
{
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("email#domain.com","password");
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Timeout = 3000000;
if (Async)
{
object userState = m;
smtpClient.SendCompleted += new SendCompletedEventHandler(Smtp_OnComplete);
try
{
smtpClient.SendAsync(m, userState);
}
catch (Exception ex)
{
//Logging
}
}
else
{
try
{
smtpClient.Send(m);
}
catch (Exception ex)
//Logging
}
}
}
}

Your using statement is disposing the SmtpClient before the asynchronous send finishes.
That won't work.
Instead, you can either use C# 5 await to wait for the async send to finish inside the using statement, or get rid of using entirely for async sends and dispose the SmtpClient in the completion event.

I just set up a simple console app to run the methods for testing
Because the email is being sent asynchronously, the console app will start the method to in a different thread and continue with its own execution. If it closes before the method to send the actual email completes, the email will fail to send because Visual Studio will kill that thread. Try adding the following line after your call to send the email to make the console app wait a few seconds:
System.Threading.Thread.Sleep(5000);
This should be more than long enough for the email method to send the email and finish before the console app closes and Visual Studio kills all processes. The code should work fine on a web server.

Related

Send SMTP email getting forcibly closed in console application before it can send email in .NET

I have an email notifier application that sends emails out and we do so in a console application that runs every 5 minutes. We run the email part in a task so it can continue onto another group of notifications.
However, we we run one notification, the console closes and the email never gets sent. On the SMTP side it says the host was forcibly closed. How can I keep the console application alive until all tasks are finished, but still be able to multi thread.
The read operation failed. Bytes transferred: 0 Remote IP:
44.444.444.44, Session: 124992, Code: 10054, Message: An existing connection was forcibly closed by the remote host
private Task SendFromServer(MailMessage mailMessage, bool reuse, bool useServerSmtp)
{
return Task.Factory.StartNew(() =>
{
var smtp = new SmtpClient();
smtp.Send(mailMessage);
}
catch (Exception ex)
{
Logger.Error(ex.InnerException ?? ex);
}
finally
{
if(!reuse)
mailMessage.Dispose();
}
});
}
}
Use the SmtpClient.SendMailAsync which can be awaited.
private async Task SendFromServer(MailMessage mailMessage) {
using (var smtp = new SmtpClient()) {
try {
await smtp.SendMailAsync(mailMessage);
} catch (Exception ex) {
Logger.Error(ex.InnerException ?? ex);
}
}
}
and since it is being called in a console application you would need to call it like
//get all notification tasks. Assuming notifications => List<MailMessage>
var tasks = notifications.Select(message => SendFromServer(message));
//execute all asynchronously
Task.WhenAll(tasks).GetAwaiter().GetResult();
so the console application waits for all of them to complete their tasks

How to understand C# SendAsync function?

When i program with C# to send a mail by batch,my code is like this:
public static bool Send(MailAddress Messagefrom,
string MessageTo,
string MessageSubject,
string MessageBody)
{
MailMessage message = new MailMessage();
message.From = Messagefrom;
message.To.Add(MessageTo);
message.Subject = MessageSubject;
message.Body = MessageBody;
message.BodyEncoding = System.Text.Encoding.UTF8;
//message.SubjectEncoding = Encoding.BigEndianUnicode;
message.IsBodyHtml = true;
message.Priority = MailPriority.High;
MailHelper mh = new MailHelper();
SmtpClient sc = mh.setSmtpClient("smtp.qq.com", 25);
sc.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
try
{
sc.SendAsync(message, message);
}
catch (Exception e)
{
LogHelper.WriteLog("Main send failed....\t the detail info:" +
e.ToString());
return false;
}
return true;
}
This is a problem!When the first mail send failed(for example the mail address is null),the next mail can't be send!
Because i have so much mail wait to send,if this situation,how to fix it?For example the failed mail may still on this table and Administator to deal it by hand.
But this situation probably in Send function,Why this happen?
You have to catch errors in the foreach loop that calls your Send() function and log the errors somewhere:
foreach (var mail in mailsToSend)
{
try
{
// Call your send function
Send(...)
}
catch (Exception ex)
{
// Log the error somewhere (console, file, ...)
Console.WriteLine("Error sending mail {0}", mail);
}
}
This ensures that the application won't crash when one email fails to send and continue sending the other mails.
Also you should use Send() instead of SendAsync() in your Send() function. This is because the SendAsync function starts a new thread for sending the mail, while Send will stop your programs execution until the mail has been sent. One more reason you shouldn't use the SendAsync function is because according to microsoft only 1 mail can be send at a time. That means using the SendAsync function for more then 1 mail will cause it to throw an exception.

Web Service responds to caller while still processing thread

I am creating a web service
Inside the web service, I do some processing, which is very fast, I send 2 to 3 emails asynchronously using SmtpClient.SendAsync().
My problem is that even though they are being sent asynchronously, I have to wait for them to finish processing before ending the service and sending back a response to the user. If I don't wait for the SendCompletedEventHandler to fire, the email is never sent. Sometimes the mail server takes some time to respond.
The user doesn't really need to know if the emails were sent or not. It would be nice to just send the emails and let them process somewhere else and respond to the user as fast as I can.
Would anybody have a good solution for this? Maybe I'm wording my searches wrong but I'm not coming up with any solutions.
You could fire up a new thread to do the sending:
ThreadPool.QueueUserWorkItem(delegate {
SmtpClient client = new SmtpClient();
// Set up the message here
using (MailMessage msg = new MailMessage()) {
client.Send(msg);
}
});
Here is a full example that I have tested and works with WCF. Control is returned immediately to the client, server starts sending, sleeps to simulate delay, then finishes. Just add a reference to System.ServiceModel to get the necessary classes.
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(HelloWorldService), new Uri("http://localhost:3264"));
ServiceMetadataBehavior mdb = new ServiceMetadataBehavior();
mdb.HttpGetEnabled = true;
host.Description.Behaviors.Add(mdb);
host.Open();
Console.WriteLine("Service Hosting...");
Console.ReadLine();
}
}
[ServiceContract]
class HelloWorldService
{
[OperationContract]
public void SendEmails()
{
Task.Factory.StartNew(() =>
{
Console.WriteLine("Start Sending Emails...");
Thread.Sleep(10000);
Console.WriteLine("Finish Sending Emails...");
});
}
}
}

Sending mail periodically in ASP.net

I'm trying to send confirmation mails to users periodically in ASP.NET.
To do this I polulate a queue with mails and check it every 30 seconds. Any confirmation emails in the queue at this time are sent and then cleared from the queue.
Does anyone know how to do this?
Here is my sending mail code
public static bool SendMail(string AdminMail,string AdminPassword,string subject,string toAddress, string content,DateTime SendTime)
{
toAddressListProperty.Enqueue(toAddress);
if(date==null)
{
date = DateTime.Now.Second;
}
if (date-SendTime.Second > 120)
{
var message = new MailMessage
{
From = new MailAddress(AdminMail)
};
foreach (var toAddressl in toAddressListProperty)
{
message.To.Add(new MailAddress(toAddressl));
}
message.Subject = subject;
message.Body = content;
message.IsBodyHtml = true;
var smtp = new SmtpClient
{
Credentials = new System.Net.NetworkCredential(AdminMail, AdminPassword),
Port = 587,
Host = "smtp.gmail.com",
EnableSsl = true
};
smtp.Send(message);
//date = SendTime;
return true;
}
return false;
}
I have done this using a background thread. I did a little research, and I believe this is an ok approach. There are a few dangers, which this blog details.
The main thing is to ensure you never throw an exception from a background thread, as I believe that will cause the web process to restart. Also, incase the thread dies, I ensure it is running on every call.
I have been using this approach for a few months, and so far no issues.
Also I run it every 1 second, this minamizes the amount of time you might loose emails due to an app shutdown.
public class BackgroundSmtpService
{
private ILog _log = LogManager.GetLogger(typeof(BackgroundSmtpService));
private readonly SmtpService SmtpService;
private static Thread _watchThread;
private static List<Email> _emailToSend = new List<Email>();
public BackgroundSmtpService(SmtpService smtpService)
{
SmtpService = smtpService;
}
public void Send(Email email)
{
lock (_emailToSend)
{
_emailToSend.Add(email);
}
EnsureRunning();
}
private void EnsureRunning()
{
if (_watchThread == null || !_watchThread.IsAlive)
{
lock (SmtpService)
{
if (_watchThread == null || !_watchThread.IsAlive)
{
_watchThread = new Thread(ThreadStart);
_watchThread.Start();
}
}
}
}
private void ThreadStart()
{
try
{
while (true)
{
Thread.Sleep(1000);
try
{
lock (_emailToSend)
{
var emails = _emailToSend;
_emailToSend = new List<Email>();
emails.AsParallel().ForAll(a=>SmtpService.Send(a));
}
}
catch (Exception e)
{
_log.Error("Error during running send emails", e);
}
}
}
catch (Exception e)
{
_log.Error("Error during running send emails, outer", e);
}
}
}
You might want to consider using Quartz.net library. It have decent documentation and it's fairly easy to use.
The biggest challenge you'll have with this is that any time your application pool recycles it will take a new request to kick stats your "timer". If you had an HTTP monitor application such as Pingdom to poll your server it shouldn't be a problem, but then again you could also just use a third party monitor tool to hit your a page on your site every N seconds that would send out the mail and issue a response.
I myself would use a Windows service to pull a queue from a database and send out messages that way.
Easiest way is to create a VBScript that sends an HTTP GET request to http://localhost/SendConfirmationEmails.aspx
You'd start the VBScript in your global.asax Application_Start method.
The SendConfirmationEmails.aspx would act as a simple web service (you could use an ashx, or actual web service asmx if you wanted). It would only be accessible on the localhost so remote users wouldn't be able to spam it.
Using a windows service is probably the best practice method, but a simple VBScript will get the job done.
surl="http://localhost/SendConfirmationEmails.aspx"
set oxmlhttp=createobject("msxml2.xmlhttp")
with oxmlhttp
.open "GET",surl,false
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send srequest
end with
You'd put the code above in a while wend loop with a Sleep to delay every 30 seconds...

Invalid Operation Exception In Calling Async Method from inside a Parallel.ForEach Loop

I have inherited a windows service that processes a large number of e-mails in a queue. Sounds simple, Grab queue, send e-mail, if SmtpClient.SendAsync does not return an error from the call back then flag the e-mail in the DB as being sent.. I am using a Semaphore to waitone on the thread so multiple calls can be made to the Async Send method of the SMTP Client. This is the only way I can get the status and per Microsoft docs it has to finish the operation before another call can be made async. So now for the fun part. I decided to use a Parallel.ForEach to get he queue like so. This method is called in the Windows Service OnStart. Please note I have tried calling this method on a separate Thread and get the same results.
I am thinking that either A, I am missing something obvious, due to my lack of knowledge on threading, or something is flat bugged. Most likely A.
private static void ProcessEmailQueue()
{
List<EmailQueue> emailQueue =
_repository.Select<EmailQueue>().Where(x => x.EmailStatuses.EmailStatus == "Pending").ToList();
Parallel.ForEach(emailQueue, message =>
{
_smtpMail.FromAddress = message.FromAddress;
_smtpMail.ToAddress = message.ToAddress;
_smtpMail.Subject = message.Subject;
_smtpMail.SendAsHtml = message.IsHtml > 0;
_smtpMail.MessageBody = message.MessageBody;
_smtpMail.UserToken = message.EmailQueueID;
bool sendStatus = _smtpMail.SendMessage();
// THIS BLOWS UP with InvalidOperation Exception
});
}
Here is the SMTP Method being called from withing the loop.
public bool SendMessage()
{
mailSendSemaphore = new Semaphore(0, 10); // This is defined as private static Semaphore mailSendSemaphore;
try
{
var fromAddress = new MailAddress(FromAddress);
var toAddress = new MailAddress(ToAddress);
using (var mailMessage = new MailMessage(fromAddress, toAddress))
{
mailMessage.Subject = Subject;
mailMessage.IsBodyHtml = SendAsHtml;
mailMessage.Body = MessageBody;
Envelope = mailMessage;
smtp.SendCompleted += smtp_SendCompleted;
smtp.SendAsync(mailMessage, UserToken);
mailSendSemaphore.WaitOne();
return _mailSent;
}
}
catch (Exception exception)
{
_logger.Error(exception);
return _mailSent;
}
}
CALLBACK For Smtp Send
private void smtp_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
}
if (e.Error != null)
{
}
else
{
_mailSent = true;
}
mailSendSemaphore.Release(2);
}
Here is the Exception, took a few to get it for some odd reason.
System.InvalidOperationException was unhandled by user code
Message=An asynchronous call is already in progress. It must be completed or canceled before you can call this method.
Source=System
StackTrace:
at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)
at DFW.Infrastructure.Communications.SmtpMail.SendMessage() in SmtpMail.cs:line 71
at EmaiProcessorService.EmailQueueService.b_0(EmailQueue message) in Service1.cs:line 57
at System.Threading.Tasks.Parallel.<>c_DisplayClass2d2.<ForEachWorker>b__23(Int32 i)
at System.Threading.Tasks.Parallel.<>c__DisplayClassf1.b__c()
InnerException:
Seems my waitone is getting obliterated by System.Threading.Tasks.Parallel
Okay, now that we've got the error text, it seems fairly clear:
Message=An asynchronous call is already in progress. It must be completed or canceled before you can call this method.
This concurs with the documentation:
Two simple options:
Create a fixed number of clients, and a queue of messages to send. Make each client take a message from the queue each time it finishes, until the queue is empty. BlockingCollection<T> is good for this.
Create a new SmtpClient per message. This could cause you to effectively launch a DOS attack on your SMTP server, which isn't ideal.
To be honest, it's not really clear why you're using SendAsync when you're then just waiting for the message to be sent anyway...
I'm not clear on why you're using a Semaphore here, but you're almost certainly using it incorrectly. You're creating a new semaphore instance for each call to SendMessage. Also, you're calling WaitOne on it once, and then calling Release(2), so eventually you'll have more releases than acquires. That's probably what causes your InvalidOperationException.
It doesn't do you any good to parallelize processing of the email queue, since you can only send one message at a time. And trying to do it asynchronously inside of the Parallel.Foreach is just more needless complication.
You're better off using something like ThreadPool.QueueUserWorkItem, and having a simple loop that sends one message at a time.
List<EmailQueue> emailQueue =
_repository.Select<EmailQueue>().Where(x => x.EmailStatuses.EmailStatus == "Pending").ToList();
ThreadPool.QueueUserWorkItem(ProcessEmailQueue, emailQueue);
void ProcessEmailQueue(object state)
{
List<EmailQueue> emailQueue = (List<EmailQueue>)state;
foreach (var message in EmailQueue)
{
// Format and send message here.
}
}
Alternatively, you can do the same thing with a Task. The point is that you just need a single thread to process the queue sequentially. Since you can't send more than one message at a time, Parallel.ForEach doesn't do you any good.
EDIT:
If you need to do multiple sends at a time, you can probably modify your original code. First, initialize the semaphore at class scope:
private static Semaphore mailSendSemaphore = new Semaphore(10, 10);
Then, in your SendMessage method:
bool SendMessage()
{
// acquire semaphore. This will block until there's a slot available.
mailSendSemaphore.WaitOne();
try
{
// do all your processing here, including sending the message.
// use Send rather than SendAsync
}
finally
{
mailSendSemaphore.Release();
}
}
There's no need to use SendAsync.

Categories

Resources