Dante’s blog

March 21, 2010

Highlight DataGridView Row using CSS

Filed under: aspx, CSS — Tags: , , , — Dante @ 11:54 am

It’s very simple to highlight a row in a “DataGridView” using the “CSS”. 

In a datagrid you can define the property “RowStyle” and assign a style for each line. 

Here a simple example of datagrid with the property “RowStyle” defined (see row 21): 


When “IIS” generates the HMTL page, The “datgridview” is transformed into a HTML table and the code looks like this (see row 2):

 

<asp:GridView ID="LISTA" Width="100%" runat="server"
AutoGenerateColumns="False" CellPadding="2" CellSpacing="3" GridLines="None"
AllowPaging="True" AllowSorting="True"
OnPageIndexChanging="LISTA_PageIndexChanging"
CaptionAlign="Top" PageSize="20" onsorting="LISTA_Sorting">
<Columns>
<asp:TemplateField HeaderText="Sel">
<ItemStyle CssClass="ROW_CENTER" Width="5%" />
<HeaderStyle CssClass="LIST_HEADER_CENTER" />
<ItemTemplate>
<asp:CheckBox runat="server" ID="The_Selection"> </asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="qte_user" HeaderText="Manager"
SortExpression="qte_user" HtmlEncode="False">
<ItemStyle CssClass="ROW_CENTER" Width="5%" />
<HeaderStyle CssClass="LIST_HEADER_CENTER" />
</asp:BoundField>
</Columns>
<PagerStyle Font-Size="Smaller" Font-Names="Verdana" Font-Underline="True" />
<RowStyle CssClass="THE_ROW_STYLE" />
<PagerSettings Mode="NumericFirstLast" FirstPageText=""
LastPageText="Last page" NextPageText="" Position="TopAndBottom"
PreviousPageText="" />
</asp:GridView>

 

...cut the code
<tr class="THE_ROW_STYLE">
<td class="ROW_CENTER" style="width:5%;">
<input id="LISTA_ctl04_The_Selection" type="checkbox" name="LISTA$ctl04$The_Selection" />
</td>
<td class="ROW_CENTER" style="width:5%;">xxxxxx</td>
</tr>
...cut the code

 

Now you can use “CSS” classes to highlight the rows at each “mouse hover”.

See the “CSS” classes to do this:


.THE_ROW_STYLE
{
	border: none 0;
	font-family: Arial, Verdana, Tahoma;
	font-size: 8pt;
	background-color: #FFFFFF;
}

.THE_ROW_STYLE:hover
{
	border: none 0;
	font-family: Arial, Verdana, Tahoma;
	font-size: 8pt;
	background-color: #e6e67d;
}

Marchesi Dante

March 17, 2010

Images rotator with javascript

Filed under: HTML, Java Script — Tags: , — Dante @ 1:41 pm

A little “javascript” to add in a HTML page an images rotator.


var images = new Array(10);
var index = 0;
function load_images(the_path, the_prefix, images_count)
{
var image_path_name = the_path + the_prefix;
var image_name;
var i = 1;
for (i = 1; i <= parseInt(images_count); i ++)
{
if (i < 10)
{
image_name = image_path_name + '0' + i + '.jpg';
}
else
{
image_name = image_path_name + i + '.jpg';
}
images[parseInt(i)-1] = image_name;
}
setInterval("Show_Image()",5000);
}
function Show_Image()
{
index = index + parseInt(1);
if (index > 9)
{
index = 0;
}
My_Image = document.getElementById("the_splash");
My_Image.src = images[index];
}

The function takes 3 parameters:

1) The path where the images are stored, for example “images/”.
2) The prefix of the images, for example. “gira”. The prefix of the images is used to group all the images that will rotate.
3) The number of images to display, for example “10″.

The BODY section of the HTML code to use the script:


<body onload="load_images('images/','gira',10)">
<div id="splash">
<asp:Image runat="server" ID="the_splash" />
</div>
</body>

Marchesi Dante

March 16, 2010

MX record lookup with c#

