Sending Mail through SMTP and SMTP Configuration in web.config in ASP.NET
Sending mail through SMTP in ASp.net C#
In order to send mail from your SMTP server you must need a 3 classes in .net which are available in
using System.Net.Mail; name space i.e
MailMessage calss
NetworkCredentilas class
SMTPClient class
1st Step:
Configure SMTP details in Web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="test@yourDomin.com" >
<network host="serverIP/serverName" userName="test@yourDomin.com" password="******" enableSsl="false" port="25" />
</smtp>
</mailSettings>
</system.net>
2nd step
write a genaric method to send the mail in C#
public static bool SendEmail(string toEmailId,string subject,string body)
{
bool isSend = false;
try
{
string fromEmailId ="test@yourDomain.com";
MailMessage mail = new MailMessage(fromEmailId, toEmailId, subject, body);
mail.Bcc.Add(fromEmailId);
SmtpClient smtpclient = new SmtpClient();
mail.IsBodyHtml = true;
smtpclient.Send(mail);
isSend = true;
return isSend;
}
catch (Exception)
{
return isSend;
}
}
3rd Step:
Call The Method
Where ever you need send a mail just call this method
bool IsSend=SendMail("Tomail@gmail.com","your subject","your body message");
you can get the true or false value ismail sent or nor into your IsSend variable
Good luck......Happy Programming..
Posted By
Ram.....
In order to send mail from your SMTP server you must need a 3 classes in .net which are available in
using System.Net.Mail; name space i.e
MailMessage calss
NetworkCredentilas class
SMTPClient class
1st Step:
Configure SMTP details in Web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="test@yourDomin.com" >
<network host="serverIP/serverName" userName="test@yourDomin.com" password="******" enableSsl="false" port="25" />
</smtp>
</mailSettings>
</system.net>
2nd step
write a genaric method to send the mail in C#
public static bool SendEmail(string toEmailId,string subject,string body)
{
bool isSend = false;
try
{
string fromEmailId ="test@yourDomain.com";
MailMessage mail = new MailMessage(fromEmailId, toEmailId, subject, body);
mail.Bcc.Add(fromEmailId);
SmtpClient smtpclient = new SmtpClient();
mail.IsBodyHtml = true;
smtpclient.Send(mail);
isSend = true;
return isSend;
}
catch (Exception)
{
return isSend;
}
}
3rd Step:
Call The Method
Where ever you need send a mail just call this method
bool IsSend=SendMail("Tomail@gmail.com","your subject","your body message");
you can get the true or false value ismail sent or nor into your IsSend variable
Good luck......Happy Programming..
Posted By
Ram.....
Labels:
ASP.NET