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

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

    Just need to try this:

    string smtpAddress = "smtp.gmail.com";
    int portNumber = 587;
    bool enableSSL = true;
    string emailFrom = "companyemail";
    string password = "password";
    string emailTo = "Your email";
    string subject = "Hello!";
    string body = "Hello, Mr.";
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;
    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
    {
       smtp.Credentials = new NetworkCredential(emailFrom, password);
       smtp.EnableSsl = enableSSL;
       smtp.Send(mail);
    }
    

提交回复
热议问题