你要用QQ邮件发邮件就配置邮件服务器为QQ邮箱的邮件服务器mail.qq.com.然后FromAddress那里配置成你的邮件地址,objSmtpClient.Credentials 需要你的邮箱和密码。方法 SendMail(string mailAddress, string mailTitle, string mailContent)需要参数mailAddress为你要发送的邮箱地址,mailTitle为邮件主题,mailContent为邮件内容。还需要引用:using System.Net.Mail; public static void SendMail(string mailAddress, string mailTitle, string mailContent) { MailMessage objMailMessage = new MailMessage(); string fromAddress = ConfigurationManager.AppSettings["FromAddress"];//你在web.config中配置的发件人地址,就是你的邮箱地址。 string mailHost = ConfigurationManager.AppSettings["MailHost"];//邮件服务器,如mail.qq.com objMailMessage.From = new MailAddress(fromAddress);//发送方地址 objMailMessage.To.Add(new MailAddress(mailAddress));//收信人地址 objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;//邮件编码 objMailMessage.Subject = mailTitle;//邮件标题 objMailMessage.Body = mailContent;//邮件内容 objMailMessage.IsBodyHtml = true;//邮件正文是否为html格式 SmtpClient objSmtpClient = new SmtpClient(); objSmtpClient.Host = mailHost;//邮件服务器地址 objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//通过网络发送到stmp邮件服务器 objSmtpClient.Credentials = new System.Net.NetworkCredential();//发送方的邮件地址,密码 //objSmtpClient.EnableSsl = true;//SMTP 服务器要求安全连接需要设置此属性 try { objSmtpClient.Send(objMailMessage); } catch (Exception ex) { LogHelper.WriteLog(LogFile.Error, ex.ToString());//记录错误日志 } }
时间: 2024-10-28 11:00:51