zoukankan      html  css  js  c++  java
  • 如何在.NET 中简单使用SMTP发送邮件?

    public class Mail
    {
    #region 邮件参数
    static public string accountName = System.Configuration.ConfigurationManager.AppSettings["SmtpAccountName"];
    static public string password = System.Configuration.ConfigurationManager.AppSettings["SmtpAccountPW"];
    static public string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
    static public int smtpPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);
    #endregion

    /// <summary>
    /// 邮件发送方法一
    /// </summary>
    /// <param name="sendTo"></param>
    /// <param name="subject"></param>
    /// <param name="body"></param>
    static public void SendMail(string sendTo, string subject, string body)
    {
    //.net smtp
    System.Web.Mail.MailMessage mailmsg = new System.Web.Mail.MailMessage();
    mailmsg.To
    = sendTo;
    //mailmsg.Cc = cc;
    mailmsg.Subject = subject;
    mailmsg.Body
    = body;
    mailmsg.BodyFormat
    = MailFormat.Html;


    //sender here
    mailmsg.From = Mail.accountName;
    // certify needed
    mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//1 is to certify
    //the user id
    mailmsg.Fields.Add(
    "http://schemas.microsoft.com/cdo/configuration/sendusername",
    Mail.accountName);
    //the password
    mailmsg.Fields.Add(
    "http://schemas.microsoft.com/cdo/configuration/sendpassword",
    Mail.password);

    System.Web.Mail.SmtpMail.SmtpServer
    = Mail.smtpServer;

    System.Web.Mail.SmtpMail.Send(mailmsg);

    }
    /// <summary>
    /// 邮件发送方法二
    /// </summary>
    /// <param name="sendTo"></param>
    /// <param name="subject"></param>
    /// <param name="body"></param>
    static public void SendMail2(string sendTo, string subject, string body)
    {
    System.Net.Mail.MailMessage msg
    = new System.Net.Mail.MailMessage(accountName, sendTo, subject, body);
    msg.From
    = new System.Net.Mail.MailAddress(accountName, "Mail");
    System.Net.Mail.SmtpClient client
    = new System.Net.Mail.SmtpClient(smtpServer);
    msg.IsBodyHtml
    = true;
    client.Credentials
    = new System.Net.NetworkCredential(accountName, password);
    client.DeliveryMethod
    = System.Net.Mail.SmtpDeliveryMethod.Network;

    client.Send(msg);
    }

    }
  • 相关阅读:
    onload执行顺序
    让EXCHANGE可以接收外部邮件服务器发送的邮件
    QQ网站里AppTui对象脚本
    owa2007写新邮件时已经选择人员不能带入到与通讯录选人界面问题解决方法
    qq网站里动态加载脚本的实现
    qq网站里对元素的操作方法
    让Exchange可以发送邮件到互联网的邮件服务器
    js实现的hashtable
    无线网卡共享网络发射
    css省略号效果
  • 原文地址:https://www.cnblogs.com/darjuan/p/aspnet_mail.html
Copyright © 2011-2022 走看看