three email Sending script in C# .NET

there are several ways, secured and unsecured , to sending an email from C#. there are three ways given here.

Email sending script from C#

Sending an email from C# script is quite easy thing. Most popular two ways to send an email from C# scripts are as follows:
1.)

public string sendingMail(string strFrom, string strTo, string strSubject, string strMessage)
{
MailMessage myMessage = new MailMessage(new MailAddress(strFrom), new MailAddress(strTo));
myMessage.IsBodyHtml = true;
myMessage.Priority = MailPriority.Normal;
myMessage.Body = strMessage;
myMessage.Subject = strSubject;
SmtpClient client = new SmtpClient("", 25);
try
{
client.Send(myMessage);
}
catch(Exception ex)
{
return ex.ToString();
}
return "Message Has Been Sent"
;
}



2.)
public string AuthenticatedMailSend(string mailTo, string Subject, string MessageBody)

{

System.Net.Mail.MailMessage email = new MailMessage("TEST@test.com", mailTo);

email.Subject = Subject;

email.IsBodyHtml = true;

email.Body = MessageBody;

System.Net.Mail.SmtpClient mailClient = new SmtpClient();

System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("", "");

mailClient.Host = "";

mailClient.Port = 25;

mailClient.UseDefaultCredentials = false;

mailClient.Credentials = basicAuthenticationInfo;

try

{

mailClient.Send(email);

}

catch (Exception ex)

{

return ex.ToString();

}

return "1";

}

You ccan use any of the above methods, some mail server only supports authenticated email send by script. In that case, go for 2nd method.

Don’t forget to reference “using System.Net.Mail;” namespace for sending an email.

Reference: Ritesh Shah

By Ritesh Shah   Popularity  (3259 Views)
Biography - Ritesh Shah
I am Ritesh Shah and currently working as IT Project Leader in one KPO of Environment Laboratory located at NJ, USA area. I have been working with Microsoft technology since last 8+ years and having sound knowledge in SQL-Server, Asp.NET and C#. I have been working as a Project Leader & Pricipal Database Administrator in my current job since 3+ year. You can further read my blogs at SQLHub.Com