.NET Framework 4
 

 

允许应用程序使用简单邮件传输协议 (SMTP) 来发送电子邮件。

System.Object
  System.Net.Mail.SmtpClient

 

命名空间:  System.Net.Mail 程序集: System(在 System.dll 中)
复制
public class SmtpClient : IDisposable

SmtpClient 类型公开以下成员。

  名称 说明
SmtpClient SmtpClient 类的新实例。
SmtpClient(String) SmtpClient 类的新实例,让其使用指定的 SMTP 服务器发送电子邮件。
SmtpClient(String, Int32) SmtpClient 类的新实例,让其使用指定的 SMTP 服务器和端口发送电子邮件。
页首
  名称 说明
ClientCertificates 指定应该使用哪些证书来建立安全套接字层 (SSL) 连接。
Credentials 获取或设置用于验证发件人身份的凭据。
DeliveryMethod 指定如何处理待发的电子邮件。
EnableSsl SmtpClient 是否使用安全套接字层 (SSL) 加密连接。
Host 获取或设置用于 SMTP 事务的主机的名称或 IP 地址。
PickupDirectoryLocation 获取或设置文件夹,应用程序在该文件夹中保存将由本地 SMTP 服务器处理的邮件。
Port 获取或设置用于 SMTP 事务的端口。
ServicePoint 获取用于传输电子邮件的网络连接。
TargetName 获取或设置在使用扩展保护时用于身份验证的服务提供程序名称 (SPN)。
Timeout Send 调用的超时时间。
UseDefaultCredentials DefaultCredentials 是否随请求一起发送。
页首
  名称 说明
Dispose SmtpClient 类的当前实例使用的所有资源。
Dispose(Boolean) SmtpClient 类的当前实例使用的所有资源,并可选择释放托管资源。
Equals(Object) Object。)
Finalize Object。)
GetHashCode Object。)
GetType Object。)
MemberwiseClone Object。)
OnSendCompleted SendCompleted 事件。
Send(MailMessage) 将指定的邮件发送到 SMTP 服务器以便传递。
Send(String, String, String, String) String 对象指定邮件的发件人、收件人、主题和邮件正文。
SendAsync(MailMessage, Object) 此方法不会阻止调用线程,并允许调用方将对象传递给操作完成时调用的方法。
SendAsync(String, String, String, String, Object) 此方法不会阻止调用线程,并允许调用方将对象传递给操作完成时调用的方法。
SendAsyncCancel 取消异步操作以发送电子邮件。
ToString Object。)
页首
  名称 说明
SendCompleted 在异步电子邮件发送操作完成时出现。
页首

http://www.ietf.org 上找到。

SmtpClient 发送的电子邮件。

 

说明

Attachment

此类允许您将文件、流或文本附加到电子邮件中。

MailAddress

表示发件人和收件人的电子邮件地址。

MailMessage

表示电子邮件。

SmtpClient 构造并发送电子邮件,必须指定以下信息:

  • Port 属性。

  • Credentials 属性。

  • MailMessage.From 属性。

  • MailMessage.To 属性。

  • MailMessage.Body 属性。

MailMessage.AlternateViews 属性来指定替代视图。

<mailSettings> 元素(网络设置)

SendAsyncCancel 方法。

注意

InvalidOperationException

当向同一个 SMTP 服务器发送大量电子邮件时,给每条消息重新建立连接的要求可能对性能有明显的影响。 对电子邮件客户端来说,典型情况是将所有 SMTP 消息发送到一个特点的 SMTP 服务器(由 Internet 服务提供商提供),然后此服务器再将此电子邮件发送到其他 SMTP 服务器。

SmtpClient 对象和该对象应在何时被清理。

这使得服务器可以释放与来自客户端的连接相关的资源,并处理客户端发来的消息。

Socket 使用的非托管资源,并根据需要释放托管资源。

SmtpClient 占用的内存。

Topic Location
如何:在 IIS 中安装和配置 SMTP 虚拟服务器 配置 ASP .NET Web 应用程序
如何:在 IIS 6.0 中安装和配置 SMTP 虚拟服务器 在 Visual Studio 中生成 ASP .NET Web 应用程序

下面的代码示例演示如何以异步方式发送电子邮件。

C#
复制
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Examples.SmptExamples.Async
{
    public class SimpleAsynchronousExample
    {
        static bool mailSent = false;
        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
             String token = (string) e.UserState;

            if (e.Cancelled)
            {
                 Console.WriteLine("[{0}] Send canceled.", token);
            }
            if (e.Error != null)
            {
                 Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
            } else
            {
                Console.WriteLine("Message sent.");
            }
            mailSent = true;
        }
        public static void Main(string[] args)
        {
            // Command line argument must the the SMTP host.
            SmtpClient client = new SmtpClient(args[0]);
            // Specify the e-mail sender.
            // Create a mailing address that includes a UTF8 character
            // in the display name.
            MailAddress from = new MailAddress("jane@contoso.com", 
               "Jane " + (char)0xD8+ " Clayton", 
            System.Text.Encoding.UTF8);
            // Set destinations for the e-mail message.
            MailAddress to = new MailAddress("ben@contoso.com");
            // Specify the message content.
            MailMessage message = new MailMessage(from, to);
            message.Body = "This is a test e-mail message sent by an application. ";
            // Include some non-ASCII characters in body and subject.
            string someArrows = new string(new char[] {'\u2190', '\u2191', '\u2192', '\u2193'});
            message.Body += Environment.NewLine + someArrows;
            message.BodyEncoding =  System.Text.Encoding.UTF8;
            message.Subject = "test message 1" + someArrows;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            // Set the method that is called back when the send operation ends.
            client.SendCompleted += new 
            SendCompletedEventHandler(SendCompletedCallback);
            // The userState can be any object that allows your callback 
            // method to identify this send operation.
            // For this example, the userToken is a string constant.
            string userState = "test message1";
            client.SendAsync(message, userState);
            Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
            string answer = Console.ReadLine();
            // If the user canceled the send, and mail hasn't been sent yet,
            // then cancel the pending operation.
            if (answer.StartsWith("c") && mailSent == false)
            {
                client.SendAsyncCancel();
            }
            // Clean up.
            message.Dispose();
            Console.WriteLine("Goodbye.");
        }
    }
}


 

相关文章:

  • 2021-12-16
  • 2021-09-12
  • 2022-12-23
  • 2022-12-23
  • 2021-04-05
  • 2021-12-03
  • 2021-07-27
猜你喜欢
  • 2021-08-10
  • 2021-10-17
  • 2022-01-03
  • 2021-08-05
  • 2022-12-23
  • 2021-09-05
相关资源
相似解决方案