.net core发送邮件。api,httpclient使用
电脑版发表于:2021/9/30 17:31
把你要发送邮件的账号,开启SMTP服务
设置-->账户-->开启SMTP服务
开启后就可以看到你的code,后面发送邮件的时候要用
代码如下,这里使用的webapi:
[Route("api/[controller]")] [ApiController] public class SendMailController : ControllerBase { [HttpPost] public IActionResult Post([FromBody] Mail mails) { //截取发件人邮箱地址从而判断Smtp的值 string[] sArray = mails.fromPerson.Split(new char[2] { '@', '.' }); if (sArray[1] == "qq") { mails.host = "smtp.qq.com";//如果是QQ邮箱则:smtp.qq.com,依次类推 163:smtp.163.com } else if (sArray[1] == "163") { mails.host = "smtp.163.com";//如果是QQ邮箱则:smtp.qq.com,依次类推 163:smtp.163.com } //将发件人邮箱带入MailAddress中初始化 MailAddress mailAddress = new MailAddress(mails.fromPerson); //创建Email的Message对象 MailMessage mailMessage = new MailMessage(); //判断收件人数组中是否有数据 if (mails.recipientArry.Any()) { //循环添加收件人地址 foreach (var item in mails.recipientArry) { if (!string.IsNullOrEmpty(item)) mailMessage.To.Add(item.ToString()); } } //判断抄送地址数组是否有数据 if (mails.mailCcArray != null && mails.mailCcArray.Any()) { //循环添加抄送地址 foreach (var item in mails.mailCcArray) { if (!string.IsNullOrEmpty(item)) mailMessage.To.Add(item.ToString()); } } //发件人邮箱 mailMessage.From = mailAddress; //标题 mailMessage.Subject = mails.mailTitle; //编码 mailMessage.SubjectEncoding = Encoding.UTF8; //正文 mailMessage.Body = mails.mailBody; //正文编码 mailMessage.BodyEncoding = Encoding.Default; //邮件优先级 mailMessage.Priority = MailPriority.High; //正文是否是html格式 mailMessage.IsBodyHtml = mails.isbodyHtml; //取得Web根目录和内容根目录的物理路径 string webRootPath = string.Empty; if (mails.files != null && mails.files.Count > 0) { //添加附件 foreach (IFormFile item in mails.files) { mailMessage.Attachments.Add(new Attachment(item.OpenReadStream(), item.FileName)); }; } //实例化一个Smtp客户端 SmtpClient smtp = new SmtpClient(); //将发件人的邮件地址和客户端授权码带入以验证发件人身份 smtp.Credentials = new System.Net.NetworkCredential(mails.fromPerson, mails.code); //指定SMTP邮件服务器 smtp.Host = mails.host; //邮件发送到SMTP服务器 smtp.Send(mailMessage); return new JsonResult(new { message = "发送成功", code = 200 }); } }
实体类:
public class Mail { /// <summary> /// 发送人 /// </summary> public string fromPerson { get; set; } /// <summary> /// 收件人地址(多人) /// </summary> public string[] recipientArry { get; set; } /// <summary> /// 抄送地址(多人) /// </summary> public string[] mailCcArray { get; set; } /// <summary> /// 标题 /// </summary> public string mailTitle { get; set; } /// <summary> /// 正文 /// </summary> public string mailBody { get; set; } /// <summary> /// 客户端授权码(可存在配置文件中) /// </summary> public string code { get; set; } /// <summary> /// SMTP邮件服务器 /// </summary> public string host { get; set; } /// <summary> /// 正文是否是html格式 /// </summary> public bool isbodyHtml { get; set; } /// <summary> /// 接收文件,附件 /// </summary> public List<IFormFile> files { get; set; } }
postman测试:
发送成功!
使用httpclient调用:
代码如下:
public void SendMail() { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var content = new { fromPerson = "发件人", mailTitle = "标题", mailBody = "正文内容", recipientArry = new List<string>() { "收件人1@qq.com", "收件人2@qq.com" }, }; StringContent strcontent = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json"); string result = client.PostAsync("http://localhost:59154/api/SendMail", strcontent).Result.Content.ReadAsStringAsync().Result; }