C#: How to send email by using your gmail account by c#.

For sending email by c# using google smtp server and your gmail account you can send email.For this you must have to provide your gmail credential.

public bool SendMail(string to, string from, string subject, string msgBody)
        {
            bool mailSendStatus = false;

            try
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.From = new MailAddress(from, "from Name");
                message.To.Add(new MailAddress(to));
                message.Subject = subject;
                message.Priority = System.Net.Mail.MailPriority.High;
                message.IsBodyHtml = true;
                message.Sender = new MailAddress(from, "Sender Name");
                message.Body = msgBody;

                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "password");

                smtp.Send(message);
                mailSendStatus = true;
            }
            catch
            {
                mailSendStatus = false;
            }
            return mailSendStatus;
        }

And alternatively instead of hardcoding smtp settings you can keep in web config file in below format.

<system.net>
      <mailSettings>
        <smtp from="codeDestination@gmail.com" deliveryMethod="Network">
          <network enableSsl="true" defaultCredentials="false"
		        host="smtp.gmail.com" port="587"
			userName="myaccountuesrname@gmail.com"
			password="mypassword" />
        </smtp>
      </mailSettings>
</system.net>

If you use smtp settings in web config then sendEmail function will look like

        // to : Adress of person whom you are sending email
 	public bool SendMail(string to, string from, string subject, string msgBody)
        {
            bool mailSendStatus = false;

            try
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.From = new MailAddress(from, "from Name");
                message.To.Add(new MailAddress(to));
                message.Subject = subject;
                message.Priority = System.Net.Mail.MailPriority.High;
                message.IsBodyHtml = true;
                message.Sender = new MailAddress(from, "Sender Name");
                message.Body = msgBody;

                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

                smtp.Send(message);
                mailSendStatus = true;
            }
            catch
            {
                mailSendStatus = false;
            }
            return mailSendStatus;
        }

page keyword: Send Email via C# through Google Apps account,.net – Sending email through Gmail SMTP server with C# How to send email in asp.net (c#) using google or gmail smtp server, send email from c# code

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s