这些守则有什么问题?它会抛出异常“{”发送邮件失败。“}”!我不知道为什么。请帮我。
编辑:内部异常:{“无法连接到远程服务器”}
string smtpAddress = "smtp.live.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "myemail@live.com";
string password = "myPassword";
string emailTo = "otherEmail@live.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = false;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
答案 0 :(得分:-1)
在发送邮件时删除使用关键字:
从:
using (SmtpClient smtpClient = new SmtpClient())
为:
SmtpClient smtpClient = new SmtpClient();
删除大括号也是这样的:
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);