C# 为什么我在发送 SMTP 电子邮件时收到“'property cannot beassigned”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9201239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:20:55  来源:igfitidea点击:

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

c#emailsmtpclient

提问by Sam Stephenson

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 = new SmtpClient();            
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.To = "[email protected]"; // <-- this one
mail.From = "[email protected]";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

采纳答案by MRB

mail.Toand mail.Fromare readonly. Move them to the constructor.

mail.To并且mail.From是只读的。将它们移动到构造函数。

using System.Net.Mail;

...

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
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);

回答by Mithrandir

This :

这个 :

mail.To = "[email protected]";

Should be:

应该:

mail.To.Add(new MailAddress("[email protected]"));

回答by Sam Stephenson

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("[email protected]","password");

MailMessage mm = new MailMessage("[email protected]", "[email protected]", "test", "test");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

client.Send(mm);

sorry about poor spelling before

抱歉之前拼写不好

回答by Mnyikka

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("[email protected]", "password");
    client.Send(Message); 
}

回答by user2332898

This is how it works for me. Hope you find it useful

这就是它对我的工作方式。希望你觉得它有用

MailMessage objeto_mail = new MailMessage();
SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "smtp.internal.mycompany.com";
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("user", "Password");
objeto_mail.From = new MailAddress("[email protected]");
objeto_mail.To.Add(new MailAddress("[email protected]"));
objeto_mail.Subject = "Password Recover";
objeto_mail.Body = "Message";
client.Send(objeto_mail);

回答by somesh

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 articlefor more details

阅读这篇文章了解更多详情

回答by DoodleKana

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.

如果您不希望您的电子邮件和密码出现在您的代码中,并希望您的公司电子邮件客户端服务器使用您的 Windows 凭据,请使用下面的方法。

client.Credentials = CredentialCache.DefaultNetworkCredentials;

Source

来源

回答by Pankaj Prakash

Initialize the MailMessagewith sender and reciever's email addresses. It should be something like

MailMessage使用发件人和收件人的电子邮件地址初始化。它应该是这样的

string from = "[email protected]";  //Senders email   
string to = "[email protected]";  //Receiver's email  
MailMessage msg = new MailMessage(from, to); 

Read the full code snippet of how to send emails in c#

阅读如何在 c# 中发送电子邮件的完整代码片段

回答by user3188978

this would work too..

这也行..

string your_id = "[email protected]";
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, "[email protected]", "subject", "body");
   client.Send(mm);
   Console.WriteLine("Email Sent");
 }
 catch (Exception e)
 {
   Console.WriteLine("Could not end email\n\n"+e.ToString());
 }

回答by jishan siddique

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

观看视频:https : //www.youtube.com/watch?v=bUUNv-19QAI