【原】邮件发送代码
代码
//要引入下面两个命名空间
using System.Net;
using System.Net.Mail ;
///error是传进来的错误信息
public string aaa(string error)
{
//发件人邮箱,就是自己的邮箱啦!
MailAddress from = new MailAddress("from@126.com");
//收件人的邮箱
MailAddress to = new MailAddress("to@126.com");
MailMessage message = new MailMessage(from, to);
//邮件标题
message.Subject = "某某网站在-"+System .DateTime .Now .ToString ()+"-发生异常";
//邮件内容
message.Body = error ;
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//邮件端口,用25就行,默认就是25啦
client.Port = 25;
//smtp邮件发送服务器,要视你的发件邮箱而定
client.Host = "smtp.126.com";
//邮箱的用户名和密码
client.Credentials = new System.Net.NetworkCredential("from", "xxxxxx");
try
{
client.Send(message);
return "发送成功!";
}
catch (Exception ex)
{
return ex.ToString();
}
}
using System.Net;
using System.Net.Mail ;
///error是传进来的错误信息
public string aaa(string error)
{
//发件人邮箱,就是自己的邮箱啦!
MailAddress from = new MailAddress("from@126.com");
//收件人的邮箱
MailAddress to = new MailAddress("to@126.com");
MailMessage message = new MailMessage(from, to);
//邮件标题
message.Subject = "某某网站在-"+System .DateTime .Now .ToString ()+"-发生异常";
//邮件内容
message.Body = error ;
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//邮件端口,用25就行,默认就是25啦
client.Port = 25;
//smtp邮件发送服务器,要视你的发件邮箱而定
client.Host = "smtp.126.com";
//邮箱的用户名和密码
client.Credentials = new System.Net.NetworkCredential("from", "xxxxxx");
try
{
client.Send(message);
return "发送成功!";
}
catch (Exception ex)
{
return ex.ToString();
}
}