Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to send an email in. NET through Gmail

What I have tried:

using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("email@gmail.com");
mail.To.Add("somebody@domain.com");
mail.Subject = "Hello World";
mail.Body = "

Hello

";
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment("C:\\file.zip"));

using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("email@gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
Posted
Updated 20-Sep-18 22:28pm

This is what I learned,

using System;
using System.Net;
using System.Net.Mail;

namespace SendMailViaGmail
{
   class Program
   {
   static void Main(string[] args)
   {

    
      string SendersAddress = "Sendersaddress@gmail.com";
      string ReceiversAddress = "ReceiversAddress@yahoo.com";
      const string SendersPassword = "Password";
   const string subject = "Testing";
const string body = "Hi This is your friend ";

      try
      {
    
         SmtpClient smtp within{}
         smtp.gmail.com and port number is 587
        SmtpClient smtp = new SmtpClient
        {
           Host = "smtp.gmail.com",
           Port = 587,
           EnableSsl = true,
           DeliveryMethod = SmtpDeliveryMethod.Network,
           Credentials    = new NetworkCredential(SendersAddress, SendersPassword),
           Timeout = 3000
        };
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using Public_Uber.Models;

namespace Public_Uber.Controllers
{
    public class SendMailerController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
   
        // GET: SendMailer
        [HttpPost]
        public ActionResult Index(Public_Uber.Models.MailModel objModelMail, HttpPostedFileBase fileUploader)
        {
            if (ModelState.IsValid)
            {
                string from = "sthehfessor95@gmail.com"; //example:- sourabh9303@gmail.com
                using (MailMessage mail = new MailMessage(from, objModelMail.To))
                {

                    ExpandedUserDTO ob = new ExpandedUserDTO();
                    objModelMail.Body= "Your Username is:"+ob.UserName + "& Password:" + ob.Password;
                    mail.Subject = objModelMail.Subject;
                    mail.Body = objModelMail.Body;
                    if (fileUploader != null)
                    {
                        string fileName = Path.GetFileName(fileUploader.FileName);
                        mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
                    }
                    mail.IsBodyHtml = false;

                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    NetworkCredential networkCredential = new NetworkCredential(from, "Sithembiso95@");
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = networkCredential;
                    smtp.Port = 587;

                    smtp.Send(mail);
                    ViewBag.Message = "Sent";
                    TempData["Message"] = "<script>alert('Email Successfully Sent! ');</script>";

                    return View("Index", objModelMail);
                }
            }
            else
            {
                return View();
            }
        }
    }
}
C#

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900