c#发送邮件

//from是发送的邮箱地址
//to是指发送的目标邮箱
///subject是标题
///body是发送的内容,注意相关词汇可能会导致当垃圾邮箱处理
private void SendMessage(string from, string to, string subject, string body)
{ System.Net.Mail.MailMessage mm = null; SmtpClient Client; Client = new SmtpClient { Host = "smtp.163.com",//QQ的是smtp.qq.com Port = 25,//端口测试和QQ的都是25测试成功 DeliveryMethod = SmtpDeliveryMethod.Network }; Client.UseDefaultCredentials = false; Client.Credentials = new NetworkCredential("***@163.com", "smtp服务的密码");//在设置里面开启会有密码提示QQ同理

try
{
mm = new System.Net.Mail.MailMessage(from, to, subject, body);
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mm.BodyEncoding = System.Text.Encoding.UTF8;
mm.IsBodyHtml = true;
Client.Send(mm);
isSent = true;

} catch (Exception ex)
{

//process ex.message

} finally {
mm.Dispose();

}


}

 

posted @ 2015-12-13 22:51  objnet  阅读(181)  评论(0)    收藏  举报