Send Email from your Gmail account using C#

Internet

GmailIn this article I am going to show you how to programmatically send an email to any email address using your Gmail account.

The .NET 2.0 framework makes sending emails pretty easy. It contains a System.Net.Mail.MailMessage class which allows you to quickly build an email message.

Before we begin building our message we must add the following namespaces to our class, since we will soon need to make use of some of the classes within these namespaces:

using System.Net.Mail;
using System.Net;

Now lets create a MailMessage object and start building our message. We start by adding a recipient, a subject and a message body as shown below:

// Create a System.Net.Mail.MailMessage object
MailMessage message = new MailMessage();

// Add a recipient
message.To.Add("daveoncsharp@gmail.com");

// Add a message subject
message.Subject = "Email message from Dave on C-Sharp";

// Add a message body
message.Body = "Test email message from www.daveoncsharp.com.";

[continue reading…]

7 comments

Hibernate or Standby Windows Programmatically

Operating System

In this short article I am going to show you how to achieve hibernation or standby of Windows programmatically.

The .NET 2.0 framework introduced the Application.SetSuspendState() method, which allows you to either hibernate or else standby your pc.

This method accepts three parameters – the first parameter “state” is a PowerState enumeration value of either Hibernate or Suspend.

The second parameter “force“, is a boolean value, which when set to true forces the hibernation or standby immediately, and when set to false causes Windows to send a suspend request to every application. It is advisable to use false for this parameter because this allows any running applications to stop gracefully instead of being forced to stop immediately by Windows.

[continue reading…]

2 comments

Recursively Search Directories

Files and Directories

Listing files from directories and sub-directories is a common requirement for many developers. In this short tutorial I will show you how to do this in two different ways.

The Directory.GetFiles() Method

This first method is by far the easier of the two, but to implement this you must be working with Microsoft .NET Framework version 2.0 or later.

The System.IO.Directory class contains a method called GetFiles(). It returns a string array of full file names for each file it finds. It also accepts a number of parameters which allow you to customize your search. Below are examples of the three overloads for GetFiles().

Here we are getting a string array of all the files within the directory “E:\Music\Dire Straits”.

string[] files = Directory.GetFiles("E:\\Music\\Dire Straits");

Now here we are filtering which files to get by file extension – we are getting only the files with an extension of “.mp3” from “E:\Music\Dire Straits.”

string[] files = Directory.GetFiles("E:\\Music\\Dire Straits", "*.mp3");

[continue reading…]

2 comments

Prevent Users Closing Your Windows Form

Windows Forms

Sometimes you might require to stop a user from closing your Windows form – maybe because the form is processing something in the background and you don’t want the user to break that process, or maybe because the user does not have access rights to close your application. Either way this is very simple to implement and I will show you how.

The quick way around this problem would be to hide the form’s control box. The problem with this approach is that the user can still hit Alt+F4 on their keyboard and close your form. You could try to catch the key presses and look for Alt and F4 but I do not recommend this approach.

The cleanest approach to solve this problem would be to subscribe to the Form.FormClosing event and cancel the closing process from there. This might sound a little difficult but it really is not – let me explain.

Subscribing to the Form.FormClosing event

There are two ways you can subscribe to a Form event when using Microsoft Visual Studio – either programmatically or by using Visual Studio’s IDE.

[continue reading…]

4 comments

Windows Forms Event Sequence

Windows Forms

When developing Windows applications using Windows Forms, you will most likely find yourself needing to make use of the form’s events, and this requires you to know when each form event fires. In this short reference article I will show you the sequence of the form events when the Form is being started and when it is being shut down.

Form Start up

Event Description
1. Control.HandleCreated Occurs when a handle is created for the control.
2. Control.BindingContextChanged Occurs when the value of the BindingContext property changes.
3. Form.Load Occurs before a form is displayed for the first time.
4. Control.VisibleChanged Occurs when the Visible property value changes.
5. Form.Activated Occurs when the form is activated in code or by the user.
6. Form.Shown Occurs whenever the form is first displayed.

[continue reading…]

0 comments