我正在尝试用 C# 发送电子邮件,但遇到了一些问题。每当我尝试发送电子邮件时,都会收到一条消息“错误:发送邮件失败”。这是我的代码:
try
{
client.Host = "smtp.gmail.com";
client.Port = 465;
client.UseDefaultCredentials = false;
client.Credentials = smtpCreds;
client.EnableSsl = true;
MailAddress sendFrom = new MailAddress("from@domain.com");
MailAddress sendTo = new MailAddress("to@domain.com");
MailMessage msg = new MailMessage(sendFrom,sendTo);
msg.Subject = "Subject";
msg.Body = "Body";
client.Send(msg);
}
catch (Exception e)
{
MessageBox.Show("Error:" + e.Message);
}
我也有这个声明:
public SmtpClient client = new SmtpClient();
public System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("mail", "password");
希望您能够帮助我。