using System;
using hp;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Collections;
namespace HPsmtp
{
public class smtp
{
ManualResetEvent wait = new ManualResetEvent(false);
public string to;
public string from;
public string body;
public string subj;
public bool servermode;
public string srv;
public string name;
public string lastserver;
public int tmeout=0;
public int amount=0;
public hpsocket sck = new hpsocket();
public string MXLookup(string dmn)
{
string nl = "\r\n";
if (dmn.IndexOf("@") > -1)
{
dmn = dmn.Substring(dmn.IndexOf("@")+1,(dmn.Length-dmn.IndexOf("@")-1));
}
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "nslookup.exe";
psi.Arguments = "-type=mx "+dmn;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
StreamReader stmrdr = p.StandardOutput;
string s = stmrdr.ReadToEnd();
s = s.Substring(s.IndexOf("anger")+8,s.Length-(s.IndexOf("anger")+8));
s = s.Substring(0,s.IndexOf(nl));
return s;
}
public bool conn()
{
int ret;
int done=0;
string rec="";
string[] receitps;
char[] sep;
ret=sck.connect(srv,"25");
if (ret == 1)
{
return true;
}
else
{
return false;
}
}
public bool close()
{
int ret;
ret=sck.write("quit\r\n");
ret=sck.close();
return true;
}
public bool Mail()
{
int ret;
int done=0;
string rec="";
string[] receitps;
char[] sep;
rec="";
tmeout=0;
if (to.IndexOf(";") < 1)
{
amount=1;
}
else
{
// to do for multiple email address's
//receitps = (to.Split(sep,50));
}
if(servermode == true)
{
srv = MXLookup(to.Substring(to.IndexOf("@"),to.Length-to.IndexOf("@")));
}
ret=sck.write("ehlo "+srv+"\r\n");
while(true)
{
rec=sck.readbuff();
if (rec.Length > 0)
break;
wait.WaitOne(1000,false);
tmeout++;
if (tmeout > 5)
{
return false;
}
}
if (rec.Substring(0,1) == "2")
{
rec="";
tmeout=0;
ret=sck.write("mail from: <"+from+">\r\n");
while(true)
{
rec=sck.readbuff();
if (rec.Length > 0)
break;
tmeout++;
if (tmeout > 5)
{
return false;
}
wait.WaitOne(1000,false);
}
while(done < amount)
{
if (rec.Substring(0,1) == "2")
{
rec="";
tmeout=0;
ret=sck.write("rcpt to: <"+to+">\r\n");
while(true)
{
rec=sck.readbuff();
if (rec.Length > 0)
break;
wait.WaitOne(1000,false);
tmeout++;
if (tmeout > 5)
{
return false;
}
}
}
done++;
}
if (rec.Substring(0,1) == "2")
{
rec="";
tmeout=0;
ret=sck.write("data\r\n");
while(true)
{
rec=sck.readbuff();
if (rec.Length > 0)
break;
wait.WaitOne(1000,false);
tmeout++;
if (tmeout > 5)
{
return false;
}
}
if (rec.Substring(0,1) == "3")
{
rec="";
tmeout=0;
ret=sck.write("From: "+name+" <"+from+">\r\nSubject:
"+subj+"\r\n\r\n"+body+"\r\n.\r\n");
while(true)
{
rec=sck.readbuff();
if (rec.Length > 0)
break;
wait.WaitOne(1000,false);
tmeout++;
if (tmeout > 5)
{
return false;
}
}
if (rec.Substring(0,1) != "2")
{
amount=-1;
return false;
}
}
else
{
amount=-1;
return false;
}
}
else
{
amount=-1;
return false;
}
}
else
{
amount=-1;
return false;
}
return true;
}
}
}
"Eric" wrote:
When I use system.web.mail namespace in most of my integration programs
and in windows application programs it doesn't work.
it normally gives an exception "Could not access 'CDO.Message' object."
or "Could not access MAPI drivers". (all these machines, outlook is not
installed and local SMTP is installed and delivery configured to relay
to the mail server. all my win32 services runs under administrator.) I
was under the impression that this namespace is a wrapper to the CDONTS
object library. Apparently it is not, it needs MAPI drives to be
installed.
Only method i know to install MAPI drivers is to install MS Outlook on
the machine and create a mail profile. Please let me know if there is
another easy method?
In utter desperation I have written a wrapper to the CDONTS object
library my self and use it at the moment or using Indy SMTP socket
client (http://www.indyproject.org/). All these machines works fine
with both methods.
Is this a common problem?
Is there an easy work around for this? I like to use framework classes
so I don't need to ship these extra dlls.
Or am I doing something stupid?