zoukankan      html  css  js  c++  java
  • ASP.NET Email + WebConfig

    这里演示如果把 Email provider 的资料写在 WebConfig 里和调用它. 

    如果整个项目只需要使用一个 Email, 可以写入system.net里, 微软已经帮我们设计好了

    <configuration>
      <system.net>
        <mailSettings>
          <smtp deliveryMethod="Network" from="Stooges Web Design Default &lt;stooges@stooges.com.my&gt;">
            <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&lt;stooges@stooges.com.my&gt;">
          <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);
  • 相关阅读:
    Visual Studio 快捷键
    C#编程使用Managed Wifi API连接无线SSID
    C#两种创建快捷方式的方法
    COJ 1059
    [Unity3D]Unity3D游戏开发之鼠标滚轮实现放大缩小
    cloudstack4.4新增功能前瞻
    hdu 4635 Strongly connected (tarjan)
    freemarker声明变量
    Android 4.4 Kitkat Phone工作流程浅析(六)__InCallActivity显示更新流程
    评教,路上的风景更美
  • 原文地址:https://www.cnblogs.com/keatkeat/p/5405025.html
Copyright © 2011-2022 走看看