Filed under: c#, Email protocols, M10MAIL — Tags: , — Dante @ 8:43 am

In 2005 I started to develop, for my hobby, a mail server, to date I have not yet completed.
Sooner or later it will complete.
However, thanks to. NET Framework, I have almost everything I need to carry out my ambitious project.
Almost, because in reality a thing is not feasible with the classes of the framework. I hope it will be possible in the future.
Thing missing is the ability to resolve the mail-exchanger for a domain, the MX record of a DNS query.
The framework provides, in the “namespace” System.Net the class “Dns” but this can only be used to resolve the name of a server (giving the ip address) or to find the ip address (giving the server name).
So, if I need to send an email to “example@wordpress.com” I must know the mail server of “wordpress.com”. Using the utility “nslookup” given with all Microsoft O.S. (xp for example) you can lookup the mail server, in this case is “mail.automatic.com”, see the pic. 

But I need that this operation is done by a program. 

How described in RFC1035: “The goal of domain names is to provide a mechanism for naming resources in such a way that the names are usable in different hosts, networks, protocol families, internets, and administrative organizations.” See here for all details.

These are the two classes develop to have the MX lookup. I assume that you know about sockets and threading  programming, otherwise this code may seem difficult to understand.


using System;
using System.IO;
using System.Diagnostics;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Threading;
using System.Text;
using System.Net;
using System.Net.Sockets;
public class Query
{
//Build a DNS query buffer according to RFC 1035 4.1.1 e 4.1.2
private int id;
private int flags;
private int QDcount;
private int ANcount;
private int NScount;
private int ARcount;
private string Qname;
private int Qtype;
private int Qclass;
public byte[] buf;
public Query(int ID, string query, int qtype)
{
//init vectors with given + default values
id = ID;
flags = 256;
QDcount = 1;
ANcount = 0;
NScount = 0;
ARcount = 0;
Qname = query;
Qtype = qtype;
Qclass = 1; //Internet = IN = 1
&lt;p&gt; //build a buffer with formatted query data
//
//header information (16 bit padding
buf = new byte[12 + Qname.Length + 2 + 4];
buf[0] = (byte)(id / 256);
buf[1] = (byte)(id - (buf[0] * 256));
buf[2] = (byte)(flags / 256);
buf[3] = (byte)(flags - (buf[2] * 256));
buf[4] = (byte)(QDcount / 256);
buf[5] = (byte)(QDcount - (buf[4] * 256));
buf[6] = (byte)(ANcount / 256);
buf[7] = (byte)(ANcount - (buf[6] * 256));
buf[8] = (byte)(NScount / 256);
buf[9] = (byte)(NScount - (buf[8] * 256));
buf[10] = (byte)(ARcount / 256);
buf[11] = (byte)(ARcount - (buf[10] * 256));
//QNAME (RFC 1035 4.1.2)
//no padding
string[] s = Qname.Split('.');
int index = 12;
foreach (string str in s)
{
buf[index] = (byte)str.Length;
index++;
byte[] buf1 = Encoding.ASCII.GetBytes(str);
buf1.CopyTo(buf, index);
index += buf1.Length;
}
//add root domain label (chr(0))
buf[index] = 0;
//add Qtype and Qclass (16 bit values)
index = buf.Length - 4;
buf[index] = (byte)(Qtype / 256);
buf[index + 1] = (byte)(Qtype - (buf[index] * 256));
buf[index + 2] = (byte)(Qclass / 256);
buf[index + 3] = (byte)(Qclass - (buf[index + 2] * 256));
}
}
public class C_DNSquery
{
public StringCollection result = new StringCollection();
public int Error = 0;
public string ErrorTxt = "undefined text"
public bool Done = false;&lt;br /&gt;<br />
public UdpClient udpClient;&lt;br /&gt;<br />
private string DNS;&lt;br /&gt;<br />
private string Query;&lt;br /&gt;<br />
private int Qtype;&lt;/p&gt;<br />
&lt;p&gt; public bool IS_BLACKLIST_QUERY = false;&lt;/p&gt;<br />
&lt;p&gt; public C_DNSquery(string IPorDNSname, string query, int type)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
DNS = IPorDNSname;&lt;br /&gt;<br />
Query = query;&lt;br /&gt;<br />
Qtype = type;&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; public void doTheJob()&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//-------------------------------------------------------&lt;br /&gt;<br />
//check if provided DNS contains an IP address or a name&lt;br /&gt;<br />
//-------------------------------------------------------&lt;br /&gt;<br />
IPAddress ipDNS;&lt;br /&gt;<br />
System.Net.IPHostEntry he;&lt;br /&gt;<br />
try&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//try to parse an IPaddress&lt;br /&gt;<br />
ipDNS = IPAddress.Parse(DNS);&lt;br /&gt;<br />
}&lt;br /&gt;<br />
catch (FormatException e)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//format error, probably is a FQname, try to resolve it&lt;br /&gt;<br />
try&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//try to resolve the hostname&lt;br /&gt;<br />
he = System.Net.Dns.GetHostByName(DNS);&lt;br /&gt;<br />
}&lt;br /&gt;<br />
catch&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//Error, invalid server name or address&lt;br /&gt;<br />
Error = 98;&lt;br /&gt;<br />
ErrorTxt = &amp;quot;Invalid server name:&amp;quot; + DNS;&lt;br /&gt;<br />
Done = true;&lt;br /&gt;<br />
return;&lt;br /&gt;<br />
}&lt;br /&gt;<br />
//OK, get the first server address&lt;br /&gt;<br />
ipDNS = he.AddressList[0];&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; //---------------------------------------------&lt;br /&gt;<br />
//Query the DNS server&lt;br /&gt;<br />
//---------------------------------------------&lt;br /&gt;<br />
//our current thread ID is used to match the reply with this process&lt;br /&gt;<br />
//Query myQuery = new Query(System.AppDomain.GetCurrentThreadId(), Query, Qtype);&lt;br /&gt;<br />
Query myQuery = new Query(System.Threading.Thread.CurrentThread.ManagedThreadId, Query, Qtype);&lt;/p&gt;<br />
&lt;p&gt; //data buffer for query return value&lt;br /&gt;<br />
Byte[] recBuf;&lt;/p&gt;<br />
&lt;p&gt; //use UDP protocol to connect&lt;br /&gt;<br />
//UdpClient udpClient = new UdpClient();&lt;br /&gt;<br />
udpClient = new UdpClient();&lt;/p&gt;<br />
&lt;p&gt; do&lt;br /&gt;<br />
{&lt;br /&gt;<br />
try&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//connect to given nameserver, port 53 (DNS)&lt;br /&gt;<br />
udpClient.Connect(DNS, 53);&lt;/p&gt;<br />
&lt;p&gt; //send query&lt;br /&gt;<br />
udpClient.Send(myQuery.buf, myQuery.buf.Length);&lt;/p&gt;<br />
&lt;p&gt; //IPEndPoint object allow us to read datagrams..&lt;br /&gt;<br />
//..selecting only packet coming from our nameserver and port&lt;br /&gt;<br />
IPEndPoint RemoteIpEndPoint = new IPEndPoint(ipDNS, 53);&lt;/p&gt;<br />
&lt;p&gt; //Blocks until a message returns on this socket from a remote host.&lt;br /&gt;<br />
recBuf = udpClient.Receive(ref RemoteIpEndPoint);&lt;/p&gt;<br />
&lt;p&gt; udpClient.Close();&lt;br /&gt;<br />
}&lt;br /&gt;<br />
catch (Exception e)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//connection error&lt;br /&gt;<br />
//probably a wrong server address&lt;br /&gt;<br />
udpClient.Close();&lt;br /&gt;<br />
Error = 99;&lt;br /&gt;<br />
ErrorTxt = e.Message + &amp;quot;(server:&amp;quot; + DNS + &amp;quot;)&amp;quot;;&lt;br /&gt;<br />
Done = true;&lt;br /&gt;<br />
return;&lt;br /&gt;<br />
}&lt;br /&gt;<br />
//repeat until we get the reply with our threadID&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; //while (System.AppDomain.GetCurrentThreadId() != ((recBuf[0] * 256) + recBuf[1]));&lt;br /&gt;<br />
while (System.Threading.Thread.CurrentThread.ManagedThreadId != ((recBuf[0] * 256) + recBuf[1]));&lt;/p&gt;<br />
&lt;p&gt; //---------------------------------------------&lt;br /&gt;<br />
//Check the DNS reply&lt;br /&gt;<br />
//---------------------------------------------&lt;/p&gt;<br />
&lt;p&gt; //check if bit QR (Query response) is set&lt;br /&gt;<br />
if (recBuf[2] &amp;lt; 128)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//response byte not set (probably a malformed packet)&lt;br /&gt;<br />
Error = 2;&lt;br /&gt;<br />
ErrorTxt = &amp;quot;Query response bit not set&amp;quot;;&lt;br /&gt;<br />
Done = true;&lt;br /&gt;<br />
return;&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; //check if RCODE field is 0&lt;br /&gt;<br />
if ((recBuf[3] &amp;amp; 15) &amp;gt; 0)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//DNS server error, invalid reply&lt;br /&gt;<br />
switch (recBuf[3] &amp;amp; 15)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
case 1:&lt;br /&gt;<br />
Error = 31;&lt;br /&gt;<br />
ErrorTxt = &amp;quot;Format error. The nameserver was unable to interpret the query&amp;quot;;&lt;br /&gt;<br />
break;&lt;br /&gt;<br />
case 2:&lt;br /&gt;<br />
Error = 32;&lt;br /&gt;<br />
ErrorTxt = &amp;quot;Server failure. The nameserver was unable to process the query.&amp;quot;;&lt;br /&gt;<br />
break;&lt;br /&gt;<br />
case 3:&lt;br /&gt;<br />
Error = 33;&lt;br /&gt;<br />
ErrorTxt = &amp;quot;Name error. Check provided domain name!!&amp;quot;;&lt;br /&gt;<br />
break;&lt;br /&gt;<br />
case 4:&lt;br /&gt;<br />
Error = 34;&lt;br /&gt;<br />
ErrorTxt = &amp;quot;Not implemented. The name server does not support the requested query&amp;quot;;&lt;br /&gt;<br />
break;&lt;br /&gt;<br />
case 5:&lt;br /&gt;<br />
Error = 35;&lt;br /&gt;<br />
ErrorTxt = &amp;quot;Refused. The name server refuses to reply for policy reasons&amp;quot;;&lt;br /&gt;<br />
break;&lt;br /&gt;<br />
default:&lt;br /&gt;<br />
Error = 36;&lt;br /&gt;<br />
ErrorTxt = &amp;quot;Unknown. The name server error code was: &amp;quot; + System.Convert.ToString((recBuf[3] &amp;amp; 15));&lt;br /&gt;<br />
break;&lt;br /&gt;<br />
}&lt;br /&gt;<br />
Done = true;&lt;br /&gt;<br />
return;&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; //OK, now we should have valid header fields&lt;br /&gt;<br />
int QDcnt, ANcnt, NScnt, ARcnt;&lt;br /&gt;<br />
int index;&lt;/p&gt;<br />
&lt;p&gt; QDcnt = (recBuf[4] * 256) + recBuf[5];&lt;br /&gt;<br />
ANcnt = (recBuf[6] * 256) + recBuf[7];&lt;br /&gt;<br />
NScnt = (recBuf[8] * 256) + recBuf[9];&lt;br /&gt;<br />
ARcnt = (recBuf[10] * 256) + recBuf[11];&lt;br /&gt;<br />
index = 12;&lt;/p&gt;<br />
&lt;p&gt; //sometimes there are no erros but blank reply... ANcnt == 0...&lt;br /&gt;<br />
if (ANcnt == 0) // if blackhole list query, means no spammer !!&lt;br /&gt;<br />
//if ((ANcnt == 0) &amp;amp; (IS_BLACKLIST_QUERY == false))&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//error blank reply, return an empty array&lt;br /&gt;<br />
Error = 4;&lt;br /&gt;<br />
ErrorTxt = &amp;quot;Empty string array&amp;quot;;&lt;br /&gt;<br />
Done = true;&lt;br /&gt;<br />
return;&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; //---------------------------------------------&lt;br /&gt;<br />
//Decode received information&lt;br /&gt;<br />
//---------------------------------------------&lt;br /&gt;<br />
string s1;&lt;/p&gt;<br />
&lt;p&gt; // START TEST&lt;/p&gt;<br />
&lt;p&gt; s1 = Encoding.ASCII.GetString(recBuf, 0, recBuf.Length);&lt;/p&gt;<br />
&lt;p&gt; // END TEST&lt;br /&gt;<br />
//if QDcnt &amp;gt; 0 skip it&lt;br /&gt;<br />
if (QDcnt &amp;gt; 0)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//we are not really interested to this string, just parse and skip&lt;br /&gt;<br />
s1 = &amp;quot;&amp;quot;;&lt;br /&gt;<br />
index = parseString(recBuf, index, out s1);&lt;/p&gt;<br />
&lt;p&gt; index += 4; //skip root domain, Qtype and QClass values... unuseful in this contest&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; if (IS_BLACKLIST_QUERY)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
// get the answers, normally one !&lt;br /&gt;<br />
// int the four last bytes there is the ip address&lt;br /&gt;<br />
Error = 0;&lt;br /&gt;<br />
int Last_Position = recBuf.Length - 1;&lt;br /&gt;<br />
result.Add(recBuf[Last_Position - 3].ToString() + &amp;quot;.&amp;quot; + recBuf[Last_Position - 2].ToString() + &amp;quot;.&amp;quot; + recBuf[Last_Position - 1].ToString() + &amp;quot;.&amp;quot; + recBuf[Last_Position].ToString());&lt;br /&gt;<br />
Done = true;&lt;br /&gt;<br />
return;&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; int count = 0;&lt;br /&gt;<br />
//get all answers&lt;br /&gt;<br />
while (count &amp;lt; ANcnt)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
s1 = &amp;quot;&amp;quot;;&lt;br /&gt;<br />
index = parseString(recBuf, index, out s1);&lt;/p&gt;<br />
&lt;p&gt; //Qtype&lt;br /&gt;<br />
int QType = (recBuf[index] * 256) + recBuf[index + 1];&lt;br /&gt;<br />
index += 2;&lt;br /&gt;<br />
s1 += &amp;quot;,&amp;quot; + QType.ToString();&lt;/p&gt;<br />
&lt;p&gt; //QClass&lt;br /&gt;<br />
int QClass = (recBuf[index] * 256) + recBuf[index + 1];&lt;br /&gt;<br />
index += 2;&lt;br /&gt;<br />
s1 += &amp;quot;,&amp;quot; + QClass.ToString();&lt;/p&gt;<br />
&lt;p&gt; //TTL (Time to live)&lt;br /&gt;<br />
int TTL = (recBuf[index] * 16777216) + (recBuf[index + 1] * 65536) + (recBuf[index + 2] * 256) + recBuf[index + 3];&lt;br /&gt;<br />
index += 4;&lt;br /&gt;<br />
s1 += &amp;quot;,&amp;quot; + TTL.ToString();&lt;/p&gt;<br />
&lt;p&gt; int blocklen = (recBuf[index] * 256) + recBuf[index + 1];&lt;br /&gt;<br />
index += 2;&lt;/p&gt;<br />
&lt;p&gt; if (QType == 15)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
int MXprio = (recBuf[index] * 256) + recBuf[index + 1];&lt;br /&gt;<br />
index += 2;&lt;br /&gt;<br />
s1 += &amp;quot;,&amp;quot; + MXprio.ToString();&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; string s2;&lt;br /&gt;<br />
index = parseString(recBuf, index, out s2);&lt;/p&gt;<br />
&lt;p&gt; s1 += &amp;quot;,&amp;quot; + s2;&lt;br /&gt;<br />
result.Add(s1);&lt;/p&gt;<br />
&lt;p&gt; count++;&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; Error = 0;&lt;br /&gt;<br />
Done = true;&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; private int parseString(byte[] buf, int i, out string s)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
int len;&lt;br /&gt;<br />
s = &amp;quot;&amp;quot;;&lt;br /&gt;<br />
bool end = false;&lt;br /&gt;<br />
while (!end)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
if (buf[i] == 192)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//next byte is a pointer to the string, get it..&lt;br /&gt;<br />
i++;&lt;br /&gt;<br />
s += getString(buf, buf[i]);&lt;br /&gt;<br />
i++;&lt;br /&gt;<br />
end = true;&lt;br /&gt;<br />
}&lt;br /&gt;<br />
else&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//next byte is the string length&lt;br /&gt;<br />
len = buf[i];&lt;br /&gt;<br />
i++;&lt;br /&gt;<br />
//get the string&lt;br /&gt;<br />
s += Encoding.ASCII.GetString(buf, i, len);&lt;br /&gt;<br />
i += len;&lt;br /&gt;<br />
//check for the null terminator&lt;br /&gt;<br />
if (buf[i] != 0)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//not null, add a point to the name&lt;br /&gt;<br />
s += &amp;quot;.&amp;quot;;&lt;br /&gt;<br />
}&lt;br /&gt;<br />
else&lt;br /&gt;<br />
{&lt;br /&gt;<br />
//null char..the string is complete, exit&lt;br /&gt;<br />
end = true;&lt;br /&gt;<br />
i++;&lt;br /&gt;<br />
}&lt;br /&gt;<br />
}&lt;br /&gt;<br />
}&lt;br /&gt;<br />
return i;&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt; private string getString(byte[] buf, int i)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
string s = &amp;quot;&amp;quot;;&lt;br /&gt;<br />
int len;&lt;br /&gt;<br />
bool end = false;&lt;/p&gt;<br />
&lt;p&gt; while (!end)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
len = buf[i];&lt;br /&gt;<br />
i++;&lt;br /&gt;<br />
s += Encoding.ASCII.GetString(buf, i, len);&lt;br /&gt;<br />
i += len;&lt;br /&gt;<br />
if (buf[i] == 192)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
i++;&lt;br /&gt;<br />
s += &amp;quot;.&amp;quot; + getString(buf, buf[i]);&lt;br /&gt;<br />
return s;&lt;br /&gt;<br />
}&lt;br /&gt;<br />
if (buf[i] != 0)&lt;br /&gt;<br />
{&lt;br /&gt;<br />
s += &amp;quot;.&amp;quot;;&lt;br /&gt;<br />
}&lt;br /&gt;<br />
else&lt;br /&gt;<br />
{&lt;br /&gt;<br />
end = true;&lt;br /&gt;<br />
}&lt;br /&gt;<br />
}&lt;br /&gt;<br />
return s;&lt;br /&gt;<br />
}&lt;br /&gt;<br />
}&lt;br /&gt;<br />

