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

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

    If you want to have your email and password not appear in your code and want your company email client server to use your windows credentials use below.

    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    

    Source

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-11-22 06:31
    public static void SendMail(MailMessage Message)
    {
        SmtpClient client = new SmtpClient();
        client.Host = "smtp.googlemail.com";
        client.Port = 587;
        client.UseDefaultCredentials = false;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = true;
        client.Credentials = new NetworkCredential("myemail@gmail.com", "password");
        client.Send(Message); 
    }
    
    0 讨论(0)
  • 2020-11-22 06:32
    MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text);
    mm.Subject = txtSubject.Text;
    mm.Body = txtBody.Text;
    if (fuAttachment.HasFile)//file upload select or not
    {
        string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
        mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
    }
    mm.IsBodyHtml = false;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);
    Response.write("Send Mail");
    

    View Video: https://www.youtube.com/watch?v=bUUNv-19QAI

    0 讨论(0)
  • 2020-11-22 06:36
    smtp.Host = "smtp.gmail.com"; // the host name
    smtp.Port = 587; //port number
    smtp.EnableSsl = true; //whether your smtp server requires SSL
    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
    smtp.Timeout = 20000;
    

    Go through this article for more details

    0 讨论(0)
  • 2020-11-22 06:37

    Finally got working :)

    using System.Net.Mail;
    using System.Text;
    
    ...
    
    // Command line argument must the the SMTP host.
    SmtpClient client = new SmtpClient();
    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    client.Timeout = 10000;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("user@gmail.com","password");
    
    MailMessage mm = new MailMessage("donotreply@domain.com", "sendtomyemail@domain.co.uk", "test", "test");
    mm.BodyEncoding = UTF8Encoding.UTF8;
    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    
    client.Send(mm);
    

    sorry about poor spelling before

    0 讨论(0)
提交回复
热议问题