WRBehning

I have a requirement for an email program to send SMTP mail without having IIS (and therefore no SMTP service) installed on work stations. I found a nice solution to this at http://www.eggheadcafe.com/articles/20030316.asp. I modified the code to fit my needs and it works. However I have a couple of issues.

One: How do I cause the SMTP server to send out delivery notifications on failure and success This is pretty easy using the DeliveryNotificationOptions of the System.Net.Mail.MailMessage class. But of course I am not using this.

Two: How do I send authentication or system credentials to the SMTP server Again, this is pretty simple using the System.Net.Mail.Message class.

Example code:

IPHostEntry IPhst = Dns.GetHostEntry(SmtpServer);

IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);

Socket s = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

s.Connect(endPt);

Senddata(s, string.Format("EHLO {0}\r\n", Dns.GetHostName()));

Senddata(s, string.Format("MAIL From: {0}\r\n", message.From));

Senddata(s, string.Format("RCPT TO: {0}\r\n", strTo));

StringBuilder Header = new StringBuilder();

Header.AppendLine("From: " + message.From.ToString());

Header.Append("To: "+strTo);

string MsgBody = message.Body;

Header.Append("Date: ");

Header.AppendLine(DateTime.Now.ToString("ddd, d M y H:m:s z"));

Header.AppendLine("Subject: " + message.Subject);

Header.AppendLine("X-Mailer: SMTPDirect v1");

Header.AppendLine("MIME-Version: 1.0");

Header.AppendLine("Content-Type: multipart/mixed; boundary=unique-boundary-1");

Header.AppendLine();

Header.AppendLine("--unique-boundary-1");

Header.AppendLine("Content-Type: text/html");

Header.AppendLine("Content-Transfer-Encoding: 7Bit");

Header.AppendLine();

Header.AppendLine(MsgBody);

Header.AppendLine(".");

Header.AppendLine();

Header.AppendLine();

Senddata(s, ("DATA\r\n"));

Senddata(s, Header.ToString());

Senddata(s, "QUIT\r\n");

s.Close();

...



Re: .NET Framework Networking and Communication SMTP Mail without SMTP Service

Dreedle

You don't need IIS or anytyng else to use SmtpClient class. It's an SMTP client. It wraps up the client side of the SMTP protocol. Same as the code you found. IIS has nothing to do with it.




Re: .NET Framework Networking and Communication SMTP Mail without SMTP Service

WRBehning

Then I guess I'm confused. The following code fails unless the SMTP service is running.

The mailClient.Send(mailMessage) times out and raises a SMTPException.

MailMessage mailMessage = GetMailMessage();

SmtpClient mailClient = new SmtpClient("GO-Exchange.GO_NT_SERVER.local");

mailClient.UseDefaultCredentials = false;

mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

mailClient.Credentials = new System.Net.NetworkCredential(strLoginName, strPassword);

mailClient.SendCompleted += new SendCompletedEventHandler(mailClient_SendCompleted);

mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure;

mailClient.Send(mailMessage);

onMailSent(mailMessage);





Re: .NET Framework Networking and Communication SMTP Mail without SMTP Service

WRBehning

Actually let me be a little more clear. The issue appeared on a Win2K environment. The application was working just fine, until the administrator uninstalled IIS, then we started experiencing the timeout exception. On my development machine XP Pro, I shutdown the SMTP service, and it does still work as you described. So I need to figure out what else could be causing the issue. Any ideas would be helpful. Thanks...



Re: .NET Framework Networking and Communication SMTP Mail without SMTP Service

RizwanSharp

"Then I guess I'm confused. The following code fails unless the SMTP service is running. "

Can you provide complete exception message that you are getting.

Best Regards,

Rizwan aka RizwanSharp






Re: .NET Framework Networking and Communication SMTP Mail without SMTP Service

Dreedle

Check for networking problems first. Make sure your Win2k box where the issue occurs can see the SMTP server. Ping the server from the Win2k box.

Basically the SmtpClient constructor wants a resolvable network address. Your development box can see the server (because it works) and the Win2k box probably can't (hence the timeout) presumably both using the same server address in the SmtpClient constructor.

As you've noticed the SMTP service on the machines from which you are SENDING email has nothing to do with it. The SMTP service is an SMTP server; ie it will accept/forward email sent to it.






Re: .NET Framework Networking and Communication SMTP Mail without SMTP Service

WRBehning

Sure:

Message: "The operation has timed out."

StackTrace:

at System.Net.Mail.SmtpClient.Send(MailMessage message)

at LymanControls.LymanMail.SendSmtp() in C:\Documents and Settings\BBehning\My Documents\Visual Studio 2005\Projects\LymanControls\LymanControls\LymanMail.cs:line 192





Re: .NET Framework Networking and Communication SMTP Mail without SMTP Service

WRBehning

Yes, you are correct. I was able to install the app on a clean Win2k machine, without IIS or the SMTP service installed, and it works just fine. So I guess after chasing my tail a bit the issue has to do with a particular machine, not the application.

Thanks for the responses.





Re: .NET Framework Networking and Communication SMTP Mail without SMTP Service

Dreedle

Actually it may rely on the local SMTP service if you are telling SmtpClient to connect to it. If that is the case the local SMTP service would have to be configured to forward the mail on the the 'real' SMTP server where you want the mail to go.

That would work but not the best way to do it. It's best to configure your client applications to go to your SMTP server directly by giving SmtpClient a resolvable server name/address in the constructor taken from app.config and NOT hardcoded into your app.