Logging errors when sending ASync email with MvcMailer - asp.net-mvc-3

I'm using the excellent MvcMailer package to send email from within my application.
I'm using the SendAsync() method to send email, and would like to log errors + dispose of attachments i.e.
MailMessage message = UserMailer.SendSomeEmail(emailViewModel);
var client = new SmtpClientWrapper();
client.SendCompleted += (sender, e) =>
{
if (e.Error != null || e.Cancelled)
{
Logger.LogError(e.Error);
}
if (message != null)
{
message.Attachments.Dispose();
message.Dispose();
}
client.Dispose();
};
message.SendAsync("this means nothing afaik", client);
This works great, but it would get too painful to repeat the same snippet all over wherver I need to send email.
How should I set this up so that I can log any errors + dispose of message attachments when the async call is completed? There has to be a better way!

If what you're trying to do is to avoid having to write that logging and cleanup code every time you send an async email, the answer is simple -- stop using anonymous methods. Just take your current code and put it in a regular method like this:
public void AsyncSendCompleted(object sender, EventArgs e)
{
// Use an appropriate type instead of EventArgs so you can get
// stuff like e.Cancelled and e.Error
// The rest of your code goes here
}
Now use this method as the event handler for your SendCompleted event:
client.SendCompleted += AsyncSendCompleted;

Related

Synchronous Call in Windows Phone 7

I know that I cannot make a true synchronous call in Windows Phone 7. However, I'm trying to at least block the threads from processing until the async call is made or there is a timeout. I've tried the following, but it seems like my app just ignores it, abandons the call, and doesn't return back. Any ideas why?
I'm trying to update a value using a Value Converter during binding.
public ManualResetEvent _event;
public void GetSync()
{
_event = new ManualResetEvent(false);
var wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(ReadCompleted);
wc.OpenReadAsync(new Uri("My URL"));
// block until async call is complete
_event.WaitOne(5000);
}
private void ReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var serializer = new XmlSerializer(typeof(MyFormatter));
// The property below is accessed back in the Value Converter for binding
StronglyTypedObject = (StObject)serializer.Deserialize(e.Result);
_event.Set();
}

WP7 Get return value from Async HttpWebRequest

I have wrote this code:
private void button1_Click(object sender, RoutedEventArgs e)
{
HttpWebRequest request = SendRequests.CreateRequest(serverTextBox.Text);
IAsyncResult result = request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
}
private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
try
{
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
string results = httpwebStreamReader.ReadToEnd();
loginValue = Parser.ParseLoginValue(results);
}
myResponse.Close();
}
catch (WebException we)
{
//
}
}
My problem at this point is that after the Click on the button I need the return value (here loginValue) of the BeginGetResponse to go on with the execution of the application.
I know that this is against the entire sense of asynchronous calls, but, there's a way to wait for the results before going on with the main thread?
Unfortunately, you have answered your own question. On Windows Phone, there is no way to do a synchronous web call.
But you don't really need to. If there is really nothing for your user to do while waiting for the response from the web, slap up a translucent overlay with a progress bar (or even better, use the global progress bar in the system try) and abide.
By contrast, if you were waiting for a synchronous call from the internet to return (which on a mobile device could take a long time), the UI would be locked and the user would think the application had hung (which, technically, is true).
Asynchronicity is your friend. Play nice with it.

SmtpClient.SendAsync blocking my ASP.NET MVC Request

I have a Action that sends a simple email:
[HttpPost, ActionName("Index")]
public ActionResult IndexPost(ContactForm contactForm)
{
if (ModelState.IsValid)
{
new EmailService().SendAsync(contactForm.Email, contactForm.Name, contactForm.Subject, contactForm.Body, true);
return RedirectToAction(MVC.Contact.Success());
}
return View(contactForm);
}
And a email service:
public void SendAsync(string fromEmail, string fromName, string subject, string body, bool isBodyHtml)
{
MailMessage mailMessage....
....
SmtpClient client = new SmtpClient(settingRepository.SmtpAddress, settingRepository.SmtpPort);
client.EnableSsl = settingRepository.SmtpSsl;
client.Credentials = new NetworkCredential(settingRepository.SmtpUserName, settingRepository.SmtpPassword);
client.SendCompleted += client_SendCompleted;
client.SendAsync(mailMessage, Tuple.Create(client, mailMessage));
}
private void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Tuple<SmtpClient, MailMessage> data = (Tuple<SmtpClient, MailMessage>)e.UserState;
data.Item1.Dispose();
data.Item2.Dispose();
if (e.Error != null)
{
}
}
When I send a email, I am using Async method, then my method SendAsync return immediately, then RedirectToAction is called. But the response(in this case a redirect) isnĀ“t sent by ASP.NET until client_SendCompleted is completed.
Here's what I'm trying to understand:
When watching the execution in Visual Studio debugger, the SendAsync returns immediately (and RedirectToAction is called), but nothing happens in the browser until email is sent?
If i put a breakpoint inside client_SendCompleted, the client stay at loading.... until I hit F5 at debugger.
This is by design. ASP.NET will automatically wait for any outstanding async work to finish before finishing the request if the async work was kicked off in a way that calls into the underlying SynchronizationContext. This is to ensure that if your async operation tries to interact with the HttpContext, HttpResponse, etc. it will still be around.
If you want to do true fire & forget, you need to wrap your call in ThreadPool.QueueUserWorkItem. This will force it to run on a new thread pool thread without going through the SynchronizationContext, so the request will then happily return.
Note however, that if for any reason the app domain were to go down while your send was still in progress (e.g. if you changed the web.config file, dropped a new file into bin, the app pool recycled, etc.) your async send would be abruptly interrupted. If you care about that, take a look at Phil Haacks WebBackgrounder for ASP.NET, which let's you queue and run background work (like sending an email) in such a way that will ensure it gracefully finishes in the case the app domain shuts down.
This is an interesting one. I've reproduced the unexpected behaviour, but I can't explain it. I'll keep digging.
Anyway the solution seems to be to queue a background thread, which kind of defeats the purpose in using SendAsync. You end up with this:
MailMessage mailMessage = new MailMessage(...);
SmtpClient client = new SmtpClient(...);
client.SendCompleted += (s, e) =>
{
client.Dispose();
mailMessage.Dispose();
};
ThreadPool.QueueUserWorkItem(o =>
client.SendAsync(mailMessage, Tuple.Create(client, mailMessage)));
Which may as well become:
ThreadPool.QueueUserWorkItem(o => {
using (SmtpClient client = new SmtpClient(...))
{
using (MailMessage mailMessage = new MailMessage(...))
{
client.Send(mailMessage, Tuple.Create(client, mailMessage));
}
}
});
With .Net 4.5.2, you can do this with ActionMailer.Net:
var mailer = new MailController();
var msg = mailer.SomeMailAction(recipient);
var tcs = new TaskCompletionSource<MailMessage>();
mailer.OnMailSentCallback = tcs.SetResult;
HostingEnvironment.QueueBackgroundWorkItem(async ct =>
{
msg.DeliverAsync();
await tcs.Task;
Trace.TraceInformation("Mail sent to " + recipient);
});
Please read this first: http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx
I sent the bug to Microsoft Connect https://connect.microsoft.com/VisualStudio/feedback/details/688210/smtpclient-sendasync-blocking-my-asp-net-mvc-request

