Skip to main content

Sending emails in asp.net using Gmail Credentials

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.



public static class EMailUtility
{
     
/// <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 { }
}
}

Popular posts from this blog

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

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-".

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.