..


Sponsored Links

Create and manage your emails in C #

Article written by Vincent Gaglio
Page 1 of 3

As we all know now the emails are a very popular communication mechanism, particularly for its ease of use. For a programmer so this is a key tool to enable your applications to send messages, reports and files between users.

Starting with version 2.0. NET Framework has been introduced the System.Net.Mail namespace, which provides several classes that allow you to create and send email messages. These messages can include simple text, HTML and attachments. In practice the transmission of an email can be divided into two steps: creating and sending the same message to an SMTP (Simple Mail Transfer Protocol).

The creation of an email message can be simple or complex. In its simplest version consists of an email message from a sender, a recipient, and the body of an object, ie the actual message. This simple type of email can be created with a single line of code using the. NET Framework. In more complex versions of the email may also have a type of custom coding, contain HTML code, contain pictures (as attachments) or have embedded images in HTML.

To create and send an email message must follow the following steps:

  1. Create a MailMessage object
  2. If you do not specify the recipients in the constructor of that object then add
  3. If there is a need to present multiple views (such as, for example, plain text and HTML), you must create the necessary objects and insert the object MailMessage AlternateView
  4. If you want to send attachments must create their own objects and place them in the object MailMessage Attachments
  5. Create an object and specify the SMTP server SmtpClient
  6. If the SMTP server requires clients to authenticate to add the appropriate authentication credentials SmtpClient object
  7. Pass the MailMessage object to the method SmtpClient.Send

Let us now see how to do this. The MailMessage class provides four constructors that allow you to create an empty message, or, in the most complete version, a message sender, recipient, subject and message body. We see below an example that uses the constructor with the maximum number of parameters






 MailMessage msg = new MailMessage ("mittente@mittente.com"

 





 "Destinatario@destinatario.com", "Subject", "Contents of the email");



It 'can also specify the sender and recipient in the form of MailAddress objects. An object of this type allows you to specify an email address, but also to specify the display name instead of the same (aliases) and the type of encryption, such as the following example shows






 MailMessage msg = new MailMessage (







 new MailAddress ("mittente@mittente.com", "Sender Name Displayed"),

 





 new MailAddress ("destinatario@destinatario.com"

 





 "Recipient Name Displayed"));



To specify the type of encryption you must use another constructor of the MailAddress






 MailMessage msg = new MailMessage (new MailAddress ("mittente@mittente.com"

 





 "Sender Name Displayed" Encoding.ASCII)

 





 new MailAddress ("destinatario@destinatario.com"

 





 "Recipient Name Displayed" Encoding.ASCII));



however, rarely need to specify the encoding type.

In the same category ...
E-Learning
OpenOffice (Ebook) OpenOffice (Ebook)
The open-source software for managing the office work. Just 25 €.
Web Marketing (Course) Web Marketing (Course)
Site promotion, search engines and marketing. From 39 €.
Webmaster Base (First) Webmaster Base (First)
Create a Web site from scratch. Starting from 29 €.
Sponsored Links