
Sending email in ASP.NET is similar to sending email in normal ASP.NET application. Below is my code snippet to declare the SendEmail method. You can copy-paste in a new class file and use it.
Send email method declaration
public class EmailSender
{
public static bool SendEmail(string toAddress, string replyToAddress, string ccAddress, string bccAddress, string subject, string body, MailPriority priority, bool isHtml)
{
try
{
using (SmtpClient smtpClient = new SmtpClient())
{
using (MailMessage message = new MailMessage())
{
MailAddress fromAddress = new MailAddress("noreply@myDomain.com", "NoReply, MyDomain.Com");
// You can specify the host name or ipaddress of your server
smtpClient.Host = "smtp.MyDomain.com"; //you can also specify mail server IP address here
//Default port will be 25
smtpClient.Port = 25;
NetworkCredential info = new NetworkCredential("noreply@myDomain.com", "Password");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = info;
//From address will be given as a MailAddress Object
message.From = fromAddress;
message.Priority = priority;
// To address collection of MailAddress
message.To.Add(toAddress);
message.Subject = subject;
if (!string.IsNullOrWhiteSpace(replyToAddress))
{
message.ReplyToList.Add(new MailAddress(replyToAddress));
}
if (!string.IsNullOrWhiteSpace(ccAddress))
{
message.CC.Add(ccAddress);
}
if (!string.IsNullOrWhiteSpace(bccAddress))
{
message.Bcc.Add(bccAddress);
}
//Specify true if it is html message
message.IsBodyHtml = isHtml;
// Message body content
message.Body = body;
// Send SMTP mail
smtpClient.Send(message);
}
}
return true;
}
catch (Exception ee)
{
throw ee;
}
}
}
You will need to ensure that you are setting correct mail server name, username and password. These things are generally provided by server folks or your IT department.
Using send email method EmailSender.SendEmail(toEmailId, replyToEmailId, ccEmailId, bccEmailId, subject, bodyOfEmail, System.Net.Mail.MailPriority.High, true);
You can change the MailPriority based on your requirement. Declare all above variables and set its value.
If you want a complete description of how things are working, here is a great tutorials
http://www.dotnetfunda.com/articles/show/1106/sending-email-with-attachment-in-aspnet.
Thanks, hope this helps.
Regards,
Sheo Narayan
http://www.dotnetfunda.com
Dev080692, if this helps please login to Mark As Answer. | Alert Moderator