163和腾讯企业邮箱 发邮件,多个附件
1、腾讯企业邮箱发送 报错:mail from address must be same as authorization user
用账号密码登录,不要用微信扫。进入后:点 设置——邮箱绑定——生成新密码。用新密码代替原登录密码。因为这里显示“关闭安全登录”,说明已经开启了安全登录。
2、465 端口发送邮件
/// <summary> /// 465 端口发送邮件 带附件 /// </summary> /// <param name="mailHost">邮件服务器</param> /// <param name="mailPort">端口</param> /// <param name="strSendMail">发送邮件的用户名</param> /// <param name="mailPassword">发送邮件的密码</param> /// <param name="strToList">收件人邮箱地址</param> /// <param name="file">附件地址</param> /// <param name="subject">邮件主题</param> /// <param name="body">内容</param> public static void Sendmail(string mailHost, string strSendMail, string mailPassword, string strToList, string subject = "", string body = "", List<string> file = null) { try { System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage(); //验证 mmsg.Subject = subject;// "zhuti1";//邮件主题 mmsg.BodyFormat = System.Web.Mail.MailFormat.Html; mmsg.Body = body;// "wqerwerwerwer";//邮件正文 mmsg.BodyEncoding = Encoding.UTF8;//正文编码 mmsg.Priority = System.Web.Mail.MailPriority.High;//优先级 //添加邮件附件,可发送多个文件 if (file != null && file.Count() > 0) { foreach (var filename in file) { System.Web.Mail.MailAttachment mailAttach_1 = new System.Web.Mail.MailAttachment(filename); mmsg.Attachments.Add(mailAttach_1); } } //发件者邮箱地址 mmsg.From = strSendMail; mmsg.To = strToList;//收件人收箱地址 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//身份验证 //登陆名 邮箱登录账号,这里跟前面的发送账号一样就行 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mmsg.From); //登陆密码 这个密码要注意:如果是一般账号,要用授权码;企业账号用登录密码 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", mailPassword); mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口 //SSL加密 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); ////企业账号用smtp.exmail.qq.com System.Web.Mail.SmtpMail.SmtpServer = mailHost; System.Web.Mail.SmtpMail.Send(mmsg); } catch (Exception ex) { throw new Exception("邮箱异常!" + ex.Message); } }
3、多附件发送
/// <summary> /// 发送邮件,提供调用 /// </summary> /// <param name="strSendToList">收件人,多个收件人用做好事隔开</param> /// <param name="strSubject">主题</param> /// <param name="strBody">内容</param> /// <param name="strAttachmentsPath">附件路径</param> /// <param name="strOutErr">错误信息</param> /// <returns></returns> public bool SendMail(string strSendToList, string strSubject, string strBody, string[] arrAttachmentsPath, out string strOutErr) { //发送邮件服务器 string strHost = System.Configuration.ConfigurationManager.AppSettings["MailHost"].ToString(); //发件人地址 string strUserMail = System.Configuration.ConfigurationManager.AppSettings["MailSender"].ToString(); //发件人密码 string strUserPwd = System.Configuration.ConfigurationManager.AppSettings["MailSenderPwd"].ToString(); string ss = string.Empty; return SendMail(strHost, strUserMail, strUserPwd, strSendToList, strSubject, strBody, arrAttachmentsPath,out strOutErr); } /// <summary> /// 发邮件 /// </summary> /// <param name="strMailService">邮箱服务器</param> /// <param name="strSendMail">发件人邮箱</param> /// <param name="strSendMailPwd">发件人密码</param> /// <param name="strToList">收件人,多个收件人用做好事隔开</param> /// <param name="strSubject">主题</param> /// <param name="strMessage">邮件内容</param> /// <param name="AttachFile">附件路径</param> public bool SendMail(string strMailService, string strSendMail, string strSendMailPwd, string strToList, string strSubject, string strMessage, string[] AttachFile, out string strOutErr) { System.Net.Mail.SmtpClient client = new SmtpClient(strMailService); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(strSendMail, strSendMailPwd); client.DeliveryMethod = SmtpDeliveryMethod.Network; System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strSendMail, strToList); message.BodyEncoding = System.Text.Encoding.UTF8; message.Subject = strSubject; message.Body = strMessage; message.IsBodyHtml = true; //在有附件的情况下添加附件 if (AttachFile != null && AttachFile.Length > 0) { foreach (string path in AttachFile) { var attachFile = new Attachment(path); message.Attachments.Add(attachFile); } } try { client.Send(message); strOutErr = string.Empty; } catch (Exception err) { strOutErr = err.Message; return false; } return true; }
4. C#代码直接发送
腾讯企业邮箱端口号用:25 、用:465有时会发送不成功。
1)163发送:
SmtpClient smtp = new SmtpClient("smtp.163.com", 25);
2)腾讯企业邮箱发送:
POP3/SMTP协议
POP3/SMTP协议:
接收邮件服务器:pop.exmail.qq.com (端口 110),使用SSL,端口号995
发送邮件服务器:smtp.exmail.qq.com (端口 25),使用SSL,端口号465或587
IMAP协议:
接收邮件服务器:imap.exmail.qq.com (端口 143),使用SSL,端口号993
发送邮件服务器:smtp.exmail.qq.com (端口 25),使用SSL,端口号465或587
账户名:您的企业邮箱账户名,账户名需要填写完整的邮件地址
密码:您的企业邮箱密码
电子邮件地址:您的企业邮箱的完整邮件地址
海外用户可使用以下服务器
POP3/SMTP协议
接收邮件服务器:hwpop.exmail.qq.com ,使用SSL,端口号995
发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465
IMAP协议
接收邮件服务器:hwimap.exmail.qq.com ,使用SSL,端口号993
发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465
如下代码已验证可用:
/// <summary> /// 发送邮件 有问题 /// </summary> /// <param name="title">标题</param> /// <param name="content">内容</param> /// <param name="list">收件人</param> public static async void Send(string title, string content, params string[] list) { try { MailMessage message = new MailMessage { From = new MailAddress(address, name)//发送人邮箱 }; foreach (var item in list) { message.To.Add(item);//收件人地址 } message.Subject = title;//发送邮件的标题 message.Body = content;//发送邮件的内容 //配置smtp服务地址 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient { Host = host, Port = port,//端口 腾讯企业邮箱端口号用:25 、用:465有时会发送不成功。 EnableSsl = enableSsl, //发送人邮箱与授权密码 Credentials = new NetworkCredential(address, authPwd) }; client.SendAsync(message, UserContext.Current.UserInfo.User_Id); } catch (Exception ex) { //这个日志可能也没有用。 Logger.Error("委托单发邮件失败!"+ex.Message); } }
调用:
StringBuilder sb= new StringBuilder(); sb.Append($@"{userInfo[0].UserTrueName} 你好!" + "\r\n\r\n"); sb.Append($@"车牌:{updateEntity.CarrierNo} 待审批,请尽快处理。" + "\r\n"); sb.Append($@"系统邮件,请勿回复!" + "\r\n\r\n"); Send("车辆备案审批", sb.ToString(), userInfo[0].Email);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构