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

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

    This :

    mail.To = "user@hotmail.com";
    

    Should be:

    mail.To.Add(new MailAddress("user@hotmail.com"));
    
    0 讨论(0)
  • 2020-11-22 06:53

    mail.To and mail.From are readonly. Move them to the constructor.

    using System.Net.Mail;
    
    ...
    
    MailMessage mail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
    SmtpClient client = new SmtpClient();
    client.Port = 25;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Host = "smtp.gmail.com";
    mail.Subject = "this is a test email.";
    mail.Body = "this is my test email body";
    client.Send(mail);
    
    0 讨论(0)
  • 2020-11-22 06:53
     //Hope you find it useful, it contain too many things
    
        string smtpAddress = "smtp.xyz.com";
        int portNumber = 587;
        bool enableSSL = true;
        string m_userName = "support@xyz.com";
        string m_UserpassWord = "56436578";
    
        public void SendEmail(Customer _customers)
        {
            string emailID = gghdgfh@gmail.com;
            string userName = DemoUser;
    
            string emailFrom = "qwerty@gmail.com";
            string password = "qwerty";
            string emailTo = emailID;
    
            // Here you can put subject of the mail
            string subject = "Registration";
            // Body of the mail
            string body = "<div style='border: medium solid grey; width: 500px; height: 266px;font-family: arial,sans-serif; font-size: 17px;'>";
            body += "<h3 style='background-color: blueviolet; margin-top:0px;'>Aspen Reporting Tool</h3>";
            body += "<br />";
            body += "Dear " + userName + ",";
            body += "<br />";
            body += "<p>";
            body += "Thank you for registering </p>";            
            body += "<p><a href='"+ sURL +"'>Click Here</a>To finalize the registration process</p>";
            body += " <br />";
            body += "Thanks,";
            body += "<br />";
            body += "<b>The Team</b>";
            body += "</div>";
           // this is done using  using System.Net.Mail; & using System.Net; 
            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(emailFrom);
                mail.To.Add(emailTo);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                // Can set to false, if you are sending pure text.
    
                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:54

    send email by smtp

    public void EmailSend(string subject, string host, string from, string to, string body, int port, string username, string password, bool enableSsl)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient smtpServer = new SmtpClient(host);
                mail.Subject = subject;
                mail.From = new MailAddress(from);
                mail.To.Add(to);
                mail.Body = body;
                smtpServer.Port = port;
                smtpServer.Credentials = new NetworkCredential(username, password);
                smtpServer.EnableSsl = enableSsl;
                smtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    
    0 讨论(0)
  • 2020-11-22 06:56

    MailKit is an Open Source cross-platform .NET mail-client library that is based on MimeKit and optimized for mobile devices. It has more and advance features better than System.Net.Mail Microsoft TNEF support via MimeKit.

    Download nuget package from here.

    See this example you can send mail

                MimeMessage mailMessage = new MimeMessage();
                mailMessage.From.Add(new MailboxAddress(senderName, sender@address.com));
                mailMessage.Sender = new MailboxAddress(senderName, sender@address.com);
                mailMessage.To.Add(new MailboxAddress(emailid, emailid));
                mailMessage.Subject = subject;
                mailMessage.ReplyTo.Add(new MailboxAddress(replyToAddress));
                mailMessage.Subject = subject;
                var builder = new BodyBuilder();
                builder.TextBody = "Hello There";            
                try
                {
                    using (var smtpClient = new SmtpClient())
                    {
                        smtpClient.Connect("HostName", "Port", MailKit.Security.SecureSocketOptions.None);
                        smtpClient.Authenticate("user@name.com", "password");
    
                        smtpClient.Send(mailMessage);
                        Console.WriteLine("Success");
                    }
                }
                catch (SmtpCommandException ex)
                {
                    Console.WriteLine(ex.ToString());              
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());                
                }
    
    0 讨论(0)
提交回复
热议问题