Tutorial Requirements:
- C# IDE (Visual Studio 2008 used in this tutorial)
- .NET Framework 2.0
For this application it is needed to declare one additional namespace:
// Accessing an additional namespace for mail sending. using System.Net.Mail;
So, here are the steps needed to be performed:
1. Create a standard C# Windows Forms application:

2. Add some labels and text boxes and two buttons to the form, so the form looks like this:

As you see, the current text in the text boxes is the name of each text box. You have to change the names to the corresponding boxes to make the code work. However, you can leave the default names, but don't forget to change the code.
3. Change the passBox PasswordChar property to '*', so when you will enter the password for the mail box, no one could see it:

4. For the contentBox change the AcceptsReturn and AcceptsTab properties to true, so when you will enter the message, you can use the TAB and ENTER keys to format the text:

Now, when the form is ready, let's pass to the code.
1. As the System.Net.Mail namespace was decalred at the beginning of the tutorial, we can pass to the sending code (for the Send button):
private void button1_Click(object sender, EventArgs e)
{
// To avoid situations, when the program crashes because server rejection or
// invalid data, an exception handling mechanism is created.
try
{
// Creating a new SMTP Client. The server URL/IP is indicated as
// sendServer.Text (that is the text box with the data).
SmtpClient client = new SmtpClient(sendServer.Text);
// Creating a new mail message. The sender and receiver are
// indicated as sendFrom.Text and SendTo.Text
// (these are the text boxes with the data).
MailMessage message = new MailMessage(sendFrom.Text, sendTo.Text);
// The message body is the message content provided in the
// contentBox.
message.Body = contentBox.Text;
// The message subject is located in the subjectBox.
message.Subject = subjectBox.Text;
// To be able to send the message, it is necessary to provide the
// credentials on the server. The username is located in the userBox
// and the password is located in the passBox.
client.Credentials = new System.Net.NetworkCredential(userBox.Text, passBox.Text);
// Some servers require a specific port to connect,
// so it is specified in the serverPort text box.
if (serverPort.Text != null)
client.Port = System.Convert.ToInt32(serverPort.Text);
// Send the message.
client.Send(message);
}
// This catches the exceptions, if any.
catch (Exception ex)
{
// Show a message, explaining the problem.
MessageBox.Show("Cannot send message: " + ex.Message);
}
}
2. There is also code for the 'Clear Fields' button:
// Clear every field.
sendServer.Clear();
serverPort.Clear();
sendTo.Clear();
sendFrom.Clear();
userBox.Clear();
passBox.Clear();
subjectBox.Clear();
contentBox.Clear();
The application is ready.
This application is using SMTP (Simple Mail Transfer Protocol) to send messages, so you must have access toy uor mail provider's SMTP server (which is available for most mail services). The SMTP server address usually looks like this:
smtp.yourmailprovider.domain
The most commonly used SMTP port is:
2525
Attached File(s)
-
sendEmail.zip (41.82K)
Number of downloads: 8222





MultiQuote





|