Why do I get “'property cannot be assigned” when sending an SMTP email?

前端 未结 17 2279
醉话见心
醉话见心 2020-11-22 05:50

I can\'t understand why this code is not working. I get an error saying property can not be assigned

MailMessage mail = new MailMessage();
SmtpClient client          


        
17条回答
  •  悲&欢浪女
    2020-11-22 06:49

    First go to https://myaccount.google.com/lesssecureapps and make Allow less secure apps true.

    Then use the below code. This below code will work only if your from email address is from gmail.

    static void SendEmail()
        {
            string mailBodyhtml =
                "

    some text here

    "; var msg = new MailMessage("from@gmail.com", "to1@gmail.com", "Hello", mailBodyhtml); msg.To.Add("to2@gmail.com"); msg.IsBodyHtml = true; var smtpClient = new SmtpClient("smtp.gmail.com", 587); //**if your from email address is "from@hotmail.com" then host should be "smtp.hotmail.com"** smtpClient.UseDefaultCredentials = true; smtpClient.Credentials = new NetworkCredential("from@gmail.com", "password"); smtpClient.EnableSsl = true; smtpClient.Send(msg); Console.WriteLine("Email Sent Successfully"); }

提交回复
热议问题