Sandip's Programming Zen

An attempt to share tech/coding experiences

Archive for October 2009

C# Winforms: Sending an email with attachment

leave a comment »

Sending an email from C# winforms application is exactly same as we do in webapps using System.Net.Mail namespace. Here is a sample code which sends out an email with an attachment (again it is not specific to winforms can be used in webapps as it is).

MailMessage msg = new MailMessage();
SmtpClient client;
Attachment attach = new Attachment(filepath);

if (varEmailID.IndexOf(',') > 0)
{
    string[] iDs;
    iDs = varEmailID.Split(',');
    for (int i = 0; i <= iDs.Length - 1; i++)
    {
        msg.To.Add(new MailAddress(iDs.GetValue(i).ToString()));
    }
}
else
{
    msg.To.Add(new MailAddress(varEmailID));
}

msg.Attachments.Add(attach);
msg.Subject = varSubject;
msg.Body = varBody;
msg.IsBodyHtml = false;
msg.Priority = MailPriority.Normal;

string FromEmail = ConfigurationSettings.AppSettings["SystemMailID"];
string SmtpServer = ConfigurationSettings.AppSettings["SmtpServer"];
string UserName = ConfigurationSettings.AppSettings["UserName"];
string Password = ConfigurationSettings.AppSettings["Password"]; 
client = new SmtpClient(SmtpServer);
client.UseDefaultCredentials = false;
System.Net.NetworkCredential creds = new System.Net.NetworkCredential(UserName, Password);
client.Credentials = creds;

msg.From = new MailAddress(FromEmail);

client.Send(msg);

Written by Sandip

October 17, 2009 at 4:22 am

Posted in .Net, Programming

Tagged with ,

LINQtoSQL : Not looking good in big project

with one comment

Having more than 150 tables and even more SPs in a database and then using LinqToSql for that is proving to be a bottleneck. 

LinqToSql is an awesome technology but working in a team of more than 5 developers, all trying to update a single dbml at the same time is not a good idea. You won’t notice this while working on a team of 1 or 2 developers , I still think subsonic or NHibernate would have been a better solution at least for the reason being they generate separate class files for each entity.

Written by Sandip

October 15, 2009 at 11:06 am

Posted in .Net, Programming

Tagged with ,

New in Asp.Net 4 : URL Routing in Webforms

leave a comment »

Asp.Net 4 (aka Framework 4.0 ) with VS 2010 now supports URL routing for webforms.

It would be a cool thing since Asp.net MVC was already supporting it out of box and this is just a sign that webforms are going to be a supported as prime platform for developments by MS.

Check here for more details.

Written by Sandip

October 13, 2009 at 9:32 am

Posted in .Net, Asp.Net, Programming

Sending Async Email in Asp.Net

leave a comment »

Found an interesting piece of code here.

When sending an email in your ASP.NET application there are times when you do not want the user experience to slow just to wait for an email to be sent.  The code sample below is how to send a System.Net.Mail.MailMessage asynchronously so that the current thread can continue while a secondary thread sends the email.

public static void SendEmail(System.Net.Mail.MailMessage m)
{
    SendEmail(m, true);
}

public static void SendEmail(System.Net.Mail.MailMessage m, Boolean Async)
{

    System.Net.Mail.SmtpClient smtpClient = null;

    smtpClient = new System.Net.Mail.SmtpClient("localhost");

        if (Async)
        {
            SendEmailDelegate sd = new SendEmailDelegate(smtpClient.Send);
            AsyncCallback cb = new AsyncCallback(SendEmailResponse);
            sd.BeginInvoke(m, cb, sd);
        }
        else
        {
            smtpClient.Send(m);
        }
}

private delegate void SendEmailDelegate(System.Net.Mail.MailMessage m);

private static void SendEmailResponse(IAsyncResult ar)
{
        SendEmailDelegate sd = (SendEmailDelegate)(ar.AsyncState);

        sd.EndInvoke(ar);
}

To use this just call the SendEmail() method with System.Net.Mail.MailMessage object.

Written by Sandip

October 13, 2009 at 9:26 am

Posted in .Net, Asp.Net, Programming