++++++++Sending mail using C# and Async and Await .
*********** I have used this setting in Web.config file ***************
<add key="fromMail" value="donotreply@DomainName" />
<add key="actualId" value="0" />
<add key="SmtpServer" value="SmtpServer Name" />
<add key="Username" value="UsernameOfYourServer" />
<add key="Password" value="SmtpServerPassword" />
**************End here ******************************
$$$$$$$$$$$$$$$$ function for sending mail $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
public async Task<string> SendMail(string Body, string ToaddressEmailID, string Subject)
{
MailAddress toAddress = null;
MailAddress fromAddress = new MailAddress(ConfigurationManager.AppSettings["fromMail"].ToString());
if (ConfigurationManager.AppSettings["actualId"].ToString() == "0") // This for local testing , i have put 0 for local testing
{
toAddress = new MailAddress("ankit@gamil.com");
}
else
{
toAddress = new MailAddress(ToaddressEmailID);
}
var message = new MailMessage(fromAddress, toAddress);
message.Subject = Subject;
message.Body = Body;
message.IsBodyHtml = true;
message.Priority = MailPriority.High;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
string ServerName = ConfigurationManager.AppSettings["SmtpServer"].ToString(); // SMTP server name here you need to configur your server
string Username = ConfigurationManager.AppSettings["Username"].ToString(); // getting value from Web.config file as you can see in above code
string Password = ConfigurationManager.AppSettings["Password"].ToString();
using (var smtp = new SmtpClient(ServerName))
{
var credential = new NetworkCredential
{
UserName = Username,
Password = Password
};
smtp.Credentials = credential;
// smtp.EnableSsl = true; // if you want to enable ssl for security you can uncomment that line but you need SSL certificate
try
{
await smtp.SendMailAsync(message);
return "Successfully sent mail";
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
}
*********** I have used this setting in Web.config file ***************
<add key="fromMail" value="donotreply@DomainName" />
<add key="actualId" value="0" />
<add key="SmtpServer" value="SmtpServer Name" />
<add key="Username" value="UsernameOfYourServer" />
<add key="Password" value="SmtpServerPassword" />
**************End here ******************************
$$$$$$$$$$$$$$$$ function for sending mail $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
public async Task<string> SendMail(string Body, string ToaddressEmailID, string Subject)
{
MailAddress toAddress = null;
MailAddress fromAddress = new MailAddress(ConfigurationManager.AppSettings["fromMail"].ToString());
if (ConfigurationManager.AppSettings["actualId"].ToString() == "0") // This for local testing , i have put 0 for local testing
{
toAddress = new MailAddress("ankit@gamil.com");
}
else
{
toAddress = new MailAddress(ToaddressEmailID);
}
var message = new MailMessage(fromAddress, toAddress);
message.Subject = Subject;
message.Body = Body;
message.IsBodyHtml = true;
message.Priority = MailPriority.High;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
string ServerName = ConfigurationManager.AppSettings["SmtpServer"].ToString(); // SMTP server name here you need to configur your server
string Username = ConfigurationManager.AppSettings["Username"].ToString(); // getting value from Web.config file as you can see in above code
string Password = ConfigurationManager.AppSettings["Password"].ToString();
using (var smtp = new SmtpClient(ServerName))
{
var credential = new NetworkCredential
{
UserName = Username,
Password = Password
};
smtp.Credentials = credential;
// smtp.EnableSsl = true; // if you want to enable ssl for security you can uncomment that line but you need SSL certificate
try
{
await smtp.SendMailAsync(message);
return "Successfully sent mail";
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
}
No comments:
Post a Comment