我不明白为什么这个代码不能工作。我得到一个错误,说属性不能被分配
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 = "user@hotmail.com"; // <-- this one
mail.From = "you@yourcompany.example";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
通过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);
}
}
通过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);
}
}