I am looking for an article that explains clearly, how to implement a smtp client ( my requirement is not to use the inbuild .net framework smtpClient). I followed this post here http://www.developerfusion.com/article/4039/custom-smtp-in-c/2/
but I keep getting an exception when I ran the program, I also do not understand why the fields used in that article do not include the username and password to authenticate the sender against the mail server.
ill appreciate some clear and working example, thanks
What errors are you getting and can you share the code you have implemented so far? It's difficult to provide help without seeing the code and the specific error message.
The line that throws the exception is in bold and underlined below, in the smtp class
and the exception message is this, as you see it does not give much detail..
//An unhandled exception of type 'TcpService.SmtpProtocol.SmtpClientException'occurred in TcpService.dll
public class Smtp : System.Net.Sockets.TcpClient
{
public string from = null;
public ArrayList to;
public ArrayList cc;
public ArrayList bcc;
public string subject = null;
public string bodyText = null;
public string bodyHtml = null;
public string server = null;
public Smtp()
{
to = new ArrayList();
cc = new ArrayList();
bcc = new ArrayList();
}
public void Send()
{
string message;
string response;
Connect(server, 25);
response = Response();
if (response.Substring(0, 3) != "220")
{
throw new SmtpClientException(response);
};
message = "HELO me\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "250")
{
throw new SmtpClientException(response);
}
message = "MAIL FROM:<" + from + ">\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "250")
{ throw new SmtpClientException(response);
}
message = "QUIT\r\n";
Write(message);
response = Response();
if (response.IndexOf(" 221") == -1)
{
throw new SmtpClientException(response);
}
}
// The Write method writes data to the socket.
public void Write(string message)
{
System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding();
byte[] WriteBuffer = new byte[1024];
WriteBuffer = en.GetBytes(message);
NetworkStream stream = GetStream();
stream.Write(WriteBuffer, 0, WriteBuffer.Length);
}
// C#'s native string type is not based on the ASCII characters, but rather is based on Unicode.
//Thus in order to send messages on SMTP we must first convert the string input parameter to an array of ASCII bytes.
//We use the ASCIIEncoding class in dotNET to make this transformation. Then we retrieve the socket stream
//using the GetStream method inherited from the TcpClient class and write the bytes to the stream.
// The Response method receives data from the socket.
// The Response method begins by retrieving the bytes from the stream and then converts the incoming ASCII bytes to our native C# string type
public string Response()
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] serverbuff = new Byte[1024];
NetworkStream stream = GetStream();
int count = stream.Read(serverbuff, 0, 1024);
if (count == 0)
return "";
return enc.GetString(serverbuff, 0, count);
}
}
public class SmtpClientException: System.Exception
{
private string message;
public SmtpClientException(string str)
{
message = str;
}
public string What()
{
return message;
}
}
And why is your requirement to not use the .NET SMTP class? It's solid, and well debugged. It's silly to write your own because you will almost certainly have bugs, and those bugs can lead to vulnerabilities in your application. Unless your intention
is to make an SMTP library that does something no other one does, using existing code is far more wise.
ysfkel
Member
137 Points
167 Posts
implement custom smtp client c#?
Feb 04, 2013 05:20 PM|LINK
Hello,
I am looking for an article that explains clearly, how to implement a smtp client ( my requirement is not to use the inbuild .net framework smtpClient). I followed this post here http://www.developerfusion.com/article/4039/custom-smtp-in-c/2/
but I keep getting an exception when I ran the program, I also do not understand why the fields used in that article do not include the username and password to authenticate the sender against the mail server.
ill appreciate some clear and working example, thanks
CodeHobo
All-Star
18647 Points
2647 Posts
Re: implement custom smtp client c#?
Feb 04, 2013 05:44 PM|LINK
What errors are you getting and can you share the code you have implemented so far? It's difficult to provide help without seeing the code and the specific error message.
Blog | Twitter : @Hattan
ysfkel
Member
137 Points
167 Posts
Re: implement custom smtp client c#?
Feb 04, 2013 05:56 PM|LINK
The line that throws the exception is in bold and underlined below, in the smtp class
and the exception message is this, as you see it does not give much detail..
//An unhandled exception of type 'TcpService.SmtpProtocol.SmtpClientException'occurred in TcpService.dll
public class Smtp : System.Net.Sockets.TcpClient
{
public string from = null;
public ArrayList to;
public ArrayList cc;
public ArrayList bcc;
public string subject = null;
public string bodyText = null;
public string bodyHtml = null;
public string server = null;
public Smtp()
{
to = new ArrayList();
cc = new ArrayList();
bcc = new ArrayList();
}
public void Send()
{
string message;
string response;
Connect(server, 25);
response = Response();
if (response.Substring(0, 3) != "220")
{
throw new SmtpClientException(response);
};
message = "HELO me\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "250")
{
throw new SmtpClientException(response);
}
message = "MAIL FROM:<" + from + ">\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "250")
{
throw new SmtpClientException(response);
}
foreach (string address in to)
{
try
{
message = "RCPT TO:<" + address + ">\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "250")
{
throw new SmtpClientException(response);
}
}
catch (SmtpClientException e)
{
System.Console.WriteLine("{ 0}", e.What());
}
}
foreach (string address in cc)
{
try
{
message = "RCPT TO:<" + address + ">\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "250")
{
throw new SmtpClientException(response);
}
}
catch (SmtpClientException e)
{
System.Console.WriteLine("{ 0}", e.What());
}
}
foreach (string address in bcc)
{
try
{
message = "RCPT TO:<" + address + ">\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "250")
{
throw new SmtpClientException(response);
}
}
catch (SmtpClientException e)
{
System.Console.WriteLine("{ 0}", e.What());
}
}
message = "DATA\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "354")
{
throw new SmtpClientException(response);
}
message = "Subject: " + subject + "\r\n";
foreach (string address in to)
{
message += "To: " + address + "\r\n";
}
foreach (string address in cc)
{
message += "Cc: " + address + "\r\n";
}
message += "From: " + from + "\r\n";
if (bodyHtml.Length > 0)
{
message += "MIME-Version: 1.0\r\n"
+ " Content-Type: text/ html;\r\n"
+ " charset=\" iso-8859-1\"\r\n";
message += "\r\n" + bodyHtml;
}
else
{
message += "\r\n" + bodyText;
};
message += "\r\n.\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "250")
{
throw new SmtpClientException(response);
}
message = "QUIT\r\n";
Write(message);
response = Response();
if (response.IndexOf(" 221") == -1)
{
throw new SmtpClientException(response);
}
}
// The Write method writes data to the socket.
public void Write(string message)
{
System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding();
byte[] WriteBuffer = new byte[1024];
WriteBuffer = en.GetBytes(message);
NetworkStream stream = GetStream();
stream.Write(WriteBuffer, 0, WriteBuffer.Length);
}
// C#'s native string type is not based on the ASCII characters, but rather is based on Unicode.
//Thus in order to send messages on SMTP we must first convert the string input parameter to an array of ASCII bytes.
//We use the ASCIIEncoding class in dotNET to make this transformation. Then we retrieve the socket stream
//using the GetStream method inherited from the TcpClient class and write the bytes to the stream.
// The Response method receives data from the socket.
// The Response method begins by retrieving the bytes from the stream and then converts the incoming ASCII bytes to our native C# string type
public string Response()
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] serverbuff = new Byte[1024];
NetworkStream stream = GetStream();
int count = stream.Read(serverbuff, 0, 1024);
if (count == 0)
return "";
return enc.GetString(serverbuff, 0, count);
}
}
public class SmtpClientException: System.Exception
{
private string message;
public SmtpClientException(string str)
{
message = str;
}
public string What()
{
return message;
}
}
test program
static void Main(string[] args)
{
try
{
Smtp smtp = new Smtp();
smtp.server = "smtp.gmail.com";
smtp.from = "ykel@gmail.com (Ykel)";
smtp.subject = "Hello World";
smtp.bodyHtml = "<HTML><BODY>Hello World</BODY></HTML>";
smtp.to.Add("ykel@yahoo.com");
smtp.Send();
}
catch (SmtpException e)
{
System.Console.WriteLine("{ 0}", e.Message);
}
Erik.SM
Member
332 Points
61 Posts
Re: implement custom smtp client c#?
Feb 04, 2013 06:29 PM|LINK
What does this have to do with ASP.NET MVC?
And why is your requirement to not use the .NET SMTP class? It's solid, and well debugged. It's silly to write your own because you will almost certainly have bugs, and those bugs can lead to vulnerabilities in your application. Unless your intention is to make an SMTP library that does something no other one does, using existing code is far more wise.
Or maybe this is homework?
ysfkel
Member
137 Points
167 Posts
Re: implement custom smtp client c#?
Feb 04, 2013 06:50 PM|LINK
Its a task and am required to do it this way...
Erik.SM
Member
332 Points
61 Posts
Re: implement custom smtp client c#?
Feb 04, 2013 07:24 PM|LINK
I ask again, what does this have to do with ASP.NET MVC?