Wednesday, January 4, 2012

Sending Mail Using System.Web.Mail/System.Net.Mail with Attachments

Hello friends,
Today also I did a lot of R&D for sending mail using System.Web.Mail for .Net 1.1.
Many of you are seeing this post because while trying sending mail in 1.1 you are unable to do so.

If you thought that it was a serious shortcoming that .Net framework 1.1 wasn't able to send email through SMTP servers that used authentication, you are in grave ignorance. On the surface of it, yes, you cannot send an email with the standard System.Web.Mail.MailMessage class; but you can of course play tricks on the Exchange server (yeah, that's what I get out of it that this things works only on an Exchange server) to make it authenticate you.

There is a namespace called http://schemas.microsoft.com/cdo/configuration that does those funny (and highly interesting) tricks.

Here both the version for you. Send mail either using System.Web.Mail or System.

protected void SendMail()
{
//step 1:
MailMessage msg = new MailMessage();
//Either use above if you are using System.Web.Mail or uncomment below one if using System.Net.Mail
//System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string smtpServer = "";

//step 2:
msg.To = _emailIdsToSend;
//Either use above if you are using System.Web.Mail or uncomment below one if using System.Net.Mail
//mail.To.Add(_emailIdsToSend);

//step 3:
msg.From = "dummyfrom@dummy.com";
//Either use above if you are using System.Web.Mail or uncomment below one if using System.Net.Mail
// mail.From = new System.Net.Mail.MailAddress("dummyfrom@dummy.com");
string date = DateTime.Now.ToString("dd-MM-yyyy");

//step 4:
msg.Subject = "Test Mail on : "+date;
//Either use above if you are using System.Web.Mail or uncomment below one if using System.Net.Mail
//mail.Subject = "Test Mail on : " + date;

//step 5:
msg.Body = "some text goes here...";
//Either use above if you are using System.Web.Mail or uncomment below one if using System.Net.Mail
//mail.Body = "some text goes here..";

//step 6:
//attachments here...
MailAttachment attachment = new MailAttachment(finalFileToAttach);
//Either use above if you are using System.Web.Mail or uncomment below one if using System.Net.Mail
//System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(finalFileToAttach);

//step 7:
msg.Attachments.Add(attachment);
//Either use above if you are using System.Web.Mail or uncomment below one if using System.Net.Mail
//mail.Attachments.Add(attachment);

//step 8:
msg.BodyFormat = MailFormat.Html;
//Either use above if you are using System.Web.Mail or uncommet below one if using System.Net.Mail
//mail.IsBodyHtml = true;

//step 9:credentials
msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "yoursmtpserverhere..";
msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 25;
msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
////Most of the times the @domain is necessary with the username
msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "usernamewithdomainehere..";
msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "passwordhere..";
//If only the server uses SSL, in this case, the smtpserverport field would be different too
message.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = true;
if (ConfigurationSettings.AppSettings["SMTPServerName"].ToString() != null && ConfigurationSettings.AppSettings["SMTPServerName"].ToString() != "")
SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["SMTPServerName"].ToString().Trim();
//.net either use above if you are using System.Web.Mail or uncommet below one if using System.Net.Mail
//System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
//client.Host = "yoursmtpserverhere..";
//client.Credentials=new System.Net.NetworkCredential("usernamewithdomainhere", "password here");
//client.Port = 25;
//step 10: Send Mail
try
{

SmtpMail.Send(msg);
//.net either use above if you are using System.Web.Mail or uncommet below one if using System.Net.Mail
//client.Send(mail);
}
finally
{
//mail.Dispose();
msg = null;
attachment = null;

}
}

No comments:

Post a Comment