@manhnguyenv

Welcome to my blog!

Send Mail using Google Account

October 30, 2019 22:58

Send Mail using Google Account (edit)

namespace ConsoleApp1
{
    using System;
    using System.IO;
    using System.Net.Mail;
    using System.Net.Mime;
 
    class Email
    {
        public static void Main(string[] args)
        {
            //You should enable application to access gmail account
 
            MailMessage m = new MailMessage();
            SmtpClient sc = new SmtpClient();
            try
            {
                m.From = new MailAddress("From@gmail.com""From");
                m.To.Add(new MailAddress("To@gmail.com""To"));
                m.CC.Add(new MailAddress("CC@gmail.com""Cc"));
 
                //Similarly BCC
                m.Subject = "Test1";
                m.IsBodyHtml = true;
                m.Body = "This is a Test Mail";
 
                FileStream fs = new FileStream("D:\\Downloads\\Test.pdf",
                                   FileMode.Open, FileAccess.Read);
                Attachment a = new Attachment(fs, "Test.pdf",
                                   MediaTypeNames.Application.Octet);
                m.Attachments.Add(a);
 
 
                string str = "<html><body><h1>Picture</h1><br/><img src =\"cid:image1\"></body></html>";
                AlternateView av = AlternateView.CreateAlternateViewFromString(str,
                             nullMediaTypeNames.Text.Html);
                LinkedResource lr = new LinkedResource("D:\\Downloads\\Test.jpg",
                             MediaTypeNames.Image.Jpeg);
                lr.ContentId = "image1";
                av.LinkedResources.Add(lr);
                m.AlternateViews.Add(av);
 
 
                sc.Host = "smtp.gmail.com";
                sc.Port = 587;
 
                sc.Credentials = new System.Net.NetworkCredential("From@gmail.com""YourPassword");
 
                sc.EnableSsl = true;
                sc.Send(m);
 
                Console.ReadLine();
 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
    }
}

Signing in with 2-Step Verification

 

 

How to send email from C#

April 25, 2018 22:28

How to send email from C#

http://csharp.net-informations.com/communications/csharp-smtp-mail.htm

https://stackoverflow.com/questions/9201239/send-e-mail-via-smtp-using-c-sharp

 

using System.Net.Mail;

...

MailMessage mail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

Categories

Recent posts