用System.Net.Mail的命名空间发邮件
难得项目空闲,用了点时间看了下.Net的Smtp类,略作随笔
首先当然是引用程序集:
System.Net
System.Net.Mail
然后需要实例化MailMessage对象:
MailAddress from = new MailAddress("from@163.com");
MailAddress to = new MailAddress("to@163.com");
MailMessage message = new MailMessage(from, to);
还有其他邮件属性,例如抄送, 暗送, 附件等, 详见MSDN
由于抄送属性是只读的,所以要这么实现:
message.CC.Add("CarbonCopies@163.com");
再然后需要实例化Smtp对象:
SmtpClient smtp = new SmtpClient("smtp.163.com", "25");
这里需要用户验证:
smtp.Credentials = new NetworkCredential("from@163.com", "Password");
NetworkCredential属于System.Net命名空间内
最后,就可以发送邮件啦~ 很简单
try
{
smtp.Send(message);
//Please output some information to make a easier understand for users
}
catch { do something.. }