sending e-mail from gmail servers, from .net code

I found this code on c-sharpcorner.com, it works, untill now i have a lot of problems when i have to send mail using gmail acount. It works, the base source is think big blog's; enjoy it!

           // C# Code
            MailMessage msg = new MailMessage();
            // Your mail address and display name.
            // This what will appear on the From field.
            // If you used another credentials to access the SMTP server, the mail message would be
            // sent from the mail specified in the From field on behalf of the real sender.
            msg.From = new MailAddress("example-mail@gmail.com", "PAN");
            // To addresses

            msg.To.Add(new MailAddress("x@example.com", "b0by"));
            msg.To.Add(new MailAddress("
x@example.com", "me"));

            // You can specify CC and BCC addresses also
            // Set to high priority
            msg.Priority = MailPriority.High;
            msg.Subject = "Hey, a fabulous site!";
            // You can specify a plain text or HTML contents
            msg.Body = "Hello everybody,<br /><br />" +
                "I found an interesting site called <a href=\"http://pan-internet.com\">" +
                "Pan Internet!</a>. Be sure to visit it soon.";
            // In order for the mail client to interpret message body correctly,
            // we mark the body as HTML because we set the body to HTML contents.
            msg.IsBodyHtml = true;
            // Attaching some data; throws an exception if the file does not exist   
            //msg.Attachments.Add(new Attachment("C:\\Site.lnk"));

            // Connecting to the server and configuring it
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.gmail.com";
            client.Port = 25;
            client.EnableSsl = true;
            // The server requires user's credentials not the default credentials
            client.UseDefaultCredentials = false;
            // Provide your credentials
            client.Credentials = new System.Net.NetworkCredential("example-mail@gmail.com", "examplepasssword");
            // Use SendAsync to send the message asynchronously

             try
            {
                client.Send(msg);
            }
            catch (SmtpFailedRecipientException e1)
            {
                
                Console.WriteLine(e1.Message);               
                //SmtpFailedRecipientException
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


Posted by: admin
Posted on: 10/20/2009 at 9:46 AM
Tags: , , ,
Categories: .NET | ASP | Google | Programing | WWW
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (2) | Post RSSRSS comment feed

Comments

Mohammad Elsheimy Egypt

Tuesday, October 20, 2009 7:20 AM

Mohammad Elsheimy

plz report the error message from SmtpFailedRecipientException or SmtpFailedRecipientsExceptios

admin United States

Tuesday, October 20, 2009 7:47 AM

admin

Hello Mohammad,

I change my code / your code handling that exceptions. This usually will appear where the client place something wrong (for 1st exception) or  the programmer didn't change my dummy connection data. I believe that is not necessary, for a programmer, to specify that default data is wrong.

Thanks

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading