Text Size

CREATING EMAILS IN C#

PDF 

Tutorials

(0 votes, average: 0 out of 5)

INTRODUTION

E-mail is an extremely popular communication mechanism. The .Net framework has added the System.Net.Mail namespace which provides classes that enable you to easily create and transmit email messages. Messages can include plain text and attachments. In this tutorial we will talk about creating emails.

CREATING EMAILS

An email must have a sender, a recipient, subject and a body. Such mails can be easily created with the .Net framework. Furthermore, you can extend your email to include custom encoding types, multiple views with plain text and HTML and you can include various images. In order to create an email you must follow through these steps:

 

1.       Create a MailMessage object.

2.       Add the recipients to the MailMessage object.

3.       Create optional AlternateView objects if you need to have multiple views of your email.

4.       Create one or more Attachment objects to add attachments in your email.

 

The most basic concept in this process is the MailMessage object. This class has four different constructors that allow you to initialize it as a blank mail, specify the sender and recipient or specify the sender, recipient, subject and message body. For example, the following code creates a mail with the full information included in the constructor:

using System.Net.Mail;

private void button1_Click(object sender, EventArgs e)

        {

            MailMessage myMail = new MailMessage(" This e-mail address is being protected from spambots. You need JavaScript enabled to view it ", " This e-mail address is being protected from spambots. You need JavaScript enabled to view it ", "mySubject", "This is a test object");

 

        }

The sender’s and recipient’s email addresses can be specified either as string, or as MailAddress objects. In the above example we defined them as strings.

 

 A MailMessage object has also some useful properties such as:

 

·         DeliveryNotificationOptions: Instructs the SMTP server to send a message to the sender of the message if a message is delayed,  fails or is successfully delivered. It is an enumeration that includes the values OnSuccess, OnFailure, Delay, None and Never.

·         ReplyTo: Specifies the address in which the replies will be sent to.

·         Priority: specifies the priority of the message. It can have values of Low Normal and High.

Finally, to attach a file to an email you must use MailMessage.Attachments collection by calling an appropriate method (Add()). You can add an attachment by specifying its location. The following snippet of code demonstrates a more detailed mail creation with an attachment in it:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net.Mail;

 

 

namespace mails

{

    class Program

    {

        static void Main(string[] args)

        {

            MailMessage myMail = new MailMessage();

            myMail.From = new MailAddress(" This e-mail address is being protected from spambots. You need JavaScript enabled to view it ", "john doe");

            myMail.To.Add(new MailAddress(" This e-mail address is being protected from spambots. You need JavaScript enabled to view it ", "Joshua doringo"));

            myMail.To.Add(new MailAddress(" This e-mail address is being protected from spambots. You need JavaScript enabled to view it ", "Jenifer van dongo"));

            myMail.Subject = " Greetings from Spain";

            myMail.Body = " Check the atached photo!!!";

            myMail.Attachments.Add(new Attachment(@"C:\Picutre1.jpg"));

           myMail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            myMail.ReplyTo = new MailAddress(" This e-mail address is being protected from spambots. You need JavaScript enabled to view it ");

        }

    }

}

 

In this example two recipients are specified for our email which includes an attached image. We have also specified that we want to be notified in case our email is not successfully delivered.

Trackback(0)
Comments (10)add comment

sunjester said:

0
...
there was no mention of outgoing smtp servers. mail doesnt magically send itself.
 
August 01, 2008
Votes: +0

Christopher said:

Christopher
...
Hi!

Please have a look at this tutorial:
http://knoge.com/tutorials/desktop-programming/c-sharp/307-c-sharp/438-sending-emails-in-c.html

It's another one just for sending emails in C#, maybe you'll find your magic there.
 
August 06, 2008
Votes: +0

AJ said:

0
...
Here is some magic for you. this guy gave source code for a static class to do just that
http://easykb.com/default.aspx?id=43
 
August 21, 2009 | url
Votes: +0

discount ugg boots said:

0
...
Thank you very much for the excellent and useful subject.discount ugg boots
 
December 25, 2009 | url
Votes: +0

vchan said:

0
...
How to send rich message body ?
 
January 04, 2010
Votes: +0

uggs outlet said:

0
...
Choose to wear a pair of boots is a very important thing, everyone familiar ugg boots is a warm autumn and winter boots, the more emphasis on style, the winter to a pair of legs can guarantee a comfortable and warm, 09 new autumn and winter uggs boots on sale snow is even more style than in the past more easily with snow boots.
 
January 10, 2010 | url
Votes: +0

ugg store said:

0
...
Good sharing!
I don’t know whethercheap uggyou can read this message, but I think I should ugg boots onlinetell you something about my attitude for you! I am so grateful for your sharing.
 
January 14, 2010 | url
Votes: +0

Uggs Outlet said:

0
...
UGG Bailey Button or not listing, the sale of those Ugg Classic Cardy shoes were broken from the factory out of a small amount of code of Ugg Boots Classic Mini shoes, if the yardage is complete, it is UGG Classic Tall, it is impossible to thing. See vamps, really UGG very bright and clean, car line is 9 per inch needle, uggs boots on sale margin is uniform, line moderately elastic, there will be no floating lines, break, jump pin. UGG Ultra Short shoe feel very good, but there is no rupture, breakage.There are many products on our shop for sale,and some now in discount, very cheap.Welcome to purchase!
 
January 19, 2010 | url
Votes: +0

ugg boots said:

0
...
I recently came across your blog and have been reading along.Nike Air Jordan 19 I thought I would leave my first comment. I don’t know what tNike Air Jordan 17
o say except that I have enjoyed reading.Nike Air Jordan 18
Nice blog.
 
February 04, 2010 | url
Votes: +0

7686 said:

0
...
smilies/smiley.gifWearing MBT shoes provides many pro-body benefits: this is the result of an increasing number of international studies supporting the numerous health benefits of the "anti-shoe ".
 
February 19, 2010
Votes: +0

Write comment

busy

Site Statistics

Stats
Total Members
: 542
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1