ThreadPool and Memory (BTStackServer) exception - .NET - asp.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.

Related

QR Code Reader for Sony SmartEyeglass

$ Hello, I am using an app I found on Github https://github.com/simsor/SmartEyeglassQRCode/blob/master/AndroidApp/app/src/main/java/com/example/sony/smarteyeglass/extension/helloworld/HelloWorldControl.java, which is a qr code reader for Sony's SmartEyeglass. But for some reason it is not working. When I scan a QRcode, it either says "QR code not found" or "please wait" for a very long time, without ever showing the result. I tried a few things, but nothing changed. Does any of you maybe know what's going on ?
public void processPicture(CameraEvent event) {
updateLayout("Please wait...");
if (event.getIndex() == 0) {
if (event.getData() != null && event.getData().length > 0) {
byte[] data = event.getData();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
//copy pixel data from the Bitmap into the 'intArray' array
bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
BinaryBitmap bbmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new QRCodeReader();
int DelayTime = 5000;
boolean error = false;
try {
Result result = reader.decode(bbmap);
Log.d(Constants.LOG_TAG, result.getText());
doWebsiteCommunication(result.getText());
} catch (NotFoundException e) {
updateLayout("QR Code Not Found");
error = true;
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
updateLayout(new String[] {
"QR Code looks corrupted",
"Maybe try again?"
});
error = true;
} catch (FormatException e) {
e.printStackTrace();
updateLayout("That's not a QR Code");
error = true;
}
if (error) {
try {
Thread.sleep(DelayTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentlyTakingPicture = false;
updateLayout(DEFAULT_TEXT);
}
}
}
}
The problem is the
doWebsiteCommunication(result.getText());
Just replace it with
updateLayout(result.getText());
This should work fine
PS: the doWebsiteCommunication was meant to update an external Website with the scandata, but since you just wanna read the qr code you dont need it

Trying to prevent session tampering works on local environment but not on prod server

So i want to prevent session tampering in my site and i implemented this in global.asax. What im doing is im generating a hash key using the GenerateHashKey function. which basically uses the browser version,userhost address etc to create a hash key. This hash key im attaching to ASP.NET_SessionId cookie. Now this works perfectly in local environment. but as soon as i host it to prod server, the "Invalid" exception is thrown the first time and then it works fine. why is this happening
I used this article
http://www.codeproject.com/Articles/859579/Hack-proof-your-asp-net-applications-from-Session
protected void Application_BeginRequest(object sender, EventArgs e)
{
try
{
if (Request.Cookies["ASP.NET_SessionId"] != null && Request.Cookies["ASP.NET_SessionId"].Value != null)
{
string newSessionID = Request.Cookies["ASP.NET_SessionId"].Value;
//Check the valid length of your Generated Session ID
if (newSessionID.Length <= 24)
{
//Log the attack details here
Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddDays(-30);
Response.Cookies["ASP.NET_SessionId"].Value = null;
throw new HttpException("Empty");
}
//Genrate Hash key for this User,Browser and machine and match with the Entered NewSessionID
if (GenerateHashKey() != newSessionID.Substring(24))
{
//Log the attack details here
Response.Cookies["TriedTohack"].Value = "True";
Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddDays(-30);
Response.Cookies["ASP.NET_SessionId"].Value = null;
throw new HttpException("Invalid:"+newSessionID);
}
//Use the default one so application will work as usual//ASP.NET_SessionId
Request.Cookies["ASP.NET_SessionId"].Value = Request.Cookies["ASP.NET_SessionId"].Value.Substring(0, 24);
}
}
catch(Exception Ex)
{
if (Ex.Message == "Invalid")
{
Response.Redirect(string.Format("~/PraiseError.aspx?Message={0}", Uri.EscapeDataString(Ex.Message)));
}
else
{
Response.Redirect("~/Home.aspx");
}
}
}
protected void Application_EndRequest(object sender, EventArgs e)
{
string gn = GenerateHashKey();
try
{
//Pass the custom Session ID to the browser.
if (Response.Cookies["ASP.NET_SessionId"] != null)
{
Response.Cookies["ASP.NET_SessionId"].Value = Request.Cookies["ASP.NET_SessionId"].Value.Replace(gn, "") + gn;
}
else
{
Response.Cookies["ASP.NET_SessionId"].Value = Request.Cookies["ASP.NET_SessionId"].Value + gn;
}
}
catch
{
Response.Cookies["ASP.NET_SessionId"].Value = Request.Cookies["ASP.NET_SessionId"].Value + gn;
}
}
private string GenerateHashKey()
{
StringBuilder myStr = new StringBuilder();
myStr.Append(Request.Browser.Browser);
myStr.Append(Request.Browser.Platform);
myStr.Append(Request.Browser.MajorVersion);
myStr.Append(Request.Browser.MinorVersion);
myStr.Append(Request.UserHostAddress);
//myStr.Append(Request.LogonUserIdentity.User.Value);
SHA1 sha = new SHA1CryptoServiceProvider();
byte[] hashdata = sha.ComputeHash(Encoding.UTF8.GetBytes(myStr.ToString()));
return Convert.ToBase64String(hashdata);
}

Sending multiple mail using aync in asp.net

I have to send mail to multiple recipient eg. to all the employee of an organization. For this I go through the resources via search engine and decided to make multiple instant of SmtpClient and send mail using async. So for test I write following code having gmail test server.
public void SendMail()
{
try
{
string strEmail = string.Empty;
// for collecting multiple recepient
//foreach (GridViewRow grd in gdv_txtMailTo.Rows)
//{
// CheckBox chkBx = (CheckBox)grd.FindControl("chkBxSelect");
// if (chkBx != null && chkBx.Checked)
// {
// strEmail += ((Label)grd.FindControl("Label1")).Text + ',';
// }
//}
strEmail = "yogendra.paudyal44#gmail.com";
string emails = strEmail;
string output = DBNull.Value.ToString();
//if (emails != "")
//{
// output = emails.Remove(emails.Length - 1, 1);
//}
MassMail_Controller.SaveSelectedEmails(output, PortalID);
MassMail_Info info = new MassMail_Info();
info.SendFrom = txtMailFrom.Text;
info.Subject = txtSubject.Text;
info.CC = txtCC.Text;
info.BCC = txtBCC.Text;
info.FileName = "";
info.SendTo = strEmail;
string messageTemplate = txtBody.Text;
info.Body = messageTemplate;
info.UserModuleId = UserModuleID;
info.PortalId = PortalID;
for (int i = 0; i < 50; i++) {
MailHelper.SendMailOneAttachment(info.SendFrom, info.SendTo, info.Subject, info.Body, info.FileName, info.CC, info.BCC);
}
//Thread thread = new Thread(new ParameterizedThreadStart(GetAllEmail));
//thread.IsBackground = true;
//thread.Start(bit);
//while (thread.IsAlive)
//{
// ShowMessage(SageMessageTitle.Exception.ToString(), "Mail Sent", "", SageMessageType.Success);
//}
}
catch (Exception ex)
{
ProcessException(ex);
}
}
And mailHelper would be:
public static void SendEMail(string From, string sendTo, string Subject, string Body, ArrayList AttachmentFiles, string CC, string BCC, bool IsHtmlFormat)
{
SageFrameConfig sfConfig = new SageFrameConfig();
//string ServerPort = sfConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.SMTPServer);
//string SMTPAuthentication = sfConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.SMTPAuthentication);
//string SMTPEnableSSL = sfConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.SMTPEnableSSL);
//string SMTPPassword = sfConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.SMTPPassword);
//string SMTPUsername = sfConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.SMTPUsername);
string ServerPort = (SageFrameSettingKeys.SMTPServer);
string SMTPAuthentication =(SageFrameSettingKeys.SMTPAuthentication);
string SMTPEnableSSL = (SageFrameSettingKeys.SMTPEnableSSL);
string SMTPPassword = (SageFrameSettingKeys.SMTPPassword);
string SMTPUsername = (SageFrameSettingKeys.SMTPUsername);
string[] SMTPServer = ServerPort.Split(':');
try
{
MailMessage myMessage = new MailMessage();
myMessage.To.Add(sendTo);
myMessage.From = new MailAddress(From);
myMessage.Subject = Subject;
myMessage.Body = Body;
myMessage.IsBodyHtml = true;
if (CC.Length != 0)
myMessage.CC.Add(CC);
if (BCC.Length != 0)
myMessage.Bcc.Add(BCC);
if (AttachmentFiles != null)
{
foreach (string x in AttachmentFiles)
{
if (File.Exists(x)) myMessage.Attachments.Add(new Attachment(x));
}
}
SmtpClient smtp = new SmtpClient();
if (SMTPAuthentication == "1")
{
if (SMTPUsername.Length > 0 && SMTPPassword.Length > 0)
{
smtp.Credentials = new System.Net.NetworkCredential(SMTPUsername, SMTPPassword);
}
}
smtp.EnableSsl = bool.Parse(SMTPEnableSSL.ToString());
if (SMTPServer.Length > 0)
{
if (SMTPServer[0].Length != 0)
{
smtp.Host = SMTPServer[0];
if (SMTPServer.Length == 2)
{
smtp.Port = int.Parse(SMTPServer[1]);
}
else
{
smtp.Port = 25;
}
object userState = myMessage;
//wire up the event for when the Async send is completed
smtp.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
smtp.SendAsync(myMessage,userState);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
//string answer = Console.ReadLine();
// If the user canceled the send, and mail hasn't been sent yet,
// then cancel the pending operation.
//if (answer.StartsWith("c"))
//{
// smtp.SendAsyncCancel();
//}
//// Clean up.
//myMessage.Dispose();
Console.WriteLine("Goodbye.");
}
else
{
throw new Exception("SMTP Host must be provided");
}
}
}
catch (Exception ex)
{
throw ex;
}
}
This code snippet works fine but I couldnot send specified number of mail. The number of email sent is differnt each time I execute this code ie. it may be 35, 40, 42 etc. It seems some instances of SmtpClient got failed, but I didnot get any exception. Am I doing something wrong. Do We have better option to send multiple mail at one time?
I want to share my experience with sending emails.I also used to send B-Day emails but in my case there were usually 100-150 people and all mails delivered successfully. I had made a web service for this whose only task was to send emails. But before emails started to deliver successfully we tested on local machine which worked fine but when we tested it on server i face the same issue and cause of this failure was that we deployed our web service in .Net framework 2.0 than we changed it to 4.0 and tested it again with 10000,5000,1000 emails and it worked fine not all emails but most of them reached destination.Also one more thing to mention is that the address from which we were sending email was restricted by network department in email server to send only 100 emails.Also try to avoid sending too many emails from one sender and from one email server because you can get black listed.
Summary
First of all check that are you using .Net framework 2.0 if yes
switch to 4.0.
Make sure that there are no restrictions by network department at
email server and also to address which you use as sender.
Place all your code in using statement to make sure objects get
disposed.
Send your emails in chunks(About 100 at a time).
using()
{
//Place you email sending code here.
}

How to resend email using SendAsync() in 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!!

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