I will demonstrate how to send email by using SMTP and Google's GMail account.
First we need to have a few prerequisites fulfilled.
- The SMTP host server that you use to send e-mail. We'll use GMail, and its host address is "smtp.gmail.com", and port 587.
- Do we need to enable SSL? In this case, yes we are required to use SSL, because GMail uses it.
- Credentials for authentication, if required by the SMTP server. GMail needs to have those, so we need to pass our username and password.
- The e-mail address of the sender. This is what goes to From-field.
- The e-mail address or addresses of the recipients. This is what goes to To-field.
- The message content, whatever you want to send.
Basically the flow of code goes like this:
- First, we create a simple email message using System.Net.Mail.MailMessage.
- You can set messge type to HTML if you want, in this sample it is commented out.
- Also you can add attachments, there's a commented line in the code that does it.
- Then we initialize SMTP-client using GMail SMTP address and port 587.
- GMail uses SSL, so we need to enable it. Also we need to authenticate to our account, and we need to set NetworkCredentials for that.
- Then we just send message and that's it (hopefully) :)
- Possible exceptions are caught and we can use that information to take a look what went wrong.
If you want further reading, check out MSDN's SmtpClient Class
1 using System;
2 using System.Net.Mail;
3 using System.Net;
4
5 namespace SmtpTest
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 Console.WriteLine("Sending test message...");
12 CreateTestMessage("smtp.gmail.com", 587);
13 Console.WriteLine("OK");
14 Console.ReadLine();
15 }
16
17 public static void CreateTestMessage(string server, int port)
18 {
19 // message info
20 string to = "*****.*****@gmail.com"; // put recipient
21 string from = "*****.*****@gmail.com"; // put sender email here
22 string subject = "SMTP client using GMail."; // message subject
23 string body = "This is our test message, w00t!"; // message body
24
25 MailMessage message = new MailMessage(from, to, subject, body);
26
27 // Uncomment rows below if you want to send attachment
28 //message.Attachments.Add(
29 // new Attachment(@"C:\AttachmentFolder\AttachmentFile.txt"));
30
31 // Uncomment row below if you want to send HTML-message
32 //message.IsBodyHtml = true;
33
34 // Initialize SMTP client
35 SmtpClient client = new SmtpClient(server, port);
36
37 // enable SSL
38 client.EnableSsl = true;
39
40 // Credentials are necessary if the server requires the client
41 // to authenticate before it will send e-mail on the client's behalf.
42 //client.Credentials = CredentialCache.DefaultNetworkCredentials;
43 // Here we define our network credetials (username and password)
44 NetworkCredential SmtpUserInfo =
45 new NetworkCredential("your.username@gmail.com", "password");
46
47 // Set the credentials
48 client.Credentials = SmtpUserInfo;
49
50 try
51 {
52 // send message
53 client.Send(message);
54 }
55 // catch possible exceptions
56 catch (SmtpException se)
57 {
58 Console.WriteLine(
59 "SmtpException caught in CreateTestMessage(): {0}", se.ToString());
60 }
61 catch (Exception e)
62 {
63 Console.WriteLine(
64 "Exception caught in CreateTestMessage(): {0}", e.ToString());
65 }
66 }
67
68 }
69 }