By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

How to send an e-mail

P: n/a
I have a service that needs to send e-mail alerts. I have been attempting
to use the System.Net.Mail function from .NET but this seems to require the
IIS be installed and running. Since some of my customers are not really
happy with having the IIS installed, not being used except to send e-mail
this is becoming a problem.

I have also tried using a little program from code project for sending
e-mail which works great on older servers but does not work on newer servers
because security which I don't have time to resolve.

Is there a better/easier way of doing this?

Regards,
John
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Here is little smtp client I did in C# (no cdo or IIS required).
http://spaces.msn.com/members/staceyw/Blog/cns!1pnsZpX0fPvDxLKC6rAAhLsQ!361.entry

--
William Stacey [MVP]

"John J. Hughes II" <no@invalid.com> wrote in message
news:Ob**************@TK2MSFTNGP15.phx.gbl...
I have a service that needs to send e-mail alerts. I have been attempting
to use the System.Net.Mail function from .NET but this seems to require the
IIS be installed and running. Since some of my customers are not really
happy with having the IIS installed, not being used except to send e-mail
this is becoming a problem.

I have also tried using a little program from code project for sending
e-mail which works great on older servers but does not work on newer
servers because security which I don't have time to resolve.

Is there a better/easier way of doing this?

Regards,
John

Nov 17 '05 #2

P: n/a
William,

Looks sort of like the one I had the security issue with but I will try it,
thanks.

Regards,
John
Nov 17 '05 #3

P: n/a
"John J. Hughes II" <no@invalid.com> wrote in
news:Ob**************@TK2MSFTNGP15.phx.gbl:
I have a service that needs to send e-mail alerts. I have been
attempting to use the System.Net.Mail function from .NET but this
seems to require the IIS be installed and running. Since some of my


Yes, it has installation requirements.

See here for an alternate solution:
http://www.codeproject.com/csharp/IndySMTP.asp
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
Nov 17 '05 #4

P: n/a
William,

It works with my local ISP so I am forward it to the problem customers :),
see how far that gets me. I am no longer with the ISP my last attempt at
this failed with so...

I had to change the following.

IPAddress[] ips = Dns.GetHostAddresses(SmtpServer);

TO THIS:

IPHostEntry iphe = Dns.GetHostByName(SmtpServer);
IPAddress[] ips = iphe.AddressList;
AND I removed this, it was giving me a 500 error.

//CheckReply(sr, SMTPResponse.QUIT_SUCCESS);

Regards,
John
Nov 17 '05 #5

P: n/a
Here is a mail class I used before there was a mailer namespace. It works
with any smtp server. I know it makes the calls manually but I used it in a
mass email app with over 60,000 recipients to tell players at sorren.com
announcements. It is solid code.

using System;

using System.IO;

using System.Net;

using System.Net.Sockets;

using System.Text;

using System.Diagnostics;

namespace Emailer

