Sending an email was never so easy except for the Failure message -
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
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


Comments