.NET smtpClient 源代码

本文档详细介绍了 .NET Framework 中的 SmtpClient 类,包括构造函数、属性、方法等,展示了如何设置 SMTP 服务器地址、端口、身份验证等参数,并提供了发送邮件的相关示例。此外,还涵盖了 SSL 支持、自定义域名设置等内容。
摘要由CSDN通过智能技术生成
namespace System.Net.Mail 
{

    using System;
    using System.IO; 
    using System.Net;
    using System.Collections; 
    using System.ComponentModel; 
    using System.Configuration;
    using System.Net.Configuration; 
    using System.Threading;
    using System.Security;
    using System.Security.Permissions;
    using System.Security.Authentication; 
    using System.Security.Cryptography.X509Certificates;
    using System.Net.NetworkInformation; 
    using System.Runtime.Versioning; 

    public delegate void SendCompletedEventHandler(object sender, AsyncCompletedEventArgs e); 

    public enum SmtpDeliveryMethod {
        Network,
        SpecifiedPickupDirectory, 
#if !FEATURE_PAL
        PickupDirectoryFromIis 
#endif 
    }
 

    public class SmtpClient {

        string host; 
        int port;
        bool inCall; 
        bool cancelled; 
        bool timedOut;
        string targetName = null; 
        SmtpDeliveryMethod deliveryMethod = SmtpDeliveryMethod.Network;
        string pickupDirectoryLocation = null;
        SmtpTransport transport;
        MailMessage message; //required to prevent premature finalization 
        MailWriter writer;
        MailAddressCollection recipients; 
        SendOrPostCallback onSendCompletedDelegate; 
        Timer timer;
        static MailSettingsSectionGroupInternal mailConfiguration; 
        ContextAwareResult operationCompletedResult = null;
        AsyncOperation asyncOp = null;
        static AsyncCallback _ContextSafeCompleteCallback = new AsyncCallback(ContextSafeCompleteCallback);
        static int defaultPort = 25; 
        internal string clientDomain = null;
 
        public event SendCompletedEventHandler SendCompleted; 

        public SmtpClient() { 
            if (Logging.On) Logging.Enter(Logging.Web, "SmtpClient", ".ctor", "");
            try {
                Initialize();
            } finally { 
                if (Logging.On) Logging.Exit(Logging.Web, "SmtpClient", ".ctor", this);
            } 
        } 

        public SmtpClient(string host) { 
            if (Logging.On) Logging.Enter(Logging.Web, "SmtpClient", ".ctor", "host=" + host);
            try {
                this.host = host;
                Initialize(); 
            } finally {
                if (Logging.On) Logging.Exit(Logging.Web, "SmtpClient", ".ctor", this); 
            } 
        }
 
        //?? should port throw or just default on 0?
        public SmtpClient(string host, int port) {
            if (Logging.On) Logging.Enter(Logging.Web, "SmtpClient", ".ctor", "host=" + host + ", port=" + port);
            try { 
                if (port < 0) {
                    throw new ArgumentOutOfRangeException("port"); 
                } 

                this.host = host; 
                this.port = port;
                Initialize();
            } finally {
                if (Logging.On) Logging.Exit(Logging.Web, "SmtpClient", ".ctor", this); 
            }
        } 
 
        void Initialize() {
            if (port == defaultPort || port == 0) { 
                new SmtpPermission(SmtpAccess.Connect).Demand();
            }
            else {
                new SmtpPermission(SmtpAccess.ConnectToUnrestrictedPort).Demand(); 
            }
 
            transport = new SmtpTransport(this); 
            if (Logging.On) Logging.Associate(Logging.Web, this, transport);
            onSendCompletedDelegate = new SendOrPostCallback(SendCompletedWaitCallback); 

            if (MailConfiguration.Smtp != null)
            {
                if (MailConfiguration.Smtp.Network != null) 
                {
                    if (host == null || host.Length == 0) { 
                        host = MailConfiguration.Smtp.Network.Host; 
                    }
                    if (port == 0) { 
                        port = MailConfiguration.Smtp.Network.Port;
                    }

                    transport.Credentials = MailConfiguration.Smtp.Network.Credential; 

                    // If the config file contains a domain to be used for the 
                    // domain element in the client's EHLO or HELO message, 
                    // use it.
                    // 
                    // We do not validate whether the domain specified is valid.
                    // It is up to the administrators or user to use the right
                    // value for their scenario.
                    // 
                    // Note: per section 4.1.4 of RFC2821, the domain element of
                    // the HELO/EHLO should be used for logging purposes. An 
                    // SMTP server should not decide to route an email based on 
                    // this value.
                    clientDomain = MailConfiguration.Smtp.Network.ClientDomain; 

                    if (MailConfiguration.Smtp.Network.TargetName != null)
                        targetName = MailConfiguration.Smtp.Network.TargetName;
                } 
                deliveryMethod = MailConfiguration.Smtp.DeliveryMethod;
                if (MailConfiguration.Smtp.SpecifiedPickupDirectory != null) 
                    pickupDirectoryLocation = MailConfiguration.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation; 
            }
 
            if (host != null && host.Length != 0) {
                host = host.Trim();
            }
 
            if (port == 0) {
                port = defaultPort; 
            } 

            if (clientDomain == null) { 
                // We use the local host name as the default client domain
                // for the client's EHLO or HELO message. This limits the
                // information about the host that we share. Additionally, the
                // FQDN is not available to us or useful to the server (internal 
                // machine connecting to public server).
                clientDomain = IPGlobalProperties.InternalGetIPGlobalProperties().HostName; 
            } 

            if (this.targetName == null) 
                targetName = "SMTPSVC/" + host;
        }

 
        public string Host {
            get { 
                return host; 
            }
            set { 

                if (InCall) {
     
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值