How to resend email using SendAsync() in asp.net - asp.net

I am using SendAsync to send an email. The reason I'm using async is simply to free up the UI rather than send multiple emails.
I have created the following callback event:
static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
var client = sender as SmtpClient;
var message = e.UserState as MailMessage;
if (e.Error.IsNotNull())
{
if (e.Error is SmtpFailedRecipientException)
{
var status = ((SmtpFailedRecipientException)(e.Error)).StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable ||
status == SmtpStatusCode.TransactionFailed)
{
// a new message!
}
else
{
// TODO: Log other uncaught recipient failures
}
}
else
{
// TODO: Log all other failure reasons
}
}
client.Dispose();
message.Dispose();
}
As you can see I'm attempting to catch some recipients failures. If I find such an exception I want to try and resend the email.
I'm trying to work out how to resend the email safely. I'm thinking to create a new SmtpClient rather than reuse the existing one, but to be honest, I'm fairly new to .net and I'm not so sure of the implications.
Any advice would be appreciated.

Sending email asynchronously without delaying response back to the client(UI) requires a Backgroundworker in .Net. I implemented this on my site and will share the class source code with you.
using System;
using System.Collections.Generic;
using System.Web;
using System.ComponentModel; //Background worker namespace
using System.Net.Mail;
/// <summary>
/// Summary description for ClassName
/// </summary>
///
public class postmail
{
BackgroundWorker bw = new BackgroundWorker();
string email1, subject1, message1, failedemails;
public postmail(string email, string subject, string message)
{
bw.WorkerReportsProgress = false;
bw.WorkerSupportsCancellation = false;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
email1 = email;
subject1 = subject;
message1 = message;
}
public postmail()
{
// TODO: Complete member initialization
}
public void startsending() {
bw.RunWorkerAsync();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Flush(); // send all buffered output to client
HttpContext.Current.Response.End();
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
var finalemail = email1.Split(new[] { ',' }, StringSplitOptions.None);
//loop through the email addresses and send individually
for (int c = 0; c < finalemail.Length; c++) {
try
{
MailMessage mailMessage = new MailMessage();
// Sender Address
mailMessage.From = new MailAddress("emailaddress");
// Recepient Address
mailMessage.To.Add(finalemail[c].ToString());
// Subject
mailMessage.Subject = subject1.ToString();
// Body
mailMessage.Body = message1.ToString();
// format of mail message
mailMessage.IsBodyHtml = true;
// new instance of Smtpclient
SmtpClient mailSmtpClient = new SmtpClient("mail server");
//mailSmtpClient.EnableSsl = true;
mailSmtpClient.Credentials = new System.Net.NetworkCredential("emailaddress", "password");
// mail sent
Object userState = mailMessage;
mailSmtpClient.SendAsync(mailMessage, userState);
}
catch (Exception exc)
{
//fix for you
var ext = exc.ToString(); //catch exception for failed message
failedemails = failedemails + finalemail[c] + ","; //create a string of failed emails
}
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//called when the background process is done working
if(failedemails != null){
postmail(failedemails, subject1, message1); //resend the failed email
startsending();
}
}
}
Your concept might not be exact like mine but the key methods are:
Create an event handlers for the BackgroundWorker.
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = false;
bw.WorkerSupportsCancellation = false;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
try
{
//Send your mail
}
catch (Exception exc)
{
//Catch exception here and call the resend method
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//do something after completion
}
The fix i made for you was to build a string of all failed addresses, then resend them after the backgroundworker is done working. cheers!!

Related

Logging Request and response to Application Insight

I'm trying to log API request payload and response data to Azure Application Insight. Using trace I can able to log. but I want to know what is the best way to log request and response data to application insight. Because data is huge, no.of API calls will be more. I can't just trace hundreds of thousands of request and response data using tracing. I tried some of the blogs like using ITelemetryInitializer/ httpcontext.feature,get, but no luck.
I want to log from c# .Net framework, Web API, not .NET Core.
Sample code which I tried.
public class AzureRequestResponseInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry != null && (HttpContext.Current.Request.HttpMethod == HttpMethod.Post.ToString() || HttpContext.Current.Request.HttpMethod == HttpMethod.Get.ToString()))
{
using (var reader = new StreamReader(HttpContext.Current.Request.InputStream))
{
string requestBody = reader.ReadToEnd();
requestTelemetry.Properties.Add("body", requestBody);
}
}
You can achieve it by implementing IHttpModule that using Application Insight's TelemtryClient, see the following code:
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Contoso.Services.Logging.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace Contoso.Services.Logging.Modules
{
public class CaptureTrafficModule : IHttpModule
{
public TelemetryClient Telemetry { get; set; }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(context_EndRequest);
Telemetry = new TelemetryClient();
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
HttpResponse response = HttpContext.Current.Response;
OutputFilterStream filter = new OutputFilterStream(response.Filter);
response.Filter = filter;
app.Context.Items["Filter"] = filter;
StringBuilder request = new StringBuilder();
// Write All The Headers too :
//foreach (string key in app.Request.Headers.Keys)
//{
// request.Append(key);
// request.Append(": ");
// request.Append(app.Request.Headers[key]);
// request.Append("\n");
//}
//request.Append("\n");
byte[] bytes = app.Request.BinaryRead(app.Request.ContentLength);
if (bytes.Count() > 0)
request.Append(Encoding.ASCII.GetString(bytes));
app.Request.InputStream.Position = 0;
string operationName = $"{app.Request.HttpMethod} {app.Request.FilePath}";
string activityId = System.Diagnostics.Activity.Current.RootId;
app.Context.Items["OperationName"] = operationName;
app.Context.Items["ActivityId"] = activityId;
using (var logRequest = Telemetry.StartOperation<RequestTelemetry>(operationName, System.Diagnostics.Activity.Current.RootId, System.Diagnostics.Activity.Current.RootId))
{
try
{
//logRequest.Telemetry.Id = $"10-{activityId}";
logRequest.Telemetry.Url = app.Request.Url;
logRequest.Telemetry.Properties["RequestBody"] = request.ToString();
}
catch (Exception ex)
{
logRequest.Telemetry.Success = false;
Telemetry.TrackException(ex);
//throw;
}
}
}
void context_EndRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
OutputFilterStream filter = null;
string operationName = "", activityId = Guid.Empty.ToString(), responseStr = "NONE";
if (app.Context.Items.Contains("OperationName"))
operationName = app.Context.Items["OperationName"].ToString();
if (app.Context.Items.Contains("ActivityId"))
activityId = app.Context.Items["ActivityId"].ToString();
if (app.Context.Items.Contains("Filter"))
{
filter = (OutputFilterStream)app.Context.Items["Filter"];
responseStr = filter.ReadStream();
}
using (var logResponse = Telemetry.StartOperation<RequestTelemetry>(operationName, activityId, activityId))
{
try
{
//logResponse.Telemetry.Id = $"20-{activityId}";
logResponse.Telemetry.Url = app.Request.Url;
logResponse.Telemetry.Properties["ResponseBody"] = responseStr.ToString();
}
catch (Exception ex)
{
logResponse.Telemetry.Success = false;
Telemetry.TrackException(ex);
//throw;
}
}
}
public void Dispose()
{
//Does nothing
}
}
}
This question is answered in https://thirum.wordpress.com/2019/08/19/logging-the-http-response-body-in-application-insights/
Please take a look.

