Skip to main content

Sending email in asp.net with email body

This article is for sending email from asp.net with the use of gmail smtp server for that we have to required gmail credential,
asp.net provide SmtpClient class for connecting to the smtp server and MailMessage for getting mail requirement.

Here is a method named SendEmail it takes 7 arguments e.g to email address, from email address, subject of email, body of email, sending like a HTML or plan text, carbon copy email address.
just copy and paste the aspx.cs code and pass these parameter to SendEmail method, if you would like to create a body the call the function CreateEmail and pass the parameters.
 


/// <summary>
/// For sending mail with Username and newly generated password.
/// </summary>
/// <param name="to">Email Id of the User</param>
/// <param name="from">Email Id of the Sender("info@guru.com")</param>
/// <param name="subject">Subject of the mail(Username and password info you requested)</param>
/// <param name="body">Body of the mail</param>
/// <param name="fHtml">Mail Type</param>
/// <param name="fCC">true or false</param>
/// <param name="cc">CCed Email Id</param>
public static void SendEmail(string to, string from, string subject, string body, bool fHtml, bool fCC, string cc)
{
    try
    {
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(from);
        msg.To.Add(to);
        msg.Subject = subject;
        msg.Body = body;
        msg.IsBodyHtml = fHtml;
        if (fCC) msg.CC.Add(cc);
        //************for using smtp server of gmail******************
        SmtpClient smtp = new SmtpClient("smtp.gmail.com");
        smtp.Port = 587;
        //  Assign your username and password to connect to gmail
        smtp.Credentials = new NetworkCredential("GMailUsername", "GmailPassword");
        smtp.EnableSsl = true//  Enable SSL
        smtp.Send(msg);

    }
    catch { }
}





public void CreateEmail(string emailTo, string mailFrom)
{
    string link = "http://" + Request.ServerVariables["server_name"].ToString();
    if (Request.ServerVariables["SERVER_PORT"].ToString() != null && Request.ServerVariables["SERVER_PORT"].ToString() != "")
    {
        link += ":" + Request.ServerVariables["SERVER_PORT"].ToString().Trim() + "/buyerseller1";
    }
    link += "/Varification.aspx";
    //?vc=" + UserInfo.Tables[0].Rows[0]["Aid"].ToString().Trim() + "&un=" + username.Text.Trim();

    StringBuilder body = new StringBuilder();
    //Creating Mail body.
    body.Append("<span style='font-family:Courier New;font-size:13px'> Dear ");
    body.Append("User");
    body.Append(":<br /><br />");
    body.Append("Please Click on the following link To activate Your Account:<br />");
    body.Append("<a href='" + link + "'>Click Here</a>");
    body.Append("<br />");
    //body.Append("Password: ");
    //body.Append(Password.ToString());
    //body.Append("<br /><br />");
    //body.Append("<hr/><br />");
    //body.Append("How to sign in and reset your password:<br />");
    //body.Append("1. Go to the Sign in page: http://");
    //body.Append(Request.ServerVariables["server_name"].ToString());
    //body.Append("/expert/login.aspx<br />");
    //body.Append("2. Enter your username: ");
    //body.Append(UserName.ToString());
    //body.Append("<br />");
    //body.Append("3. Enter your password: ");
    //body.Append(Password.ToString());
    //body.Append("<br />");
    //body.Append("4. Click the sign in button. <br />");
    //body.Append("<br />");
    //body.Append("<hr/><br />");
    //body.Append("Sincerely, <br />");
    //body.Append("Comparison Team</span>");
    SendEmail(emailTo, mailFrom, "Activation Profile", body.ToString(), true, false, "");
}
}



Popular posts from this blog

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

Customize comment box in blogger

Now it is possible to customize your blogger comment box in simple way there no need to edit HTML of your blogger just through the adding some simple css to the blogger.

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview , Scrollble gridview with fixed header through javascript , File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview. We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.