Mail Server
Mail Server
Mail Server
Introduction
The power of network programming in .NET platform cannot be denied. Socket programming is
the core of network programming in Windows and Linux, and today the .NET platform
implements it in a powerful way. In this article, we will see the basics of socket programming in
C#. To be precise, I have created a command client and a command server, to communicate
between a remote server and up to 200 clients and send the specified commands to them. As a
sample application, I have created a chat client application that uses this command client/server
to implement chat functions. Before I start explaining my application, let me give you a small
introduction on network programming and sockets taken from the book 'C# network
programming', written by Richard Blum.
Sockets
In socket-based network programming, you don't directly access the network interface device to
send and receive packets. Instead, an intermediary connector is created to handle the
programming interface to the network. Assume that a socket is a connector that connects your
application to a network interface of your computer. For sending and receiving data to and from
the network you should call the socket's methods.
Socket programming in C#
The 'System.Net.Sockets' namespace contains the classes that provide the actual .NET
interface to the low-level Winsock APIs. In network programming, apart from which
programming language to use there are some common concepts like the IP address and port. IP
address is a unique identifier of a computer on a network and port is like a gate through which
applications communicate with each other. In brief, when we want to communicate with a
remote computer or a device over the network, we should know its IP address. Then, we must
open a gate (Port) to that IP and then send and receive the required data.
IP addresses in C#
One of the biggest advantages you will notice in the .NET network library is the way IP
address/port pairs are handled. It is a fairly straightforward process that presents a welcome
improvement over the old, confusing UNIX way. .NET defines two classes in the System.Net
namespace to handle various types of IP address information:
• IPAddress
• IPEndPoint
IPAddress
An IPAddress object is used to represent a single IP address. This value is then used in various
socket methods to represent the IP address. The default constructor for IPAddress is as follows:
Collapse
IPEndPoint
The .NET Framework uses the IPEndPoint object to represent a specific IP address/port
combination. An IPEndPoint object is used when binding sockets to local addresses, or when
connecting sockets to remote addresses.
Connection-oriented and connectionless sockets
The world of IP connectivity revolves around two types of communication: connection-oriented
and connectionless. In a connection-oriented socket, the TCP protocol is used to establish a
session (connection) between two IP address endpoints. There is a fair amount of overhead
involved with establishing the connection, but once it is established, the data can be reliably
transferred between the devices.
Connectionless sockets use the UDP protocol. Because of that no connection information is
required to be sent between the network devices and it is often difficult to determine which
device is acting as a "server", and which is acting as a "client". We will focus on the first type of
socket programming in this article.
Using connection-oriented sockets
In the .NET Framework, you can create connection-oriented communications with remote hosts
across a network. To create a connection-oriented socket, separate sequences of functions must
be used for server programs and client programs:
Server
You have four tasks to perform before a server can transfer data with a client connection:
1. Create a socket.
2. Bind the socket to a local IPEndPoint.
3. Place the socket in listen mode.
4. Accept an incoming connection on the socket.
Creating the server
The first step to constructing a TCP server is to create an instance of the Socket object. The
other three functions necessary for successful server operations are then accomplished by using
the methods of Socket object. The following C# code snippet includes these steps:
Collapse
IPEndPoint ipep =
new IPEndPoint(Ipaddress.Parse("127.0.0.1"), 8000);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
server.Connect(ipep);
This example attempts to connect the socket to the server located at address 127.0.0.1.This is the
IP address of the local host (current computer) and is a loopback IP for testing a network
application without a network. Of course, you can also use hostnames along with the
Dns.Resolve() method in a real network. (Dns is in System.Net namespace). Once the remote
server TCP program accepts the connection request, the client program is ready to transmit data
with the server using the standard Send() and Receive() methods.
//Type
//Sender IP
byte [] senderIPBuffer =
Encoding.ASCII.GetBytes(cmd.SenderIP.ToString());
buffer = new byte [4];
buffer = BitConverter.GetBytes(senderIPBuffer.Length);
this.networkStream.Write(buffer , 0 , 4);
this.networkStream.Flush();
this.networkStream.Write(senderIPBuffer, 0,
senderIPBuffer.Length);
this.networkStream.Flush();
//Sender Name
byte [] senderNameBuffer =
Encoding.Unicode.GetBytes(cmd.SenderName.ToString());
buffer = new byte [4];
buffer = BitConverter.GetBytes(senderNameBuffer.Length);
this.networkStream.Write(buffer , 0 , 4);
this.networkStream.Flush();
this.networkStream.Write(senderNameBuffer, 0,
senderNameBuffer.Length);
this.networkStream.Flush();
//Target
byte [] ipBuffer =
Encoding.ASCII.GetBytes(cmd.Target.ToString());
buffer = new byte [4];
buffer = BitConverter.GetBytes(ipBuffer.Length);
this.networkStream.Write(buffer , 0 , 4);
this.networkStream.Flush();
this.networkStream.Write(ipBuffer , 0 , ipBuffer.Length);
this.networkStream.Flush();
//Meta Data.
byte [] metaBuffer =
Encoding.Unicode.GetBytes(cmd.MetaData);
buffer = new byte [4];
buffer = BitConverter.GetBytes(metaBuffer.Length);
this.networkStream.Write(buffer , 0 , 4);
this.networkStream.Flush();
this.networkStream.Write(metaBuffer, 0, metaBuffer.Length);
this.networkStream.Flush();
The send and receive are bidirectional operations. For example, when we send 4 bytes to the
client, the client should read the 4 bytes. We should repeat this operation until all the sent data is
read. See the receive code of the client here:
Collapse
while ( this.clientSocket.Connected )
{
//Read the command's Type.
Bottom of Form
C# Tutorial
using System;
using System.Windows.Forms;
using System.Text;
using System.Net.Sockets ;
using System.Threading;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
System.Net.Sockets.TcpClient clientSocket = new
System.Net.Sockets.TcpClient();
NetworkStream serverStream = default(NetworkStream);
string readData = null;
public Form1()
{
InitializeComponent();
}
byte[] outStream =
System.Text.Encoding.ASCII.GetBytes(textBox3.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
}
}
The C# Chat Server Program has two sections.
1. C# Chat Server
2. Chat Client
Create the C# Chat Server and C# Chat Client are two separate C# projects and compile and
build the program. Open a DOS Prompt and run the Server Program first and then run the Client
program .
In the Client program, Enter a Chat name and click " Connect to Server " button . Then you can
see the message in the Server program User "Joined Chat Room" . Similarly you can connect
more than one Clients at the same time and start chatting each other. If you plan to run more than
one client, it is better to copy the .exe file in separate folders and run from the .exe file.
C# Tutorial
using System;
using System.Windows.Forms;
using System.Net;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
C# Tutorial
using System;
using System.Net;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
C# Tutorial
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
C# Tutorial
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
mail.IsBodyHtml = true;
string htmlBody;
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.Credentials = new
System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
You have to provide the necessary information like your gmail username and password etc.
How to send email with attachment
from C#
C# simplifies network programming in .Net framework. C# describes various protocols using
communication programming like Socket communications , SMTP mail , UDP , URL etc. The
System.Net classes uses to communicate with other applications by using the HTTP, TCP, UDP,
Socket etc. In the previous program we saw how to SMTP email from C# describes how to send
an email with text body . Here we are sending an email with an attachment.
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:\\containers.xls");
mail.Attachments.Add(attachment);
The following C# source code shows how to send an email with an attachment from a Gmail
address . The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 .
Here using NetworkCredential for password based authentication.
SmtpServer.Credentials =
new System.Net.NetworkCredential("username", "password");
C# Tutorial
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("you attachment
file");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new
System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
You have to provide the necessary information like your gmail username and password etc.
C# Tutorial
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SmtpServer.Port = 587;
SmtpServer.Credentials = new
System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
You have to provide the necessary information like your gmail username and password etc.
C# Tutorial
using System;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
There are two types of communication protocol uses for Socket Programming in C# , they are
TCP/IP ( Transmission Control Protocol/Internet protocol ) Communication and UDP/IP ( User
Datagram Protocol/Internet protocol ) Communication .
In the following section we are going to communicate a C# Server Socket Program and C#
Client Socket Program using TCP/IP Communication Protocol.
C# Tutorial
using System;
using System.Net.Sockets;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(8888);
int requestCount = 0;
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
Console.WriteLine(" >> Server Started");
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> Accept connection from client");
requestCount = 0;
while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0,
(int)clientSocket.ReceiveBufferSize);
string dataFromClient =
System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0,
dataFromClient.IndexOf("$"));
Console.WriteLine(" >> Data from client - " +
dataFromClient);
string serverResponse = "Server response " +
Convert.ToString(requestCount);
Byte[] sendBytes =
Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> exit");
Console.ReadLine();
}
}
}
When you finish coding and build the Server and Client program , First you have to start C#
Server Socket Program from DOS prompt , then you will get a message " Server Started " in
your DOS screen, where the server program is running .
Next step is to start C# Client Socket Program in the same computer or other computers on the
same network . When you start the client program , it will establish a connection to the Server
and get a message in client screen " Client Started " , at the same time you can see a message in
the Server screen " Accept connection from client " .
Now your C# Server Socket Program and C# Client Socket Program is get connected and
communicated . If you want to communicate the Server and Client again , click the button in
theclient program , then you can see new messages in the Server and Client programs displayed.
The above picture shows a Server and Client communication interfaces in C#.
C# Server Socket Program:
The Server Socket Program is done through a C# Console based application . Here the Server is
listening for the Client's request , and when the C# Server gets a request from Client socket , the
Server sends a response to the Client . Click the following link to see in detail of a C# Server
Socket Program.
C# Client Socket Program:
The C# Client Socket Program is a windows based application . When the C# Client program
execute , it will establish a connection to the C# Server program and send request to the Server ,
at the same time it also receive the response from C# Server . Click the following link to see in
detail of C# Client Socket Program.
How to run this program ?
The C# Socket Program has two sections.
1. C# Server Socket Program
2. C# Client Socket Program
When you finish coding and build the Server and Client program , First you have to start C#
Server Socket Program from DOS prompt , then you will get a message "Server Started" in your
DOS screen, where the server program is running .
Next step is to start C# Client Socket Program in the same computer or other computers on the
same network . When you start the client program , it will establish a connection to the Server
and get a message in client screen " Client Started " , at the same time you can see a message in
the Server screen "Accept connection from client" .
Now your C# Server Socket Program and C# Client Socket Program is get connected and
communicated . If you want to communicate the Server and Client again , click the button in
theclient program , then you can see new messages in the Server and Client programs displayed.
C# Tutorial
C# Tutorial
using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
System.Net.Sockets.TcpClient clientSocket = new
System.Net.Sockets.TcpClient();
public Form1()
{
InitializeComponent();
}
When you finish coding and build the Server and Client program , First you have to start C#
Server Socket Program from DOS prompt , then you will get a message " Server Started " in
your DOS screen, where the server program is running .
Next step is to start C# Client Socket Program in the same computer or other computers on the
same network . When you start the client program , it will establish a connection to the Server
and get a message in client screen " Client Started " , at the same time you can see a message in
the Server screen " Accept connection from client " .
Now your C# Server Socket Program and C# Client Socket Program is get connected and
communicated . If you want to communicate the Server and Client again , click the button in
theclient program , then you can see new messages in the Server and Client programs displayed.
C# Tutorial
using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
int counter = 0;
serverSocket.Start();
Console.WriteLine(" >> " + "Server Started");
counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> " + "Client No:" +
Convert.ToString(counter) + " started!");
handleClinet client = new handleClinet();
client.startClient(clientSocket, Convert.ToString(counter));
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> " + "exit");
Console.ReadLine();
}
}
while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0,
(int)clientSocket.ReceiveBufferSize);
dataFromClient =
System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0,
dataFromClient.IndexOf("$"));
Console.WriteLine(" >> " + "From client-" + clNo +
dataFromClient);
rCount = Convert.ToString(requestCount);
serverResponse = "Server to clinet(" + clNo + ") " +
rCount;
sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(" >> " + ex.ToString());
}
}
}
}
}
Create C# Multithreaded Server Socket Program and C# Multi Threaded Client Socket Program
in two separate C# projects .
After compile and build the projects, open a DOS prompt and run the Server Program first.Then
you will get the message "Server started" in Server side.
Next you start the Client program , then you can see the message from Server . You can start
more than one client at the same time and communicate with the Server program. If you plan to
run more than one client, it is better to run the client program's .exe file to copy in separate
folders and run from .exe file.
C# Tutorial
using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
System.Net.Sockets.TcpClient clientSocket = new
System.Net.Sockets.TcpClient();
NetworkStream serverStream;
public Form1()
{
InitializeComponent();
}
Create C# Multi Threaded Server Socket Program and C# Multithreaded Client Socket Program
in two separate C# projects .
After compile and build the projects, open a DOS prompt and run the Server Program first.Then
you will get the message "Server started" in Server side.
Next you start the Client program , then you can see the message from Server . You can start
more than one client at the same time and communicate with the Server program. If you plan to
run more than one client, it is better to run theclient program's .exe file to copy in separate
folders and run from .exe file.
Each of the fields can contain JSF like expressions. For example:
<mail to='#{initiator}' subject='websale' text='your websale of #{quantity}
#{item} was approved' />
For more information about expressions, see Section 18.3, “Expressions”.
There are two attribute to specify recipients: actors and to. The to attribute should resolve to a
semicolon separated list of email addresses. The actors attribute should resolve to a semicolon
separated list of actorIds. Those actorIds will be resolved to email addresses with by means of
address resolving.
<mail to='[email protected]' subject='urgent' text='the mailserver is
down :-)' />
For more about how to specify recipients, see Section 16.3, “Specifying mail recipients”
Mails can be defined in templates and in the process you can overwrite properties of the
templates like this:
<mail template='sillystatement' actors="#{president}" />
More about templates can be found in Section 16.4, “Mail templates”
16.1.2. Mail node
Just the same as with mail actions, sending of an email can also be modelled as a node. In that
case, the runtime behaviour is just the same, but the email will show up as a node in the process
graph.
The attributes and elements supported by mail nodes are exactly the same as with the mail
actions.
<mail-node name="send email" to="#{president}" subject="readmylips"
text="nomoretaxes">
<transition to="the next node" />
</mail-node>
Mail nodes should have exactly one leaving transition.
16.1.3. Task assign mails
A notification email can be send when a task gets assigned to an actor. Just use the
notify="yes" attribute on a task like this:
<task-node name='a'>
<task name='laundry' swimlane="grandma" notify='yes' />
<transition to='b' />
</task-node>
Setting notify to yes, true or on will cause jBPM to send an email to the actor that will be
assigned to this task. The email is based on a template (see Section 16.4, “Mail templates”) and
contains a link to the task page of the web application.
16.1.4. Task reminder mails
Similarly as with assignments, emails can be send as a task reminder. The reminder element in
jPDL is based upon the timer. The most common attributes will be the duedate and the repeat.
The only difference is that no action has to be specified.
<task-node name='a'>
<task name='laundry' swimlane="grandma" notify='yes'>
<reminder duedate="2 business days" repeat="2 business hours"/>
</task>
<transition to='b' />
</task-node>
<mail-template name='task-assign'>
<actors>#{taskInstance.actorId}</actors>
<subject>Task '#{taskInstance.name}'</subject>
<text><![CDATA[Hi,
Task '#{taskInstance.name}' has been assigned to you.
Go for it: #{BaseTaskListURL}#{taskInstance.id}
Thanks.
---powered by JBoss jBPM---]]></text>
</mail-template>
<mail-template name='task-reminder'>
<actors>#{taskInstance.actorId}</actors>
<subject>Task '#{taskInstance.name}' !</subject>
<text><![CDATA[Hey,
Don't forget about #{BaseTaskListURL}#{taskInstance.id}
Get going !
---powered by JBoss jBPM---]]></text>
</mail-template>
</mail-templates>
As you can see in this example (BaseTaskListURL), extra variables can be defined in the mail
templates that will be availble in the expressions.
The resource that contains the templates should be configured in the jbpm.cfg.xml like this:
<jbpm-configuration>
...
<string name="resource.mail.templates" value="jbpm.mail.templates.xml" />
</jbpm-configuration>
Prev Up
Prev Up