SmtpClient class, SmtpClient

Source: Internet
Author: User
Tags mailmessage smtpclient

SmtpClient class, SmtpClient

SmtpClient class

Allow applications to send emails using Simple Mail Transfer Protocol (SMTP.

Namespace: System.net. mail

Attribute

ClientCertificates: Specifies the certificate to use to establish a Secure Socket Layer (SSL) connection.

Credentials: gets or sets the credential used to authenticate the sender.

DeliveryFormat: gets or sets the transfer format used by SmtpClient to send an email

DeliveryMethod: Specifies how an email is sent to process messages

EnableSsl: Specifies whether SmtpClient uses SSL-encrypted connections.

Host: gets or sets the IP address of the Host used by one or more SMTP transaction records.

PickupDirectoryLocation: Get or set the application to save the mail in it to process the folder on the local SMTP Server

Port: Get or set the Port used for SMTP transaction

ServicePoint: gets the network connection used for email transmission

TargetName: extended protection is used for authentication when obtaining or setting the service provider name (SPNs)

Timeout: gets or sets a value to specify the time-out period of the Send call.

Usedefacrecredentials: gets or sets a Boolean value that controls whether defacrecredentials is sent along with the request.

Method

Dispose ()

Sends a QUIT message to the SMTP server, ends the TCP connection normally, and releases all resources of the SmtpClient class used by the current instance.

Dispose (Boolean)

Send a QUIT message to the SMTP server. When the TCP connection ends normally, all the resources of the SmtpClient class used by the current instance are released and managed resources can be released as needed.

Equals (Object)

Determines whether the specified object is equal to the current object

Finalize ()

Allow the object to try to release resources and perform other cleanup operations before the garbage collection mechanism recycles an object.

GetHashCode ()

As the default Hash Function

GetType ()

Obtains the Type of the current instance.

MemberwiseClone ()

Create a superficial copy of the current Object

OnSendCompleted (AsyncCompletedEventArgs)

Trigger SendComplete event

Send (MailMessage)

Sends the specified message to the SMTP server for transmission.

Send (String, separator String)

Send the specified email to the SMTP server for transmission. Specify a String object for the sender, recipient, subject, and body of the email.

SendAsync (MailMessage, jsonobject)

Send the specified email to the SMTP server for transmission. This method does not block the call thread and allows the caller to pass the object to the method called when the operation is completed.

SendAsync (String, response String, response Object)

Send an email to the SMTP server for transmission. The sender, recipient, subject, and body of the message use the specified String object. This method does not block the calling thread and allows the caller to pass the object to the method called when the operation is completed.

SendAsyncCancel ()

Cancel Asynchronous Operation to send email

SendMailAsync (MailMessage)

Send the specified message to the SMTP server for asynchronous operation.

SendMailAsync (String, latency String)

Send the specified message to the SMTP server for delivery in asynchronous mode. . The sender, recipient, subject, and body of the message use the specified String object.

ToString ()

Returns the string of the current object. (Inherited from Object .)

Event

SendCompleted

This occurs when the asynchronous email sending operation is complete.

Remarks

The classes shown in the following table are used to build an SmtpClient that can be sent.

Attachment class

A file attachment, which allows you to attach a file, stream, or text to an email.

MailAddress class

The email address of the sender and recipient.

MailMessage class

Indicates an email.

To construct and send an email using SmtpClient, you must specify the following information ︰

  • The SMTP host server used to send emails.

  • For authentication, if the SMTP server requires creden.

  • The sender's email address.

  • Email Address or recipient address.

  • Message content.

To include the use of emailAttachmentFirst, create an Attachment and use the Attachment class, and then add it to the message through the MailMessage. Attachments attribute. Depending on the reader of an email using the recipient and attachment file type, some recipients cannot read the attachment. For clients that cannot display attachments in their original format, you can replace the view MailMessage. AlternateViews attribute by specifying.

You can use this application or computer configuration file to specify SmtpClient objects for all default hosts, ports, and creden.

To Send an email and a block while waiting for an email to be transmitted to the SMTP server, use a synchronous Send method. To allow the main thread of the program to continue sending email, use the SendAsync method, which is asynchronous. The SendAsync operation is complete when SendCompleted raises an event. To receive this event, you must add SendCompletedEventHandler to delegate to SendCompleted. SendCompletedEventHandler refers to the callback method that must be referenced by the delegate to process the SendCompleted event of the notification. To cancel asynchronous Email transmission, use the SendAsyncCancel method.

Main Code of the email 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 button#click (object sender, EventArgs e)
{
Form6 form = new Form6 ();
Form. SendParaHandler + = new Form6.SendPara (reload); // hook up the event
Form. 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. usedefacrecredentials = 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 mailsubject = textBox2.Text;
String mailbody = textBox3.Text;
If (sendmail (mailfrom, mailto, mailsubject, mailbody ))
{
MessageBox. Show ("email sent successfully ");
}
Else
{
MessageBox. Show ("email sending failed ");
}
}


}
}

Main Code for setting the sender information page:

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 button#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 (); // defines the delegate
Public event SendPara SendParaHandler; // defines the event
Private void button2_Click (object sender, EventArgs e)
{
SendParaHandler. Invoke ();
Write ();
This. Close ();
}
}
}

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.