Communicating with an SMTP server and sending emails in .NET
Overview
This article will demonstrate how to communicate with an SMTP server and how to send emails in NET using Email Factory for .NET.
Code Example
Download licenseEmail.txt
| Download SMTPEmailEx.cs
| Download SMTPEmailEx.vb
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using Jscape.Email;
5:
6: namespace SMTPNet
7: {
8: class SMTPEx
9: {
10: Smtp smtp = null;
11: public SMTPEx(string server, string username, string password)
12: {
13: smtp = new Smtp(server);
14: smtp.LicenseKey = null; // copy license from license.txt here
15: smtp.Login(username, password);
16: smtp.ConnectedEvent +=
17: new Smtp.ConnectedEventHandler(smtp_ConnectedEvent);
18: smtp.DisconnectedEvent +=
19: new Smtp.DisconnectedEventHandler(smtp_DisconnectedEvent);
20: }
21:
22: private void smtp_DisconnectedEvent(object sender, SmtpDisconnectedEventArgs e)
23: {
24: Console.WriteLine("Disconnected from : " + e.Host);
25: }
26:
27: private void smtp_ConnectedEvent(object sender, SmtpConnectedEventArgs e)
28: {
29: Console.WriteLine("Connected to : " + e.Host);
30: }
31:
32: public void openConnection()
33: {
34: smtp.Connect();
35: }
36:
37: public void closeConnection()
38: {
39: smtp.Disconnect();
40: }
41:
42: public string IssueCommand(string command)
43: {
44: return smtp.IssueCommand(command);
45: }
46:
47: public void sendMessage(string from, string to, string subject, string body)
48: {
49: EmailMessage message = new EmailMessage(to, from);
50: message.Subject = subject;
51: message.SetBody(body);
52: smtp.Send(message);
53: }
54:
55: public void sendHtmlMessage(string to, string from, string subject,
56: string textBody, string htmlBody,
57: Dictionary<string, string> resources)
58: {
59: HtmlEmailMessage ret = new HtmlEmailMessage();
60: ret.To = to;
61: ret.From = from;
62: ret.Subject = subject;
63: ret.SetTextBody(textBody);
64: ret.SetHtmlBody(htmlBody);
65: foreach (KeyValuePair<string, string> pair in resources)
66: ret.Embed(pair.Value, pair.Key);
67: smtp.Send(ret);
68: }
69:
70:
71:
72: static void Main(string[] args)
73: {
74: try
75: {
76: string server = "[server]";
77: string username = "[username]";
78: string password = "[password]";
79: SMTPEx e = new SMTPEx(server, username, password);
80: e.openConnection();
81: string response = e.IssueCommand("HELO 10.0.0.1");
82: Dictionary<string, string> dict = new Dictionary<string, string>();
83: dict.Add("imageUrl1", "imageKey1");
84: dict.Add("imageUrl2", "imageKey2");
85: dict.Add("imageUrl3", "imageKey3");
86: e.sendHtmlMessage("to@to.com", "from@from.com", "subject",
87: "testBody", "htmlBody", dict);
88: e.closeConnection();
89: }
90: catch (Exception e)
91: {
92: Console.WriteLine(e.Message);
93: }
94:
95: Console.WriteLine("Press any key to exit ... ");
96: Console.Read();
97: }
98: }
99: }
Lines 1-4 : Necessary import statements.
Lines 10-20 : Initialize Smtp instance and connect event handlers.
Lines 22-30 : Event handlers for connected/disconnected events.
Lines 32-40 : Methods for opening and closing connections.
Lines 42-45 : Issue a SMTP command to the server.
Lines 47-53 : Send an email message.
Lines 55-68 : Send an html message.
Line 79 : Initialize SMTPEx instance.
Line 81 : Issue a command to the server.
Lines 82-85 : Create a dictionary instance.
Lines 86-87 : Send an html message.
Comments