11 Sep

Asynchronous Programming with Async And Await in C# .Net

This page shows how to use async and await keywords for asynchronous programming in C#.
We can avoid performance bottlenecks and enhance the overall responsiveness of our application by using asynchronous programming. However, traditional techniques for writing asynchronous applications can be complicated, making them difficult to write, debug, and maintain. This example uses async and await keywords that coming in .Net Framework 4.5.

Usage :

                Task<int> returnedTaskTResult = TaskOfTResult_MethodAsync();
                int result = await returnedTaskTResult;

Sample :

            // Signature specifies Task<TResult>
            async Task<int> TaskOfTResult_MethodAsync()
            {
                int result;
                // . . .
                // The body of the method should contain one or more await expressions.

                // Return statement specifies an integer result.
                return result;
            }
            
            // Signature specifies Task
            async Task Task_MethodAsync()
            {
                // The body of the method should contain one or more await expressions.

                // The method has no return statement.  
            }
11 Sep

C# yield keyword

If we use the yield keyword in a statement, we indicate that the method, operator, or get accessor in which it appears is an iterator.

Two forms of the yield statement are used.

   yield return <expression>;
   yield break;

We use a “yield return” statement to return each element one at a time.
We use a “yield break” statement to end the iteration.

Usage :

            // Display powers of 2 up to the exponent of 10: 
            foreach (int i in Power(2, 10))
            {
                Console.Write("{0} ", i);
            }
            // Output: 2 4 8 16 32 64 128 256 512 1024

Sample :

        public System.Collections.Generic.IEnumerable <int> Power(int number, int exponent)
        {
            int result = 1;
            for (int i = 0; i < exponent; i++)
            {
                result = result * number;
                yield return result;
            }
        }
10 Sep

C# Send an Email with Attachment from GMail

This example shows how to send an email with attachment from GMail.
To send an email using GMail, we need to add a reference to the dynamic link library System.Net.Mail.
To do this:

  • Go to solution explorer of your project
  • Select add a reference
  • Click on .Net Tab
  • Select System.Net.Mail.

The GMail SMTP Server requires an encrypted connection (SSL) on port 587.

Sample :

        public void SendMailUsingGMail()
        {
            try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress("sender@gmail.com");

                //receiver email adress
                mailMessage.To.Add("receiver@gmail.com");

                //subject of the email
                mailMessage.Subject = "This is a subject";

                //attach the file
                mailMessage.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
                mailMessage.Body = "Body of the email";
                mailMessage.IsBodyHtml = true;

                //SMTP client
                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
                //port number for gmail
                smtpClient.Port = 587;
                //credentials to login in to Gmail account
                smtpClient.Credentials = new NetworkCredential("sender@gmail.com", "password");
                //enabled SSL
                smtpClient.EnableSsl = true;
                //Send an email
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {

            }
        }
10 Sep

C# Send an Email with Attachment from HotMail

This example shows how to send an email with attachment from HotMail.
To send an email using HotMail, we need to add a reference to the dynamic link library System.Net.Mail.
To do this:

  • Go to solution explorer of your project
  • Select add a reference
  • Click on .Net Tab
  • Select System.Net.Mail.

The HotMail SMTP Server requires an encrypted connection (SSL) on port 25.
Sample :

        public void SendMailUsingHotmail()
        {
            try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress("sender@hotmail.com");
                
                //receiver email adress
                mailMessage.To.Add("receiver@gmail.com");
                
                //subject of the email
                mailMessage.Subject = "This is a subject";
                
                //attach the file
                mailMessage.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
                mailMessage.Body = "Body of the email";
                mailMessage.IsBodyHtml = true;
                
                //SMTP client
                SmtpClient smtpClient = new SmtpClient("smtp.live.com");
                //port number for Hot mail
                smtpClient.Port = 25;
                //credentials to login in to hotmail account
                smtpClient.Credentials = new NetworkCredential("sender@hotmail.com", "password");
                //enabled SSL
                smtpClient.EnableSsl = true;
                //Send an email
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {

            }
        }
10 Sep

C# Send an Email with Attachment from Yahoo

This example shows how to send an email with attachment from Yahoo.
To send an email using Yahoo, we need to add a reference to the dynamic link library System.Net.Mail.
To do this:

  • Go to solution explorer of your project
  • Select add a reference
  • Click on .Net Tab
  • Select System.Net.Mail.

The Yahoo SMTP Server requires an encrypted connection (SSL) on port 587.

Sample :

        public void SendMailUsingYahoo()
        {
            try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress("sender@yahoo.com");

                //receiver email adress
                mailMessage.To.Add("receiver@gmail.com");

                //subject of the email
                mailMessage.Subject = "This is a subject";

                //attach the file
                mailMessage.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
                mailMessage.Body = "Body of the email";
                mailMessage.IsBodyHtml = true;

                //SMTP client
                SmtpClient smtpClient = new SmtpClient("smtp.mail.yahoo.com");
                //port number for Yahoo
                smtpClient.Port = 587;
                //credentials to login in to yahoo account
                smtpClient.Credentials = new NetworkCredential("sender@yahoo.com", "password");
                //enabled SSL
                smtpClient.EnableSsl = true;
                //Send an email
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {

            }
        }