Tutorials
Desktop Programming
C Sharp
SENDING EMAILS IN C#
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");
}
}
}

Sagar
said:
|
... thx .... what all header files are required for sending mail in c . please reply on \n 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 |
|
Votes: +0
| 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 |