qr code generator asp net c# Exceptions that Can Occur when Sending Mail in Visual C#

Encode QR Code JIS X 0510 in Visual C# Exceptions that Can Occur when Sending Mail

Exceptions that Can Occur when Sending Mail
QR Code Drawer In C#
Using Barcode maker for .NET framework Control to generate, create QR image in Visual Studio .NET applications.
www.OnBarcode.com
QR Code JIS X 0510 Recognizer In Visual C#.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Situation You did not define the server hostname. The server hostname could not be found. You are sending a message to a recipient at your local mail server, but the recipient does not have a mailbox. You are not a valid user, or other message transmission problems.
Barcode Creation In C#
Using Barcode creator for Visual Studio .NET Control to generate, create barcode image in .NET applications.
www.OnBarcode.com
Bar Code Reader In C#
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Exception InvalidOperationException SmtpException with an inner WebException SmtpFailedRecipientException
QR Code ISO/IEC18004 Generator In VS .NET
Using Barcode creator for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications.
www.OnBarcode.com
Painting QR Code In .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
Synchronous or Asynchronous Both Both Synchronous
Print Denso QR Bar Code In VB.NET
Using Barcode drawer for VS .NET Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
Matrix Barcode Generation In Visual C#.NET
Using Barcode maker for VS .NET Control to generate, create 2D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
SmtpException
EAN-13 Supplement 5 Encoder In C#
Using Barcode printer for Visual Studio .NET Control to generate, create EAN 13 image in .NET applications.
www.OnBarcode.com
Bar Code Encoder In C#.NET
Using Barcode drawer for .NET framework Control to generate, create barcode image in .NET framework applications.
www.OnBarcode.com
Both
Encode Data Matrix In C#
Using Barcode printer for .NET framework Control to generate, create Data Matrix 2d barcode image in VS .NET applications.
www.OnBarcode.com
Creating EAN 8 In Visual C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create European Article Number 8 image in Visual Studio .NET applications.
www.OnBarcode.com
When using SmtpClient.SendAsync, invalid recipients and several other events do not result in an exception. Instead, the runtime will call the SmtpClient.SendCompleted event. If AsyncCompletedEventArgs.Error is not Null, an error occurred.
UCC - 12 Printer In Objective-C
Using Barcode maker for iPad Control to generate, create UPC A image in iPad applications.
www.OnBarcode.com
Making PDF417 In Java
Using Barcode printer for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
How to Configure Credentials
Creating EAN-13 In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create EAN13 image in ASP.NET applications.
www.OnBarcode.com
Code 128B Maker In None
Using Barcode maker for Font Control to generate, create Code 128C image in Font applications.
www.OnBarcode.com
To reduce spam, all SMTP servers should reject messages from unauthorized users when the message recipients are not hosted by the SMTP server. SMTP servers
QR Decoder In Visual Studio .NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
GTIN - 13 Creator In Java
Using Barcode generator for Eclipse BIRT Control to generate, create GS1 - 13 image in BIRT reports applications.
www.OnBarcode.com
Lesson 2: Sending Mail
Recognizing PDF417 In .NET Framework
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Barcode Reader In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
provided by ISPs typically determine whether a user is authorized based on the user s IP address; if you are part of the ISP s network, you are allowed to use the SMTP server. Other SMTP servers (and some ISP SMTP servers) require users to provide a valid username and password. To use the default network credentials, set SmtpClient .UseDefaultCredentials to True. Alternatively, you can set SmtpClient.Credentials to CredentialCache.DefaultNetworkCredentials (in the System.Net namespace), as the following code demonstrates:
' VB Dim client As SmtpClient = New SmtpClient("smtp.contoso.com") client.Credentials = CredentialCache.DefaultNetworkCredentials // C# SmtpClient client = new SmtpClient("smtp.contoso.com"); client.Credentials = CredentialCache.DefaultNetworkCredentials;
To specify the username and password, create an instance of the System.Net.NetworkCredential class and use it to define SmtpClient.Credentials. The following example shows hard-coded credentials; however, you should always prompt the user for credentials:
' VB Dim client As SmtpClient = New SmtpClient("smtp.contoso.com") client.Credentials = New NetworkCredential("user", "password") // C# SmtpClient client = new SmtpClient("smtp.contoso.com"); client.Credentials = new NetworkCredential("user", "password");
How to Configure SSL
Another important security-related property is SmtpClient.EnableSsl. When you set this value to True, the runtime will encrypt the SMTP communications using SSL. Not all SMTP servers support SSL, but you should always enable this property if support is available.
NOTE .NET 2.0
SmtpClient.EnableSsl is new in .NET 2.0.
How to Send a Message Asynchronously
Sending an e-mail message often takes less than a second. Other times, however, the SMTP server might be slow or completely unresponsive, causing your application to wait for the value specified by SmtpClient.Timeout. While your application waits for
15
Mail
the SMTP server (up to 100 seconds by default), your application will be unresponsive and the cursor will change to an hourglass. Users don t have much patience for unresponsive applications, and there is a good chance they will terminate your application. Fortunately, you can send e-mails asynchronously to enable your application to respond to the user while you wait for the message to be sent. You can even give the user the opportunity to cancel the e-mail transmission. To send a message asynchronously, perform these tasks: 1. Create a method to respond to the SmtpClient.SendCompleted event. This method needs to determine whether the transmission was successful, unsuccessful, or cancelled. 2. Add your event handler to SmtpClient.SendCompleted. 3. Call SmtpClient.SendAsync. 4. Optionally, provide the user the opportunity to cancel the e-mail by calling the SmtpClient.SendAsyncCancel method. For example, the following method responds to an SmtpClient.SendCompleted event (it requires the System.ComponentModel namespace):
' VB Sub sc_SendCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) If e.Cancelled Then Console.WriteLine("Message cancelled") Else If Not (e.Error Is Nothing) Then Console.WriteLine("Error: " + e.Error.ToString) Else Console.WriteLine("Message sent") End If End If End Sub // C# void sc_SendCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Cancelled) Console.WriteLine("Message cancelled"); else if (e.Error != null) Console.WriteLine("Error: " + e.Error.ToString()); else Console.WriteLine("Message sent"); }
Lesson 2: Sending Mail
And the following code creates the SmtpClient object, adds the event handler, calls the asynchronous send, and then immediately cancels the send. Naturally, in real code, you would wait for the user to initiate a cancellation. This code assumes a MailMessage object named mm already exists.
' VB SmtpClient sc = new SmtpClient("server_name"); ' Add the event handler AddHandler sc.SendCompleted, AddressOf sc_SendCompleted ' Send the message asynchronously sc.SendAsync(mm, Nothing) ' Cancel the send sc.SendAsyncCancel()
// C# sc = new SmtpClient("server_name"); // Add the event handler sc.SendCompleted += new SendCompletedEventHandler(sc_SendCompleted); // Send the message asynchronously sc.SendAsync(mm, null); // Cancel the send sc.SendAsyncCancel();
SmtpClient.SendAsync accepts two parameters: the MailMessage object to be sent and a generic Object. You can specify Null or any other object for the second parameter; it is strictly for your own use. The .NET Framework simply passes it to the event handler. If you were sending multiple messages asynchronously, you could use the second parameter to keep track of which message generated the event.
Copyright © OnBarcode.com . All rights reserved.