My company uses IBM DOMINO as “mail server”, so, during the development of the procedure for managing the offers to the customer, we need to store the email sent with the procedure (c# and IIS, INTRANET solution) into the DOMINO “.nsf” mail system.
First of all, DOMINO can be accessed using IMAP4 protocol but there is no the “sent mail” folder, because it’s a view of the ”inbox” folder, so, we had to create a new folder named “quotes sent”.
Second, we developed using c# a simple class to upload the email using the IMAP4 protocol.
Take a look here for the protocol specification in particular to the “APPEND” command, used to store email into an IMAP4 server.
Steps to achieve the result are the follow:
- login ino the IMAP4 server providing the username and password.
- append the email message into the folder using “APPEND” commad
- close the connection
Here the complete C# class to use into your application:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace M10IMAPC
{
public class M10IMAPC
{
private string CRLF = "\r\n";
private string _IMAP_SERVER;
private int _TCP_PORT;
private TcpClient _THE_SERVER;
private NetworkStream _THE_STREAM;
public string THE_ERROR;
public M10IMAPC(string IMAP_SERVER, int TCP_PORT)
{
_IMAP_SERVER = IMAP_SERVER;
_TCP_PORT = TCP_PORT;
}
public bool OPEN_CONNECTION(string USER_ID, string PASSWORD)
{
try
{
_THE_SERVER = new TcpClient(_IMAP_SERVER, _TCP_PORT);
_THE_STREAM = _THE_SERVER.GetStream();
}
catch (Exception ex)
{
THE_ERROR = ex.Message;
return false;
}
Byte[] bytes = new byte[512];
int bytesRead = _THE_STREAM.Read(bytes, 0, bytes.Length);
string ServerResponse = Encoding.UTF8.GetString(bytes, 0, bytesRead);
if (ServerResponse.Length < 4)
{
THE_ERROR = "server error";
return false;
}
if (!ServerResponse.StartsWith("* OK"))
{
THE_ERROR = "protocol error";
return false;
}
// OK TRY TO LOGIN
string ClientResponse = "L001 " + "LOGIN " + USER_ID + " " + PASSWORD + CRLF;
byte[] SendData = System.Text.Encoding.UTF8.GetBytes(ClientResponse);
_THE_STREAM.Write(SendData, 0, SendData.Length);
bytes = new byte[512];
bytesRead = _THE_STREAM.Read(bytes, 0, bytes.Length);
ServerResponse = Encoding.UTF8.GetString(bytes, 0, bytesRead);
if (ServerResponse.Length < 7)
{
THE_ERROR = "server error";
return false;
}
if (!ServerResponse.StartsWith("L001 OK"))
{
THE_ERROR = "protocol error";
return false;
}
return true;
}
public bool CLOSE_CONNECTION()
{
// OK TRY TO LOGOUT
try
{
string ClientResponse = "L999 " + "LOGOUT" + CRLF;
byte[] SendData = System.Text.Encoding.UTF8.GetBytes(ClientResponse);
_THE_STREAM.Write(SendData, 0, SendData.Length);
_THE_STREAM.Close();
_THE_SERVER.Close();
}
catch (Exception ex)
{
THE_ERROR = ex.Message;
return false;
}
return true;
}
public bool APPEND_MAIL(string FOLDER, string EMAIL)
{
try
{
System.IO.FileInfo the_file = new System.IO.FileInfo(EMAIL);
// OK TRY TO APPEND EMAIL
string ClientResponse = "L010 " + "APPEND " + "\"" + FOLDER + "\"" + @" (\Seen) {" + the_file.Length.ToString() + "}" + CRLF;
byte[] SendData = System.Text.Encoding.UTF8.GetBytes(ClientResponse);
_THE_STREAM.Write(SendData, 0, SendData.Length);
byte[] bytes = new byte[512];
int bytesRead = _THE_STREAM.Read(bytes, 0, bytes.Length);
string ServerResponse = Encoding.UTF8.GetString(bytes, 0, bytesRead);
if (ServerResponse.Length < 1)
{
THE_ERROR = "server error";
return false;
}
if (!ServerResponse.StartsWith("+"))
{
THE_ERROR = "protocol error";
return false;
}
// SEND EMAIL
System.IO.FileStream inFile;
byte[] binaryData;
try
{
inFile = new System.IO.FileStream(EMAIL,
System.IO.FileMode.Open,
System.IO.FileAccess.Read);
binaryData = new Byte[inFile.Length];
bytesRead = inFile.Read(binaryData, 0,
(int)inFile.Length);
inFile.Close();
_THE_STREAM.Write(binaryData, 0, binaryData.Length);
}
catch (System.Exception exp)
{
}
ClientResponse = CRLF + ".";
SendData = System.Text.Encoding.UTF8.GetBytes(ClientResponse);
_THE_STREAM.Write(SendData, 0, SendData.Length);
bytes = new byte[512];
bytesRead = _THE_STREAM.Read(bytes, 0, bytes.Length);
ServerResponse = Encoding.UTF8.GetString(bytes, 0, bytesRead);
if (ServerResponse.Length < 7)
{
THE_ERROR = "server error";
return false;
}
if (ServerResponse.IndexOf("L010 OK") == -1)
{
THE_ERROR = "protocol error";
return false;
}
}
catch (Exception ex)
{
THE_ERROR = ex.Message;
return false;
}
return true;
}
}
}
HOW TO USE:
// init SmtpClient class to send the email
SmtpClient THE_SMTP = new SmtpClient("server-1", 25);
MailAddress THE_ADDRESS_FROM = new MailAddress("xyz@xyz.com","sales dept.");
MailMessage THE_MAIL_MESSAGE = new MailMessage();
THE_MAIL_MESSAGE.From = "xyz@xyz.com";
THE_MAIL_MESSAGE.To.Add(new MailAddress("mno@mno.com");
THE_MAIL_MESSAGE.IsBodyHtml = true;
THE_MAIL_MESSAGE.Subject = "subject";
THE_MAIL_MESSAGE.Body = "test email";
// send the email
THE_SMTP.Send(THE_MAIL_MESSAGE);
// save the email message into the file system
THE_SMTP.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
THE_SMTP.PickupDirectoryLocation = MapPath("~") + @"/TEMP";
THE_SMTP.Send(THE_MAIL_MESSAGE); // mail will be saved into "temp" folder with ".eml" extension
// upload email message into DOMINO mail server using M10IMAPC
M10IMAPC THE_IMAP_CLIENT = new M10IMAPC("Server-1", 143); // init the class with mail server name and port
string email_name = string.Empty;
if (THE_IMAP_CLIENT.OPEN_CONNECTION("user_name", "user_password"))
{
System.IO.DirectoryInfo THE_DIR = new System.IO.DirectoryInfo(MapPath("~") + @"/TEMP/");
System.IO.FileInfo[] THE_EMAIL_FILES = THE_DIR.GetFiles("*.eml");
for (int i = 0; i < THE_EMAIL_FILES.Length; i++)
{
if (!THE_IMAP_CLIENT.APPEND_MAIL("folder name", THE_EMAIL_FILES[i].FullName))
{
// ok
}
}
THE_IMAP_CLIENT.CLOSE_CONNECTION();
}
else
{
// no connection, check user name and password
}
NOTE: I haven't used the ssl connection because my programs runs into our domain.
Forgot ... This class can be used with any "mail server" IMPA4 compliant.
Dante Marchesi