c#


Send email based on Request.QueryString parameters received


Hi guys I have a scenario which I would like you to see it, possible you get what I'm not getting now, I have to conditions, the first one works when parameters meet the criteria, the second one does not works (does not send the email out) even though all parameters are met:
try
{
SmtpClient smtpClient = new SmtpClient();
MailMessage mail = new MailMessage();
if (Request.QueryString["dot"].ToLower() == "rejected" || Request.QueryString["dep"].ToLower() == "rejected" || Request.QueryString["fdny"].ToLower() == "rejected")
{
mail.To.Add(new MailAddress("receiver#domain.org"));
mail.From = new MailAddress("sysadmin#domain.org", "DONOTREPLY");
mail.Subject = "The Subjectt";
mail.IsBodyHtml = true;
mail.Body = "testing testing testing";
smtpClient.Send(mail);
}
else if (Request.QueryString["dot"].ToLower() == "gcl36" && (Request.QueryString["fdny"].ToLower() == "rejected" || Request.QueryString["hro"].ToLower() == "rejected"))
{
mail.To.Add(new MailAddress("receiver#domain.org"));
mail.From = new MailAddress("sysadmin#domain.org", "DONOTREPLY");
mail.Subject = "The Subject ";
mail.IsBodyHtml = true;
mail.Body = "testing testing testing";
smtpClient.Send(mail);
}
}
catch (Exception ex)
{
return;
}
The URL looks like this: http://example.com/SPD/FileUpload.aspx?APPid=APP-016784&DocumentId=DC080497-9AFA-E511-BD4F-00155D47EC98&Userid=John Doe&SubName=Submission-1459797754588&dot=GCL36&fdny=Rejected&hro=na
As you may see in this URL the "dot" variable has the value GCL36 and "fdny" has rejected, however the second condition in the "if" never get called.
Ok.. I found the issue with the code. With the current code and the example URL even the first if block will not be executed.
In the first if block you are also checking Request.QueryString["dep"].ToString(). The example URL you have posted does not have parameter dep. That's why if statement will throw NullReferenceException and it will directly go to catch block and return from there without performing any action.
What you need to do is make sure that making sure that QueryString parameters are not null before you use them. Also you need to make sure the specific conditions are checked first before the generic conditions.
try
{
var dot = Request.QueryString["dot"]?.ToLower() ?? string.Empty;
var dep = Request.QueryString["dep"]?.ToLower() ?? string.Empty;
var fdny = Request.QueryString["fdny"]?.ToLower() ?? string.Empty;
var hro = Request.QueryString["hro"]?.ToLower() ?? string.Empty;
SmtpClient smtpClient = new SmtpClient();
MailMessage mail = new MailMessage();
//Putting if block of more specific condition check first.
if (dot == "gcl36" && (fdny == "rejected" || hro == "rejected"))
{
mail.To.Add(new MailAddress("receiver#domain.org"));
mail.From = new MailAddress("sysadmin#domain.org", "DONOTREPLY");
mail.Subject = "The Subject ";
mail.IsBodyHtml = true;
mail.Body = "testing testing testing";
smtpClient.Send(mail);
}
else if (dot == "rejected" || dep == "rejected" || fdny == "rejected")
{
mail.To.Add(new MailAddress("receiver#domain.org"));
mail.From = new MailAddress("sysadmin#domain.org", "DONOTREPLY");
mail.Subject = "The Subjectt";
mail.IsBodyHtml = true;
mail.Body = "testing testing testing";
smtpClient.Send(mail);
}
}
catch (Exception ex)
{
return;
}
This should resolve you issue.
P.S. You could have identified the issue if you have handled the exception properly in catch block. Simply returning from catch block will put you in this kind of situations quite frequently.

Related Links

ASp.net MVC without any view file- React
C# temporarily change child stdin
Two different versions of System.Net.Http. Local vs DEV server
How to reuse an xml document in multiple calls?
Add a generated certificate to the store and update an IIS site binding
Moq and Async fun using LazyCache
not able to update in 3 tier architecture
Deserialize a json container array
How to send message from child to parent process using environment variable in C#?
How can i use Take to get all files from a directory and all it's sub directories?
Escape sequences with String Interpolation
How to know if error from dotnet build command with powershell
Passing an entire array from one method to another [closed]
wpf get selected row when checkbox value changes
Preceding class declaration C# syntax like [SomeType (someArg1, …, someArgN)] [duplicate]
Why SharpDX render my bitmap in grey and corrupted?

Categories

HOME
qlikview
asp.net
classification
optimization
azure-documentdb
ide
jenkins-pipeline
process
wifi
hiveql
query-string
download
msbi
opacity
sahi
software-collections
google-app-maker
digital
mamp
hdf5
oxid
element
dat.gui
autologin
caldav
fibonacci
lotusscript
rvm
pumping-lemma
android-wear-2.0
symlink
spock
log4net-configuration
traitsui
angularjs-resource
http-proxy
wit-ai
lfe
irr
colorbox
pexpect
cakephp-3.2
piranha-cms
weld
azure-data-catalog
activeandroid
opos
ng-class
alarmmanager
turbolinks
scikit-image
patching
kik
innertext
web-optimization
bootstrap-tabs
dcg
tinymce-plugins
avd
crtdbg.h
drools-guvnor
kss
jsonschema2pojo
famous-engine
system.net
powerpoint-2010
xts
scjp
rails-migrations
dayofweek
windows-phone-store
php-amqplib
kendo-window
tlbimp
wpd
removeall
generator-expression
blotter
reflexil
wdm
xcdatamodel
versions
zend-acl
stackless

Resources

Encrypt Message