ASP.NET Email + WebConfig
这里演示如果把 Email provider 的资料写在 WebConfig 里和调用它.
如果整个项目只需要使用一个 Email, 可以写入system.net里, 微软已经帮我们设计好了
<configuration> <system.net> <mailSettings> <smtp deliveryMethod="Network" from="Stooges Web Design Default <stooges@stooges.com.my>"> <network host="mail.stooges.com.my" port="587" userName="stooges@stooges.com.my" password="password" enableSsl="false" /> </smtp> </mailSettings> </system.net> <configuration>
然后简单调用就可以了
SmtpClient smtp = new SmtpClient(); MailMessage mail = new MailMessage { Subject = "subject", Body = "html content", IsBodyHtml = true }; mail.To.Add("hengkeat87@gmail.com"); smtp.Send(mail);
如果有多个Email要使用,我们就得自己写webconfig然后掉用了 :
读 webconfig 资料可以参考: http://www.cnblogs.com/keatkeat/p/5404128.html
<configuration> <configSections> <sectionGroup name="mailSettings"> <section name="stooges" type="System.Net.Configuration.SmtpSection"/> </sectionGroup> </configSections> <mailSettings> <stooges deliveryMethod="Network" from="Stooges Web Design<stooges@stooges.com.my>"> <network host="mail.stooges.com.my" port="587" userName="stooges@stooges.com.my" password="password" enableSsl="false" /> </stooges> </mailSettings> </configuration> SmtpClient smtp = new SmtpClient(); SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("mailSettings/stooges"); SmtpNetworkElement network = smtpSection.Network; smtp.Host = network.Host; smtp.Port = network.Port; smtp.EnableSsl = network.EnableSsl; smtp.UseDefaultCredentials = network.DefaultCredentials; smtp.Credentials = new NetworkCredential(network.UserName, network.Password); string from = smtpSection.From; //Stooges Web Design<stooges@stooges.com.my> int ipos = from.IndexOf("<"); string displayName = from.Substring(0, ipos); string email = from.Substring(ipos + 1, from.Length - displayName.Length - 2); MailMessage mail = new MailMessage { From = new MailAddress(email, displayName), Subject = "subject", Body = "html content", IsBodyHtml = true }; mail.To.Add("hengkeat87@gmail.com"); smtp.Send(mail);
分类:
ASP.NET
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· 【全网最全教程】使用最强DeepSeekR1+联网的火山引擎,没有生成长度限制,DeepSeek本体