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

前端 未结 17 2477
醉话见心
醉话见心 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:41

    this would work too..

    string your_id = "your_id@gmail.com";
    string your_password = "password";
    try
    {
       SmtpClient client = new SmtpClient
       {
         Host = "smtp.gmail.com",
         Port = 587,
         EnableSsl = true,
         DeliveryMethod = SmtpDeliveryMethod.Network,
         Credentials = new System.Net.NetworkCredential(your_id, your_password),
         Timeout = 10000,
       };
       MailMessage mm = new MailMessage(your_iD, "recepient@gmail.com", "subject", "body");
       client.Send(mm);
       Console.WriteLine("Email Sent");
     }
     catch (Exception e)
     {
       Console.WriteLine("Could not end email\n\n"+e.ToString());
     }
    

提交回复
热议问题