出于调试目的,我已全局处理所有异常。每当发生异常时,我都会默默处理它,并希望向自己发送一封包含错误详细信息的电子邮件,以便我可以解决这个问题。
我有两封电子邮件,email1 @ gmail.com和email2@gmail.com ......
我尝试使用此代码向自己发送电子邮件,但它无效。
string to = "email1@gmail.com";
string from = "email2@gmail.com";
string subject = "an error ocurred";
string body = e.ToString();
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.google.com");
client.Timeout = 100;
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
我尝试了无数其他代码,但我不知道该怎么做。有没有人有这个坚实的解决方案?非常感谢。
答案 0 :(得分:2)
这必须奏效。在此处查看更多信息:Sending email in .NET through Gmail
using System.Net;
using System.Net.Mail;
//...
var fromAddress = new MailAddress("alextodorov01@abv.bg", "From Name");
var toAddress = new MailAddress("kozichka01@abv.bg", "To Name");
const string fromPassword = "fromPassword";
const string subject = "an error ocurred";
const string body = e.ToString();
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
//...