利用SmtpClient类发送邮件
以qq邮箱为例
1.首先去配置smtp服务(QQ邮箱默认的SMTP服务是关闭的,要自己去开)
首先打开QQ邮箱界面,点击设置》账户
2.代码配置部分
public ActionResult SendMali() { try { string _from = "发件人地址"; string to = "收件人地址"; string password = "**********";//授权码 SmtpClient mailClient = new SmtpClient("smtp.qq.com"); //设置邮箱端口,pop3端口:110, smtp端口是:25 mailClient.Port = 25; mailClient.EnableSsl = true; mailClient.UseDefaultCredentials = false; //Credentials登陆SMTP服务器的身份验证. mailClient.Credentials = new NetworkCredential(_from, password); //test@qq.com发件人地址、test@tom.com收件人地址 MailMessage message = new MailMessage(new MailAddress(_from), new MailAddress(to)); message.Body = "this is a Body!";//邮件内容 message.Subject = "this is a Subject";//邮件主题 string path = Server.MapPath("/upload/demo.rar"); Attachment a = new Attachment(path);//附件 message.Attachments.Add(a); mailClient.Send(message); return Json("发送成功"); } catch (Exception) { return Json("发送失败"); } }
3.SmtpClient类官方api https://msdn.microsoft.com/zh-cn/library/system.net.mail.smtpclient(v=vs.110).aspx