OAuthException not catched with C# FacebookSDK

I try to get my code working with catching certain errors. I store the token for a user after he or she grants permission to my app (this is a WP7 app). When I try to post on the wall by using the stored token it works. When I remove the permissions on facebook it throws an OAuthException. I can't catch it it seems. My app just crashes. This is the code I used:
private object PostToFacebook()
{
_fbApp = new FacebookClient(_appsettings.faceBookToken);
FacebookAsyncCallback callback = new FacebookAsyncCallback(this.postResult);
var parameters = new Dictionary<string, object>();
parameters.Add("message", "message on wall");
try
{
_fbApp.PostAsync("me/feed", parameters, callback);
}
catch (Exception ex)
{
}
return null;
}
private void postResult(FacebookAsyncResult asyncResult)
{
if (asyncResult.Error == null)
{
status = "succes";
}
else
{
status = "error" + asyncResult.Error.Message;
}
}
The try catch doesn't catch anything and the generic exception handler in my app.xaml.cs either.
Any ideas how to catch this error so I can ask the user to authenticate again?
Put your try..catch in the callback.
You can also catch exceptions globally by handling the UnhandledException event on the App object.

Hooking to Send/Receive Sync event of Outlook Contacts

How can I attach an event handler for SendAndReceive event of Contact folders/Contact Items in Outlook 2007 using VSTO AddIn? I tried using:
Application.ActiveExplorer().SyncObjects.ForEach
{
SyncObject.SyncEnd += \\Do something
}
But it is not working.
I tried
Application.ActiveExplorer().SyncObjects.AppFolders.SyncEnd += \\EventHandler
This hooks on to send/receive of all default folders..
Actually my need was a bit different but may be the same:
I wanted to be notified of the changes of a folder (appointments in my case) after a send/receive.
My first thought (and I think you are on the same track) was to check for a send/receive event and maybe get some collection of items out of it or something similar, but no such thing is available. (as is also explained in this forum post)
My second path came from the following article: I can register to the Item_Add and Item_Change (and even Item_Removed) event of a folder (whom are also triggered by the changes done by a send receive):
Some code:
// Get the folder calendar folder and subscribe to the events.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items.ItemChange += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(Items_ItemChange);
}
// Do something with it.
void Items_ItemAdd(object Item)
{
logItem(Item, "Add");
}
void logItem(object Item, string Action)
{
Outlook.AppointmentItem item = Item as Outlook.AppointmentItem;
File.AppendAllText(#"e:\log.txt", string.Format("Item {0}: {1}", Action, Item));
if (item != null)
{
File.AppendAllText(#"e:\log.txt", " - Appointment: " + item.Subject);
}
}
You can hook up the mail send/receive event and then check that the mail type is a ContactItem. Here is an example of the Send event.
// hook up the event
this.Application.ItemSend += ThisApplication_SentMail;
then in your event handler you check the mail item type;
internal void ThisApplication_SentMail(object item, ref bool cancel)
{
Outlook.ContactItem contactItem = item as Outlook.ContactItem;
// mail message is not a ContactItem, so exit.
if (contactItem == null) return;
// do whatever you need to here
}
In my case, I need to trigger an event after a new email is received & after email sync so that I get a new email, or else I will not receive a new email attachment.
Below my solution may help you.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewMail += Application_NewMail;
}
private void Application_NewMail()
{
_currentExplorer = Application.ActiveExplorer();
_currentExplorer.Session.SyncObjects[1].SyncEnd += AppFolders_SyncEnd;
_currentExplorer.Session.SyncObjects[1].Start();
}
private void AppFolders_SyncEnd()
{
//Your enter code here
}

Resources