SmtpClient mailClient = new SmtpClient();
mailClient.Host = "smtp.qq.com";
//Credentials登陆SMTP服务器的身份验证.
mailClient.Credentials = new NetworkCredential("test@qq.com", "123123123");
//通过network发送至smtp服务器
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
//myqq@qq.com 发件人地址、test@qq.com收件人地址
MailMessage message = new MailMessage(new MailAddress("test@qq.com"),new MailAddress("myqq@qq.com"));
message.Body = "Hello Word!";// 邮件内容
message.Subject = "this is a test";//邮件主题
//发送....
try
{
mailClient.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("邮件发送不成功,原因:" + ex);
}
上述代码能实现邮件的简单发送功能,需要注意的以下几点:
1.发件人的邮件必须开通smtp功能,否则程序会出异常,通常会显示mail from address must be ths same as authorization user
2.发件人地址不存在时,他会在认证时出现need EHLO and AUTH first异常