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) {
.NET smtpClient 源代码
最新推荐文章于 2023-01-12 18:28:22 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)