How to send an e-mail

  • Thread starter Thread starter John J. Hughes II
  • Start date Start date
J

John J. Hughes II

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
 
William,

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

Regards,
John
 
John J. Hughes II said:
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/
 
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
 
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

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top