public bool GmailSendEmail(string Body, string MailTo, string subject)
{
bool blnSent = true;        
try
        {
            SmtpClient smtpClient = new SmtpClient();
            NetworkCredential basicCredential = new NetworkCredential("account@gmail.com", "password");
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress("no-reply@demo.com", "Title Mail System Notify");

            smtpClient.Host = "smtp.gmail.com";
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = basicCredential;
            smtpClient.EnableSsl = true;
            smtpClient.Port = 587;

            message.From = fromAddress;
            //message.CC = sCc;
            message.Subject = subject;

            //Set IsBodyHtml to true means you can send HTML email.
            message.IsBodyHtml = true;

            message.Priority = MailPriority.High;
            message.Body = Body;
            string[] mail = MailTo.Split(',');
            foreach (string s in mail)
            {
                if (s != "")
                {
                    message.Bcc.Add(new MailAddress(s));
                }
            }

            message.Sender = fromAddress;
            smtpClient.Send(message);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return blnSent;
    }

จากโค้ดข้างบนท่านจะส่งเมลย์ไม่ได้หากยังไม่เปิด “การเข้าถึงของแอปที่มีความปลอดภัยน้อย” โดยคลิกลิ้งด้านล่าง
https://www.google.com/settings/security/lesssecureapps
or
https://myaccount.google.com/lesssecureapps

TG Facebook Comments