But… How can I use it ?
Here the example..

&lt;br /&gt;<br />
public string ExecResolution(string Domain, string DnsServer1, string DnsServer2)&lt;br /&gt;<br />
        {&lt;br /&gt;<br />
            // resolv the authoritative domain (type=2)&lt;br /&gt;<br />
            C_DNSquery DnsQry = new C_DNSquery(DnsServer1, Domain, 2);&lt;/p&gt;<br />
&lt;p&gt;            Thread t1 = new Thread(new ThreadStart(DnsQry.doTheJob));&lt;br /&gt;<br />
            t1.Start();&lt;br /&gt;<br />
            int timeout = 20;&lt;br /&gt;<br />
            while ((timeout &amp;gt; 0) &amp;amp; (!DnsQry.Done))&lt;br /&gt;<br />
            {&lt;br /&gt;<br />
                Thread.Sleep(100);&lt;br /&gt;<br />
                timeout--;&lt;br /&gt;<br />
            }&lt;br /&gt;<br />
            if (timeout == 0)&lt;br /&gt;<br />
            {&lt;br /&gt;<br />
                if (DnsQry.udpClient != null)&lt;br /&gt;<br />
                {&lt;br /&gt;<br />
                    DnsQry.udpClient.Close();&lt;br /&gt;<br />
                }&lt;br /&gt;<br />
                t1.Abort();&lt;br /&gt;<br />
                DnsQry.Error = 100;&lt;br /&gt;<br />
            }&lt;/p&gt;<br />
&lt;p&gt;            string[] ns1;&lt;br /&gt;<br />
            string MyMX = &amp;quot;&amp;quot;;&lt;br /&gt;<br />
            if (DnsQry.Error == 0)&lt;br /&gt;<br />
            {&lt;br /&gt;<br />
                ns1 = DnsQry.result[0].Split(',');&lt;br /&gt;<br />
                MyMX = ns1[4];&lt;br /&gt;<br />
                t1.Abort();&lt;br /&gt;<br />
            }&lt;br /&gt;<br />
            else&lt;br /&gt;<br />
            {&lt;br /&gt;<br />
                t1.Abort();&lt;br /&gt;<br />
                MyMX = DnsServer1;&lt;br /&gt;<br />
            }&lt;/p&gt;<br />
&lt;p&gt;            // Resolve MX (type = 15)&lt;br /&gt;<br />
            DnsQry = new C_DNSquery(MyMX, Domain, 15);&lt;br /&gt;<br />
            Thread t2 = new Thread(new ThreadStart(DnsQry.doTheJob));&lt;br /&gt;<br />
            t2.Start();&lt;br /&gt;<br />
            timeout = 20;&lt;br /&gt;<br />
            string TTL = &amp;quot;&amp;quot;;&lt;br /&gt;<br />
            string MXName = &amp;quot;&amp;quot;;&lt;br /&gt;<br />
            while ((timeout &amp;gt; 0) &amp;amp; (!DnsQry.Done))&lt;br /&gt;<br />
            {&lt;br /&gt;<br />
                Thread.Sleep(100);&lt;br /&gt;<br />
                timeout--;&lt;br /&gt;<br />
            }&lt;br /&gt;<br />
            if (timeout == 0)&lt;br /&gt;<br />
            {&lt;br /&gt;<br />
                if (DnsQry.udpClient != null)&lt;br /&gt;<br />
                {&lt;br /&gt;<br />
                    DnsQry.udpClient.Close();&lt;br /&gt;<br />
                }&lt;br /&gt;<br />
                t2.Abort();&lt;br /&gt;<br />
                DnsQry.Error = 100;&lt;br /&gt;<br />
            }&lt;/p&gt;<br />
&lt;p&gt;            if (DnsQry.Error == 0)&lt;br /&gt;<br />
            {&lt;br /&gt;<br />
                if (DnsQry.result.Count == 1)&lt;br /&gt;<br />
                {&lt;br /&gt;<br />
                    string[] ns2 = DnsQry.result[0].Split(',');&lt;br /&gt;<br />
                    MXName = ns2[5];&lt;br /&gt;<br />
                    TTL = ns2[3];&lt;br /&gt;<br />
                }&lt;br /&gt;<br />
                else&lt;br /&gt;<br />
                {&lt;br /&gt;<br />
                    Int32 preference = 9910000;&lt;br /&gt;<br />
                    for (int indns = 0; indns &amp;lt;= DnsQry.result.Count - 1; indns++)&lt;br /&gt;<br />
                    {&lt;br /&gt;<br />
                        string[] ns2 = DnsQry.result[indns].Split(',');&lt;br /&gt;<br />
                        if (Int32.Parse(ns2[4]) &amp;lt; preference)&lt;br /&gt;<br />
                        {&lt;br /&gt;<br />
                            MXName = ns2[5];&lt;br /&gt;<br />
                            TTL = ns2[3];&lt;br /&gt;<br />
                            preference = Int32.Parse(ns2[4]);&lt;br /&gt;<br />
                        }&lt;br /&gt;<br />
                    }&lt;br /&gt;<br />
                }&lt;br /&gt;<br />
            }&lt;/p&gt;<br />
&lt;p&gt;            DnsQry = null;&lt;br /&gt;<br />
            t2.Abort();&lt;/p&gt;<br />
&lt;p&gt;            // try with alternative DNS Server&lt;br /&gt;<br />
            if (MXName == &amp;quot;&amp;quot;)&lt;br /&gt;<br />
            {&lt;br /&gt;<br />
                DnsQry = new C_DNSquery(DnsServer2, Domain, 15);&lt;br /&gt;<br />
                Thread t3 = new Thread(new ThreadStart(DnsQry.doTheJob));&lt;br /&gt;<br />
                t3.Start();&lt;br /&gt;<br />
                timeout = 20;&lt;br /&gt;<br />
                TTL = &amp;quot;&amp;quot;;&lt;br /&gt;<br />
                MXName = &amp;quot;&amp;quot;;&lt;br /&gt;<br />
                while ((timeout &amp;gt; 0) &amp;amp; (!DnsQry.Done))&lt;br /&gt;<br />
                {&lt;br /&gt;<br />
                    Thread.Sleep(100);&lt;br /&gt;<br />
                    timeout--;&lt;br /&gt;<br />
                }&lt;br /&gt;<br />
                if (timeout == 0)&lt;br /&gt;<br />
                {&lt;br /&gt;<br />
                    if (DnsQry.udpClient != null)&lt;br /&gt;<br />
                    {&lt;br /&gt;<br />
                        DnsQry.udpClient.Close();&lt;br /&gt;<br />
                    }&lt;br /&gt;<br />
                    t3.Abort();&lt;br /&gt;<br />
                    DnsQry.Error = 100;&lt;br /&gt;<br />
                }&lt;/p&gt;<br />
&lt;p&gt;                if (DnsQry.Error == 0)&lt;br /&gt;<br />
                {&lt;br /&gt;<br />
                    if (DnsQry.result.Count == 1)&lt;br /&gt;<br />
                    {&lt;br /&gt;<br />
                        string[] ns2 = DnsQry.result[0].Split(',');&lt;br /&gt;<br />
                        MXName = ns2[5];&lt;br /&gt;<br />
                        TTL = ns2[3];&lt;br /&gt;<br />
                    }&lt;br /&gt;<br />
                    else&lt;br /&gt;<br />
                    {&lt;br /&gt;<br />
                        Int32 preference = 9910000;&lt;br /&gt;<br />
                        for (int indns = 0; indns &amp;lt;= DnsQry.result.Count - 1; indns++)&lt;br /&gt;<br />
                        {&lt;br /&gt;<br />
                            string[] ns2 = DnsQry.result[indns].Split(',');&lt;br /&gt;<br />
                            if (Int32.Parse(ns2[4]) &amp;lt; preference)&lt;br /&gt;<br />
                            {&lt;br /&gt;<br />
                                MXName = ns2[5];&lt;br /&gt;<br />
                                TTL = ns2[3];&lt;br /&gt;<br />
                                preference = Int32.Parse(ns2[4]);&lt;br /&gt;<br />
                            }&lt;br /&gt;<br />
                        }&lt;br /&gt;<br />
                    }&lt;br /&gt;<br />
                }&lt;/p&gt;<br />
&lt;p&gt;                DnsQry = null;&lt;br /&gt;<br />
                t3.Abort();&lt;br /&gt;<br />
            }&lt;/p&gt;<br />
&lt;p&gt;            return MXName;&lt;/p&gt;<br />
&lt;p&gt;}&lt;br /&gt;<br />

This code was extracted from M10Mail Server, my never ending project.
Dante Marchesi with the fundamental contribution of Giovanni Magugliani.

March 15, 2010

Upload email message to DOMINO using IMAP4 protocol and C#

Filed under: c#, Email protocols, NET Framework — Tags: , , — Dante @ 10:06 am

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:

  1. login ino the IMAP4 server providing the username and password.
  2. append the email message into the folder using “APPEND” commad
  3. 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

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.