Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Send email using gmail SMTP C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 388
    Comment on it

    This is a simple C# program that send email to recipient. It uses gmail smtp.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Mail;
    using System.Net;
    
    namespace SendEmail
    {
        class Program
        {
            static void Main(string[] args)
            {
                var fromAddress = new MailAddress("from@gmail.com", "From Name");//Email Address for from Email
                var toAddress = new MailAddress("to@example.com", "To Name");//Recipient address
                const string fromPassword = "fromPassword";//Gmail Password for user
                const string subject = "Subject";//Subject of email
                const string body = "Body";//Body content for email
    
    
                var smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)//Credentials of gmail user
                };
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    //IsBodyHtml=true,//Set it to true if your email body contains html content
                    Subject = subject,
                    Body = body
                })
                {
                    smtp.Send(message);
                }
            }
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: