How to send an email through gmail without enabling 'insecure access'?

后端 未结 6 697
你的背包
你的背包 2020-12-12 17:35

Google are pushing us to improve the security of script access to their gmail smtp servers. I have no problem with that. In fact I\'m happy to help.

But they\'re n

6条回答
  •  青春惊慌失措
    2020-12-12 18:06

      protected string SendEmail(string toAddress, string subject, string body)
        {
            string result = "Message Sent Successfully..!!";
    
            string senderID = "...........";// use sender's email id here..
            const string senderPassword = "........."; // sender password here...
    
            try
            {
                SmtpClient smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com", // smtp server address here...
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
                    Timeout = 30000,
    
                };
    
                MailMessage message = new MailMessage(senderID, toAddress, subject, body);
    
                smtp.Send(message);
            }
            catch (Exception ex)
            {
                result = "Error sending email.!!!";
            }
    
            return result;
        }
    

提交回复
热议问题