Spring MVC returns 405 for api call made from my android client

I have an android app which is making api requests to my server running Spring MVC. The RestController works fine when I make a request from the browser but it responds with 404 when I am making requests from android. Not sure why
Here is code snippet from Android app making requests
public class AsyncFetch extends AsyncTask<Pair<String, String>, String, String> {
public ProgressDialog pdLoading;
private HttpURLConnection conn;
private String urlStr;
private String requestMethod = "GET";
public AsyncFetch(String endpoint, Context ctx)
{
pdLoading = new ProgressDialog(ctx);
Properties reader = PropertiesReader.getInstance().getProperties(ctx, "app.properties");
String host = reader.getProperty("host", "10.0.2.2");
String port = reader.getProperty("port", "8080");
String protocol = reader.getProperty("protocol", "http");
String context = reader.getProperty("context", "");
this.urlStr = protocol+"://"+host+":"+port+context+endpoint;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
#Override
protected String doInBackground(Pair<String, String>... params) {
URL url;
try {
url = new URL(urlStr);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod(requestMethod);
conn.setDoOutput(true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made`enter code here`
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
Spring MVC Controller
#RestController
public class ApiController {
#RequestMapping(value = "homefeed", method=RequestMethod.GET)
public String homefeed(#RequestParam(value="userId", required = false) Integer id, #RequestParam(value="search", required = false) String search, #RequestParam(value="page", required = false, defaultValue = "0") Integer page) { ... }
}
localhost:8080/api/homefeed -- works
127.0.0.1:8080/api/homefeed -- works
My Public IP:8080/api/homefeed -- does not works
10.0.2.2:8080/api/homefeed -- android emulator to localhost -- does not work
10.0.2.2:8080/Some resource other than the api endpoint -- works
Any help is highly appreciable, have wasted quiet some time in debugging.

Tracking Bounced Emails Through ASP.Net

Is there any way I can track (through the code) the bounced emails.
Consider the email id like 'skdfisdcnsodioifs#gmail.com'. This is valid email id but does not exists so certainly it will be bounced backed.
I am using ASP.Net's SmtpClient with "message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure". However it still shows the message sent successfully and after ~20-25 minutes I get the fail delivery notification email.
Below is my code
class Program
{
static void Main(string[] args)
{
SmtpClient client = new SmtpClient("xxxx.xxxxxx.xxx", 25);
client.Credentials = new NetworkCredential("xxxxxx#xxxxxx.com", "xxxxxx");
MailAddress to = new MailAddress("abcdsfasdfasdfasdfasdf2342342defgh#gmail12333.com");
MailAddress from = new MailAddress("xxxxxx#xxxxxx.com");
MailMessage message = new MailMessage(from, to);
message.Headers.Add("Return-Path", "xxxxxx#gmail.com");
message.ReplyToList.Add(new MailAddress("xxxxxx#gmail.com"));
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess;
message.Subject = "Test POC";
message.Body = "This is a test e-mail message sent by an application. ";
client.SendCompleted += client_SendCompleted;
string UserState = "test";
client.Timeout = 3;
try
{
Console.WriteLine("start to send email ...");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.SendAsync(message,UserState);
Console.WriteLine("email was sent successfully!");
}
catch (SmtpFailedRecipientsException ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}
catch (Exception ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}
Console.WriteLine("test.");
Console.ReadKey();
}
static void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
//String token = (string)e.UserState;
if (e.Cancelled)
{
Console.WriteLine("Send canceled.");
}
if (e.Error != null)
{
Console.WriteLine("[{0}] ", e.Error.ToString());
}
else
{
Console.WriteLine("Message sent.");
}
}
}
Thanks in advance.
Abhishek

ThreadPool and Memory (BTStackServer) exception - .NET

In continuation with my problem here, I created a test app that uses ThreadPool threads to send emails. Here's the code I am using.
protected void btnSend_Click(object sender, EventArgs e)
{
EmailInfo em = new EmailInfo { Body = "Test", FromEmail = "test#test.com", Subject = "Test Email", To = "test#test.com" };
//txtNumEmails is a textbox where I can enter number of emails to send
for (int i = 0; i < Convert.ToInt32(this.txtNumEmails.Text); i++)
{
bool bResult = ThreadPool.QueueUserWorkItem(new WaitCallback(EmailNow), em);
}
}
public void EmailNow(object emailInfo) // Email Info object
{
EmailInfo em = emailInfo as EmailInfo;
SmtpClient client = new SmtpClient("localhost");
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = #"C:\temp\testemails\";
MailMessage m = new MailMessage();
m.To.Add(new MailAddress(em.To));
m.Body = em.Body;
m.IsBodyHtml = false;
m.From = new MailAddress(em.FromEmail);
m.Subject = em.Subject;
client.Send(m);
}
For smaller numbers (10k, 50k) they work great, but once I increase the number to 200k (which is my target), I got this exception:
It created about 186k emails before it threw this exception.
I am assuming this is not the exception caused due to lack of disk space (since i am storing all the emails locally in C:\temp\testemails but because it was low in RAM(?). As suggested by a this user, I used semaphores to limit them to 10k, but still ran in to the same issue. Here's the code for the one with semaphores.
protected void Button1_Click(object sender, EventArgs e)
{
EmailInfo em = new EmailInfo { Body = "Test", FromEmail = "test#test.com", Subject = "Test Email", To = "test#test.com" };
var semaphore = new SemaphoreSlim(10000, 10000);
for (int i = 0; i < Convert.ToInt32(this.txtNumEmails.Text); i++)
{
semaphore.Wait();
bool bResult = ThreadPool.QueueUserWorkItem( (state) => {
try
{
EmailNow(em);
}
catch (Exception)
{
throw;
}
finally
{
semaphore.Release();
}
}, null);
}
}
This one threw an exception as well but I see all 200k emails in the folder. I can definitely use try catch and gracefully exit if this exception occurs, but how would I prevent this from happening in the first place.
Try disposing SmtpClient object by either wrapping inside using block or calling dispose() explicitly.
Try reducing the 10000 to 1000 for the SemaphoreSlim constructor.

SmtpClient.SendAsync bug in ASP.NET 2.0

I may be wrong, but if you are working with SmtpClient.SendAsync in ASP.NET
2.0 and it throws an exception, the thread processing the request waits
indefinitely for the operation to complete.
To reproduce this problem, simply use an invalid SMTP address for the host
that could not be resolved when sending an email.
Note that you should set Page.Async = true to use SendAsync.
If Page.Async is set to false and Send throws an exception the thread
does not block, and the page is processed correctly.
TIA.
Note that you should set Page.Async = true to use SendAsync.
Please explain the rationale behind this. Misunderstanding what Page.Async does may be the cause of your problems.
Sorry, I was unable to get an example working that reproduced the problem.
See http://msdn.microsoft.com/en-us/magazine/cc163725.aspx (WICKED CODE: Asynchronous Pages in ASP.NET 2.0)
EDIT: Looking at your code example, I can see that you're not using RegisterAsyncTask() and the PageAsyncTask class. I think you must do this when executing asynchronous tasks on a Page where #Async is set to true. The example from MSDN Magazine looks like this:
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask task = new PageAsyncTask(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation),
new EndEventHandler(TimeoutAsyncOperation),
null
);
RegisterAsyncTask(task);
}
Inside BeginAsyncOperation, then, should you send a mail asynchronously.
RegisterAsyncTask could not be used.
Look at the BeginEventHandler delegate:
public delegate IAsyncResult BeginEventHandler(
Object sender,
EventArgs e,
AsyncCallback cb,
Object extraData
)
It should return an IAsyncResult.
Now look at the SmtpClient.SendAsync function :
public void SendAsync(
MailMessage message,
Object userToken
)
There is no return value.
Anyway this is working fine, as long as SmtpClient.SendAsync does not throw an exception.
Here is mine. Give it a try.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Using an incorrect SMTP server
SmtpClient client = new SmtpClient(#"smtp.nowhere.private");
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("someone#somewhere.com",
"SOMEONE" + (char)0xD8 + " SOMEWHERE",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("someone#somewhere.com");
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1";
try
{
client.SendAsync(message, userState);
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(string.Format("Send Error [{0}].", ex.InnerException.Message));
}
finally
{
}
}
private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;
if (e.Cancelled)
{
Response.Write(string.Format("[{0}] Send canceled.", token));
}
if (e.Error != null)
{
Response.Write(string.Format("[{0}] {1}", token, e.Error.ToString()));
}
else
{
Response.Write("Message sent.");
}
}
}

Resources