Re: How to send an e-mail



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@xxxxxxxxxxx> wrote in message
news:ObmuwU0cFHA.3184@xxxxxxxxxxxxxxxxxxxxxxx
>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
>


.



Relevant Pages