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

Request in c#; how to get a web page content fast

This is a start-up information, when you try to made an program that should us the Internet. Of course, many domains are already covered, example using the web services, but you can still find places where to use separat web requests. What happens if you need to grab a image from the Internet, over http? Or a web page?

 

This problem is solved by this method:

       static Stream GetStream(string URL)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);



            // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse();

            #region Encoding
            Encoding encode = Encoding.UTF8;
            try
            {
                encode = System.Text.Encoding.GetEncoding(myHttpWebResponse.CharacterSet);
            }
            catch { }
            #endregion


            Stream streamResponse = myHttpWebResponse.GetResponseStream();


            //myHttpWebResponse.Close();
            return streamResponse;//Return the page as an Stream
        }

 

It works, but sometime you will need cookies, or you have to use a proxy server or many thinck like this. A long time ago, I create a class for working with requests. It can be downloaded by here; the ideea is when you have to use the same code again and again, it has to be optimized. In this moment my code is not very optimized, I just do it and forget about it, probably because it works. In the future this will be a project for an CodeProject article codeproject.com, where a long time ago, I promise to my self to put more code there :) .

coninue


Posted by: admin
Posted on: 9/21/2009 at 12:29 AM
Tags: , , ,
Categories: .NET | ASP | Programing
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

The proxy checking is working

In this week I will try to solve a important problem of my main website application: verifyng the proxy servers online, in the front of the user. In this way, you will check the proxy online, fast and without waiting from a desktop application or something that involves your resources, excepting the Internet.

The mchanism of checking is simple: the application is doing a request to a web page that contains an XML as response and you will get the result using the content of the xml response. You can check this page response, using your browser and placing there an transparent proxy (the simpliest way). Intuitive you will fnd the mechanism, but this will be also explained in an article here.

How much does it take to me to solve this new functionality? 1 hour and 30 minutes; includint the design. Why do I finish it in a week? Because We are living bad times, on limit. :(( this is it.

After this part works okay, I can start to work on optimizing this two sites (pan-internet.com and this blog). In the end!


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