vs2003
vs2005
public static bool SendMail(string from, string to, string subject, string body, string smtpServer, string userName, string password, ref string result)
{
MailMessage mailMsg = new MailMessage();
//设置正文格式
mailMsg.BodyFormat = MailFormat.Html;
//设置收件人的邮件地址
mailMsg.To = to;
//设置发送者的邮件地址
mailMsg.From = from;
//设置邮件主题
mailMsg.Subject = subject;
//设置邮件内容
mailMsg.Body = body;
//设置支持服务器验证
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置用户名
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
//设置用户密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
try
{
//设置发送邮件服务器
SmtpMail.SmtpServer = smtpServer;
//发送邮件
SmtpMail.Send(mailMsg);
return true;
}
catch (Exception err)
{
result = err.Message.ToString();
return false;
}
}
{
MailMessage mailMsg = new MailMessage();
//设置正文格式
mailMsg.BodyFormat = MailFormat.Html;
//设置收件人的邮件地址
mailMsg.To = to;
//设置发送者的邮件地址
mailMsg.From = from;
//设置邮件主题
mailMsg.Subject = subject;
//设置邮件内容
mailMsg.Body = body;
//设置支持服务器验证
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置用户名
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
//设置用户密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
try
{
//设置发送邮件服务器
SmtpMail.SmtpServer = smtpServer;
//发送邮件
SmtpMail.Send(mailMsg);
return true;
}
catch (Exception err)
{
result = err.Message.ToString();
return false;
}
}
vs2005
public void SendEmail()
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.163.com");
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("from@163.com", "888999");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("from@163.com", "to@gmail.com", "subject ", "body");
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.163.com");
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("from@163.com", "888999");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("from@163.com", "to@gmail.com", "subject ", "body");
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}