C # Supplements's SmtpClient class

Source: Internet
Author: User
Tags mailmessage smtpclient
SmtpClient class

Allows an application to send e-mail messages using the Simple Mail Transfer Protocol (SMTP).

Namespaces: System.Net.Mail

Property

ClientCertificates: Specifies which certificate should be used to establish a Secure Sockets Layer (SSL) connection

Credentials: Gets or sets the credentials used to authenticate the sender

Deliveryformat: Gets or sets the delivery format used smtpclient send e-mail

Deliverymethod: Specifies how e-mail messages that are sent will process the message

Enablessl: Specifies whether to smtpclient a connection using Secure Sockets Layer (SSL) encryption

Host: Gets or sets the IP address of the host used by one or more SMTP transactions

Pickupdirectorylocation: Gets or sets the folder in which the application saves messages to process the local SMTP server

Port: Gets or sets the ports used for SMTP things

ServicePoint: Get the network connection used to transfer e-mail

TargetName: Gets or sets the service provider name (SPN) used for authentication using extended Protection

Timeout: Gets or sets a value that specifies the time-out for the send call

useDefaultCredentials: Gets or sets a Boolean value that controls whether DefaultCredentials is sent with the request

Method

Dispose ()

Sends a quit message to the SMTP server, gracefully ends the TCP connection, and frees all resources for the SmtpClient class used by the current instance

Dispose (Boolean)

Sends a quit message to the SMTP server, ends the TCP connection gracefully, frees all the resources of the SmtpClient class used by the current instance, and releases the managed resources as needed

Equals (Object)

Determines whether the specified object is equal to the current object

Finalize ()

Allows the object to attempt to free resources and perform other cleanup operations before the garbage collection mechanism reclaims an object

GetHashCode ()

As the default hash function

GetType ()

Gets the type of the current instance

MemberwiseClone ()

Creates a shallow copy of the current object

Onsendcompleted (AsyncCompletedEventArgs)

Raising the Sendcomplete Event

Send (MailMessage)

Sends the specified message to the SMTP server for delivery

Send (String, String, String, String)

Sends the specified e-mail message to the SMTP server for delivery. Message sender, recipient, subject, and message body use the specified string object

SendAsync (MailMessage, Object)

Sends the specified e-mail message to the SMTP server for delivery. This method does not block the calling thread and allows the caller to pass the object to the method that is called when the operation completes

SendAsync (String, String, String, String, Object)

An e-mail message is sent to the SMTP server for delivery. The message sender, recipient, subject, and message body use the specified String object. This method does not block the calling thread and allows the caller to pass the object to the method that is called when the operation completes.

Sendasynccancel ()

Cancel an asynchronous operation to send an e-mail

Sendmailasync (MailMessage)

Sends the specified message to the SMTP server for delivery in the form of an asynchronous operation.

Sendmailasync (String, String, String, String)

Sends the specified message to the SMTP server for delivery in the form of an asynchronous operation. The message sender, recipient, subject, and message body use the specified String object.

ToString ()

Returns a String that represents the current object. (Inherit from Object.) )

Event

sendcompleted

Occurs when an asynchronous e-mail send operation completes

Note

The classes shown in the following table are used to build e-mail messages that can be sent using SmtpClient.

Attachment class

Represents a file attachment that allows you to attach files, streams, or text to an e-mail message

MailAddress class

Represents the e-mail address of the sender and the recipient

MailMessage class

Represents an e-mail message

Construct and send an e-mail message using SmtpClient, you must specify the following information:

The SMTP host server that is used to send e-mail.

For authentication, if the SMTP server requires credentials.

The sender's e-mail address.

e-mail address or recipient's address.

The message content.

To include using an e-mail attachment, first create an attachment by using the Attachment class, and then add the message through the Mailmessage.attachments property. Depending on the reader of the e-mail message that uses the recipient and the file type of the attachment, some recipients may not be able to read the attachment. For clients that cannot keep attachments displayed in their original format, you can mailmessage.alternateviews properties by specifying an alternate view.

You can use the application or Computer configuration file to specify the SmtpClient object for all default host, port, and credential values.

To send e-mail messages and blocks while waiting for e-mail to be transferred to the SMTP server, use a synchronous send method. To allow the main thread of the program to resume the transfer of e-mail, use one of the async SendAsync methods. Sendcompleted The SendAsync operation completes when the event is raised. To receive this event, you must add a Sendcompletedeventhandler delegate to sendcompleted. The Sendcompletedeventhandler delegate must refer to the callback method that is used to process the notification's sendcompleted event. To cancel an asynchronous e-mail transfer, use the Sendasynccancel method.

Main code of mail sending interface:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;using system.io;using System.Net;using System.net.mail;namespace sendemail{public partial class form3:form{string severaddress;string mailuser;string Userpwd;public Form3 () {InitializeComponent ();} private void Button1_Click (object sender, EventArgs e) {Form6 form = new FORM6 (); form. Sendparahandler +=new Form6.sendpara (reload);//The hook-up form for the event. Show ();} public void Reload () {StreamReader read = new StreamReader (@ "FAJIANREN.ASDF"); severaddress = read. ReadLine (); mailuser = read. ReadLine (); userpwd = read. ReadLine (); read. Close ();} private void Form3_load (object sender, EventArgs e) {reload ();} public bool SendMail (string mailfrom,string mailto,string mailsubject,string mailbody) {mailaddress from = new MailAddress (Mailfrom); MailMessage message = new MailMessage (); try{message. from = From;message. To.add (mailto); message. Subject = maIlsubject;message. Body = Mailbody;message. priority = Mailpriority.normal; SmtpClient smtp = new SmtpClient (); SMTP. Host = Severaddress;smtp. useDefaultCredentials = False;smtp. Enablessl = True;smtp. Credentials = new NetworkCredential (mailuser,userpwd); SMTP. Deliverymethod = Smtpdeliverymethod.network;smtp. Send (message);} catch (Exception e) {return false;} return true;} private void Button2_Click (object sender, EventArgs e) {string mailfrom = mailuser;string mailto = textbox1.text;string mai Lsubject = textbox2.text;string Mailbody = textbox3.text;if (SendMail (Mailfrom, mailto, mailsubject, mailbody)) { MessageBox.Show ("Mail sent successfully");} Else{messagebox.show ("message sent Failed");}}}

Set the sender information interface main code:

using system;using system.collections.generic;using System.ComponentModel; Using system.data;using system.drawing;using system.linq;using system.text;using system.windows.forms;using System.io;namespace sendemail{public Partial class Form6:form{public FORM6 () {InitializeComponent ();} private void Button1_Click (object sender, EventArgs e) {Write ();} Load Information private void Form6_load (object sender, EventArgs e) {StreamReader read = new StreamReader (@ "FAJIANREN.ASDF"); TextBox1.Text = read. ReadLine (); TextBox2.Text = read. ReadLine (); textbox3.text = read. ReadLine (); read. Close ();} Write Information public void write () {StreamWriter write = new StreamWriter (@ "FAJIANREN.ASDF"); write. WriteLine (TextBox1.Text); write. WriteLine (TextBox2.Text); write. WriteLine (textbox3.text); write. Close ();} public delegate void Sendpara ();//define Delegate public event Sendpara sendparahandler;//define event private void button2_click (object sender, EventArgs e) {sendparahandler.invoke (); Write (); Close ();}}} 

The above is the C # supplements SmtpClient class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.