Email using C#

I had a co-worker recently ask me what I did to send email from a C# web application. I gave him what worked for me. He made a few adjustments and sent it back to me.  I did request that he send me what he came up with that finally worked for him. I think he did a better job than I so I am going to post it for you to see. Have a look and see how it may fit into your application or project. Improvements are welcome. He ended up writing a class to call from anywhere in the project. It takes the following arguments; from, to, subject, body, attachments, and isBodyHTML boolean.

using System.Net;
using System.Net.Mail;

public class mailer
{
public Boolean sendemail(String strFrom, string strTo, string strSubject, string strBody, string strAttachmentPath, bool IsBodyHTML)
{
Array arrToArray;
char[] splitter = { ';' };
arrToArray = strTo.Split(splitter);
MailMessage mm = new MailMessage();
mm.From = new MailAddress(strFrom);
mm.Subject = strSubject;
mm.Body = strBody+disclaimer;
mm.IsBodyHtml = IsBodyHTML;
//mm.ReplyTo = new MailAddress("replyto@xyz.com");
foreach (string s in arrToArray)
{
mm.To.Add(new MailAddress(s));
}
if (strAttachmentPath != "")
{
try
{
//Add Attachment
Attachment attachFile = new Attachment(strAttachmentPath);
mm.Attachments.Add(attachFile);
}
catch { }
}
SmtpClient smtp = new SmtpClient();
try
{
smtp.Host = "mail.domain.com";
smtp.Port = 25; //Specify your port No;
smtp.Send(mm);
return true;
}
catch
{
mm.Dispose();
smtp = null;
return false;
}
}
}

Thanks Shane!

Hope this helped someone.  Have fun coding and as always, if there are any questions or suggestions, they are welcome.  Thank you.

Page.IsPostback is Sneaky

   I had an issue yesterday with a dropdown on a form. I was trying to post the values to a new form.  All the values would post fine except this one dropdown.  I was racking my brain.  What was happening is everytime I submitted the form, the value of the dropdown (varname.SelectedItem.Value) would be item[0] no matter what I choose!?  This was driving me crazy because all the other form variables were passing just fine. I even had another dropdown that was posting fine.  This one was giving me a problem.  What was the difference you may ask?  The only difference was that the dropdown that was not passing correctly was dynamically populated by a databind.  Well, it turns out, which now makes sense, is that when the page posts back, the original value is sent. This happens because the page load function runs first on post back. Hence the default value being posted. What you have to do is this;

 

If Not Page.IsPostBack Then

   varname.DataSource = DataTable

   varname.DataTextField = “value

   varname.DataValueField = “value

   varname.DataBind()

End If

 

Adding these two lines of code around my databind worked wonders. It’s the small things that count. Keep your eyes open and as always, if there are any questions or suggestions, they are welcome. Thank you.

Using Web Services: Best Practices

  • Ensure that the connection between web service and web server is asynchronous. This means that the web application does not need to stop its processes until the web server responds to its request.

  • Minimize the number of requests that an application needs to send to a web server to consume a web service. Send as much information as can be sent. This will ensure network traffic is cut to a minimum by decreasing the number of requests.

  • Ensure the web service is created using the concepts of object oriented programming. Create once use many times. This also should create a standard of information used.

  • Minimize time of response by using cache. This will ensure that every time the web service is called upon, the web service is not run, the cache will be used. You can set the time in which the cache can be refreshed so to catch any updates that may happen.