{

/// <summary>

/// Summary description for Class1.

/// </summary>

public class Mailer

{

private string strEmailServer;

private string strSendTo;

private string strMailFrom;

private string strSubject;

private string strBody;

public static TcpClient tcpc = new TcpClient(); // public static so is
thread safe

public static NetworkStream nwstream; // public static so is thread safe

public Mailer(string SMTPServer, string Sender)

{

this.SMTPServer = SMTPServer;

this.Sender = Sender;

}

public string SMTPServer

{

get

{

return strEmailServer;

}

set

{

strEmailServer = value;

}

}

public string Recipient

{

get

{

return strSendTo;

}

set

{

strSendTo = value;

}

}

public string Sender

{

get

{

return strMailFrom;

}

set

{

strMailFrom = value;

}

}

public string Subject

{

get

{

return strSubject;

}

set

{

strSubject = value;

}

}

public string Body

{

get

{

return strBody;

}

set

{

strBody = value;

}

}

public string Send(string Recipient, string Subject, string Body)

{

this.Subject=Subject;

return Send(Recipient, Body);

}

public string Send(string Recipient, string Body)

{

this.Recipient=Recipient;

this.Body=Body;

return Send();

}

public string Send()

{

string strResponse;

StringBuilder smtpLog = new StringBuilder();

Debug.WriteLine("Mailer.Send() : Requesting Lock on 'tcpc'...");

lock(tcpc)

{

Debug.WriteLine("Mailer.Send() : Lock on 'tcpc' is approved.");

Debug.WriteLine("Mailer.Send() : Creating TcpClient");

// TcpClient tcpc = new TcpClient(); made public static so is thread safe

tcpc = new TcpClient();

try

{

tcpc.Connect(strEmailServer, 25);

}

catch (SocketException socketEx)

{

Debug.WriteLine("Mailer.Send() : Connection Error: " + socketEx.ToString());

return "Connection Error: " + socketEx.ToString();

}

// string strResponse;

// string strFullResponse=""; // needed to be defined outside "lock"-ed code

Debug.WriteLine("Mailer.Send() : Getting NetworkStream");

// made public static so is thread safe

// NetworkStream nwstream = tcpc.GetStream();

nwstream = tcpc.GetStream();

Debug.WriteLine("Mailer.Send() : Writing to Stream");

try

{ string smtp;

ReadFromStream(ref nwstream, out strResponse);

smtpLog.Append(strResponse);

smtp = "HELO myhost";

WriteToStream(ref nwstream, smtp);

smtpLog.Append(smtp + Environment.NewLine);

ReadFromStream(ref nwstream, out strResponse);

smtpLog.Append(strResponse);

smtp = "MAIL FROM: " + strMailFrom;

WriteToStream(ref nwstream, smtp);

smtpLog.Append(smtp + Environment.NewLine);

ReadFromStream(ref nwstream, out strResponse);

smtpLog.Append(strResponse);

smtp = "RCPT TO: " + strSendTo;

WriteToStream(ref nwstream, smtp);

smtpLog.Append(smtp + Environment.NewLine);

ReadFromStream(ref nwstream, out strResponse);

smtpLog.Append(strResponse);

smtp = "DATA";

WriteToStream(ref nwstream, smtp);

smtpLog.Append(smtp + Environment.NewLine);

ReadFromStream(ref nwstream, out strResponse);

smtpLog.Append(strResponse);

smtp = "From: " + strMailFrom;

WriteToStream(ref nwstream, smtp);

smtpLog.Append(smtp + Environment.NewLine);

smtp = "Subject: " + strSubject;

WriteToStream(ref nwstream, smtp);

smtpLog.Append(smtp + Environment.NewLine);

smtp = "To: " + strSendTo;

WriteToStream(ref nwstream, smtp);

smtpLog.Append(smtp + Environment.NewLine);

smtp = "";

WriteToStream(ref nwstream, "");

smtpLog.Append(smtp + Environment.NewLine);

smtp = strBody;

WriteToStream(ref nwstream, smtp);

smtpLog.Append(smtp + Environment.NewLine);

smtp = "\r\n.";

WriteToStream(ref nwstream, smtp);

smtpLog.Append(smtp + Environment.NewLine);

ReadFromStream(ref nwstream, out strResponse);

smtpLog.Append(strResponse);

}

catch

{

Debug.WriteLine("Mailer.Send() : Problem Reading/writing");

}

Debug.WriteLine("Mailer.Send() : Closing NetworkStream");

nwstream.Close();

Debug.WriteLine("Mailer.Send() : Closing TcpClient");

tcpc.Close();

Debug.WriteLine("Mailer.Send() : Releasing Lock on 'tcpc'...");

} // lock(tcpc) {

Debug.WriteLine("Mailer.Send() : Lock on 'tcpc' is released.");

Debug.WriteLine("Mailer.Send() : Returning Full Response");

return smtpLog.ToString();

}

private bool WriteToStream(ref NetworkStream nwstream, string strLine)

{

string strString2Send = strLine + "\r\n";

Byte[] arr2Send = Encoding.ASCII.GetBytes(strString2Send.ToCharArray ());

try

{

nwstream.Write(arr2Send, 0, arr2Send.Length);

}

catch

{

return false;

}

return true;

}

private bool ReadFromStream(ref NetworkStream nwstream, out string
strMessage)

{

byte[] readBuffer = new byte[4096];

int nLength = nwstream.Read(readBuffer, 0, readBuffer.Length);

strMessage = Encoding.ASCII.GetString(readBuffer, 0, nLength);

return (3 <= readBuffer[0]); // 2 success, 3 informational

}

}

}
"John J. Hughes II" <no@invalid.com> wrote in message
news:Ob**************@TK2MSFTNGP15.phx.gbl...
I have a service that needs to send e-mail alerts. I have been attempting
to use the System.Net.Mail function from .NET but this seems to require the
IIS be installed and running. Since some of my customers are not really
happy with having the IIS installed, not being used except to send e-mail
this is becoming a problem.

I have also tried using a little program from code project for sending
e-mail which works great on older servers but does not work on newer
servers because security which I don't have time to resolve.

Is there a better/easier way of doing this?

Regards,
John

Nov 17 '05 #6

P: n/a
Some of that was fx2.0 specific stuff - sorry did not mention that.

--
William Stacey [MVP]

"John J. Hughes II" <no@invalid.com> wrote in message
news:u0**************@TK2MSFTNGP14.phx.gbl...
William,

It works with my local ISP so I am forward it to the problem customers :),
see how far that gets me. I am no longer with the ISP my last attempt at
this failed with so...

I had to change the following.

IPAddress[] ips = Dns.GetHostAddresses(SmtpServer);

TO THIS:

IPHostEntry iphe = Dns.GetHostByName(SmtpServer);
IPAddress[] ips = iphe.AddressList;
AND I removed this, it was giving me a 500 error.

//CheckReply(sr, SMTPResponse.QUIT_SUCCESS);

Regards,
John

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.