This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

SENDING EMAILS IN C#
(1 vote, average: 5.00 out of 5)
Written by Christopher   
Thursday, 12 June 2008 18:26

INTRODUCTION

The process of sending emails is a simple one. However, because of its dependence on many factors things can become complicated. For example, if the server is not responding, the user should be able to choose whether to stop or not.

SENDING EMAILS

Once an email message has been created you need to send it through an SMTP server which will forward it to the recipient. The SmtpClient class represents this server. To send a message you just have to write the following code:

 

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 ");

            SmtpClient myClient = new SmtpClient("smtp.contoso.com");

            myClient.Send(myMail);

 

 

        }

    }

}

 

The last two lines actually send the email. However, with the complexity of communications’ infrastructures these days, many things can go wrong. It is imperative that your program does not crash when trying to send an email message. When the runtime throws an exception your application must be prepared to catch the exception. The following exceptions are these for which the programmer must account for in his/her program:

 

·         InvalidOperationException:  The server hostname was not correctly defined.

·         SmtpException with an inner WebException:  The server hostname could not be found.

·         SmtpFailedRecipientException:  The recipient does not have a mailbox.

·         SmtpException:  You are not a valid user, or other possible transmission problems.

 

When you send a message you should always be prepared to catch an SmtpException. These are the most frequent exceptions in procedures like this. In case the Smtp server cannot be found, messages will be rejected. Another possible reason of rejection is that the specific email was considered to be a spam message. The SmtpFailedRecipientException means that the Smtp server rejected the recipient’s email address. Bear in mind that only local Smtp servers can reject unknown recipients’ addresses. For example, if you are sending a message to This e-mail address is being protected from spambots. You need JavaScript enabled to view it through mail.com Smtp server, the message will be rejected if the email address is wrong. However, if you use another Smtp server it will not be able to identify that the email address is invalid. The following code demonstrates the complete process of sending emails, with try-catch statements to catch possible exceptions:

 

static void Main(string[] args)

        {

            try

            {

 

                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 ");

                SmtpClient myClient = new SmtpClient("smtp.contoso.com");

                myClient.Send(myMail);

 

            }

            catch (InvalidOperationException)

            {

                Console.WriteLine("You did not specify a server name");

            }

            catch (SmtpFailedRecipientException)

            {

                Console.WriteLine("The recipient's address was rejected");

            }

            catch (SmtpException)

            {

                Console.WriteLine(" Not a valid user or other fault");

            }

 

        }

    }

 

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options