Send Mail Using ASP.Net
public static bool Send(string ToAddr, string Subject, string Body)
{
try
{
MailMessage Msg = new MailMessage();
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "youremail@gmail.com");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
Msg.To = ToAddr;
Msg.From = "youremail@gmail.com";
Msg.Subject = Subject;
Msg.BodyFormat = MailFormat.Html;
Msg.Body = Body;
Msg.Priority = System.Web.Mail.MailPriority.High;
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";
SmtpMail.Send(Msg);
return true;
}
catch (Exception ex)
{
return false;
}
}
public static string SQLFix(string textData)
{
return textData.Trim().Replace("'", "''");
}
protected void btnSend_Click(object sender, EventArgs e)
{
if (Send(SQLFix(txtTo.Text), SQLFix(txtSubject.Text), SQLFix(txtMessage.Text)))
{
lblmsg.Text = "Email Successfully Send";
}
else
{
lblmsg.Text = "Problem with Sending Email , Try after some time";
}
}
{
try
{
MailMessage Msg = new MailMessage();
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "youremail@gmail.com");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
Msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
Msg.To = ToAddr;
Msg.From = "youremail@gmail.com";
Msg.Subject = Subject;
Msg.BodyFormat = MailFormat.Html;
Msg.Body = Body;
Msg.Priority = System.Web.Mail.MailPriority.High;
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";
SmtpMail.Send(Msg);
return true;
}
catch (Exception ex)
{
return false;
}
}
public static string SQLFix(string textData)
{
return textData.Trim().Replace("'", "''");
}
protected void btnSend_Click(object sender, EventArgs e)
{
if (Send(SQLFix(txtTo.Text), SQLFix(txtSubject.Text), SQLFix(txtMessage.Text)))
{
lblmsg.Text = "Email Successfully Send";
}
else
{
lblmsg.Text = "Problem with Sending Email , Try after some time";
}
}
Comments
Post a Comment