Tutorials
Desktop Programming
C Sharp
CREATING EMAILS IN C#
Written by Christopher Thursday, 12 June 2008 18:17
Tutorials
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.

sunjester
said:
|
... there was no mention of outgoing smtp servers. mail doesnt magically send itself. |
|
Votes: +0
Christopher
said:
|
... 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. |
|
Votes: +0
| < Prev | Next > |
|---|
| Training Tip #1 :: Push-ups on the Swiss Ball admin 28.4.2009 7:07 |
Re:hello admin 21.4.2008 14:11 |
| Dell Inspiron 1545 Windows XP Drivers admin 12.4.2009 8:03 |
hello yuppy 21.4.2008 10:28 |
| Re:Get rich ebooks - are they really working? lann 23.4.2008 17:04 |
Re:Get rich ebooks - are they really working? admin 19.4.2008 14:08 |