This is a function to send Email from Azure Server Using Send Grid.
public Dictionary FogotPassword(String Email)
{
Dictionary row = new Dictionary();
string Password = string.Empty;
DataTable dt = new DataTable();
try
{
OpenConnection(); //Opening database connection
//BeginTransation();
cmd.Parameters.Clear();
cmd.CommandText = "SP_GetUserDetailByEmail"; //Calling stored procedure
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@EmailId", Email));
cmd.CommandTimeout = 0;
cmd.ExecuteNonQuery();
da.Fill(dt);
if ((dt.Rows.Count > 0))
{
MailMessage mailMsg = new MailMessage();
// To
mailMsg.To.Add(new MailAddress(Email, Email));
// From
mailMsg.From = new MailAddress("emailaddress", "flirtapp");
// Subject and multipart/alternative Body
mailMsg.Subject = "FlirtApp - Forgot Password";
string text = "text body";
string html = @"Dear User,
Your Password is " + dt.Rows[0]["Password"] + "
";
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
// Init SmtpClient and send
SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("azure_cred@azure.com", "Password");
smtpClient.Credentials = credentials;
smtpClient.Send(mailMsg);
FillMessage(row, true, Convert.ToString(dt.Rows[0]["UserID"]), "Password sent successfully!");
}
else
{
FillMessage(row, true, string.Empty, "EmailId does not exist");// Function to fill Dictionary object
}
return row;
}
catch (Exception ex)
{
FillMessage(row, false, string.Empty, string.Format("Error:{0}", ex.Message));// Function to fill Dictionary object
return row;
}
finally
{
CloseConnection();
}
}
0 Comment(s)