今天作一个小项目,其中有个提醒功能,需要短信和邮件。因此查询了一下C#发送邮件,原来在学习计算机网络时了解到,其实就是实现SMTP协议和POP3协议,但是自己有没有服务器,于是利用126的邮箱制作。
代码如下:
MailMessage message = new MailMessage();
message.From = new MailAddress("XXX@126.com", "zhang");
message.To.Add(new MailAddress("XXX@hotmail.com"));
message.Subject = "TEST SMTP";
message.Body = "Hello World!";
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.126.com";
NetworkCredential nc = new NetworkCredential("XXX@126.com", "XXX");
smtp.Credentials = nc;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Send(message);
代码如下:
MailMessage message = new MailMessage();
message.From = new MailAddress("XXX@126.com", "zhang");
message.To.Add(new MailAddress("XXX@hotmail.com"));
message.Subject = "TEST SMTP";
message.Body = "Hello World!";
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.126.com";
NetworkCredential nc = new NetworkCredential("XXX@126.com", "XXX");
smtp.Credentials = nc;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Send(message);