Sending an email was never so easy except for the Failure message -

I was studying for the Application Foundation exam and was playing with the System.Net.Mail namespace...

My initial attempt resulted in getting a failure message displayed below

Exception message: Mailbox unavailable. The server response was: 5.7.1 Unable t
 relay for
user007@infusiondev.com
Stack Trace:    at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, M
ilAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientExcep
ion& exception)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at ApplicationFoundation.Chapter03Demo.SendMessage() in C:\Documents and Set
ings\Owner\My Documents\Visual Studio 2005\Projects\Prototypes\ApplicationFound
tion\ApplicationFoundation\Program.cs:line 102

After some googling I found out that seting the delivery method on the SMTPclient solved the problem. Here is the full code with the "line specifying the delivery method option " in a bigger and different  font

Stream stream =

new FileStream("Arul.JPG", FileMode.Open, FileAccess.Read);

            try

            {

                MailMessage message = new MailMessage();

                message.Subject = "Cool mail namespace";

                message.Body = "This is the body !!!";

message.Priority = MailPriority.High;

 

               

MailAddress address1 = new  
        MailAddress("hpadmanaban@infusiondev.com", "Hari Pad");

                MailAddress address2 = new MailAddress("test@CC.com");

               

                 message.To.Add(address1);

message.From = address2;

               

                Attachment attachment =

new Attachment(stream, "Arul.jpg", MediaTypeNames.Image.Jpeg);

               

   message.Attachments.Add(attachment);

                SmtpClient client = new SmtpClient("127.0.0.1");

                client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

                client.Send(message);

                Console.WriteLine("mail Sent Successfully!!!");

            }

            catch (Exception exception)

            {

                Console.WriteLine("Exception message: " + exception.Message);

                Console.WriteLine("Stack Trace: " + exception.StackTrace);

            }

            finally

            {

 

                stream.Close();

            }


Hope this helps someone

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.