Library tutorials & articles
Custom SMTP in C#
- Introduction
- Getting Started
- Sending the mail
- The Other Methods
- Demonstration
Sending the mail
The Send method does all the work in our class. Once you've set the subject, body, from and recipient data members, you simply call the Send method and the SMTP communication is initiated. I won't explain the details of the SMTP protocol here, but rather encourage the user to step through the code, read the SMTP RFC and read my previous articles on SMTP.
public void Send()
{
string message;
string response;
Connect( server, 25);
response = Response();
if (response.Substring( 0, 3) != "220")
{
throw new SmtpException( response);
};
message = "HELO me\r\n";
Write( message);
response = Response();
if (response.Substring( 0, 3) != "250")
{
throw new SmtpException( response);
}
message = "MAIL FROM:<" + from + ">\r\n";
Write( message);
response = Response();
if (response.Substring( 0, 3) != "250")
{
throw new SmtpException( 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 SmtpException(
response);
}
}
catch( SmtpException 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 SmtpException(
response);
}
}
catch( SmtpException 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 SmtpException(
response);
}
}
catch( SmtpException e)
{
System.Console.WriteLine("{ 0}",
e.What());
}
}
message = "DATA\r\n";
Write( message);
response = Response();
if (response.Substring( 0, 3) != "354")
{
throw new SmtpException( 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 SmtpException( response);
}
message = "QUIT\r\n";
Write( message);
response = Response();
if (response.IndexOf(" 221") == -1)
{
throw new SmtpException( response);
}
}
The Send method uses three methods, the Connect, Write and Response methods.
The Connect method is inherited from the TcpClient class
and establishes a TCP connection between our client and a TCP server.
Related articles
Related discussion
-
How to POP3 in C#
by converter2009 (9 replies)
-
google apps email using php
by lghtyr (0 replies)
Related podcasts
-
Moving your Email into the Cloud - Google for Apps and Live Custom Domains
Scott and Carl talk about Scott's Family's recent move to Google Apps and Carl considers moving to Live Custom Domains. What are the benefits of moving your life into the cloud?
http://www.lesnikowski.com/mail
Try using this library:
http://www.lesnikowski.com/mail
It's simple and effective.
Allows sending, receiving and parsing email messages.
Encodes/decodes any kind of attachment.
Includes POP3 and STMP clients.
Written entirely in managed code in C#.
CC (and BCC, but the B stands for "Blind" so it's not marked as a copy) are traditionally used to send
it to others that need to see the letter as well without making any changes to it.
With SMTP, I think you just use another RCPT TO and perhaps add a CC: blah
and in the data section of the email...
So to RECEIVE SMTP or INTERCEPT SMTP packets, does one need to write a local mail server?
Looking at the code, I don't see any differentiation between To, CC, and BCC when you are sending them to the Mail server. For that matter, RFC821 doesn't seem to be much help on the matter either.
How should one identify the three types when doing an SMTP send?
In this example, SMTP class inherits tcpclient, is it more suitable to use a udpclient to send and receive e-mail?
I searched this site again and found this article
http://www.developerfusion.com/show/4071/1/
If I was to strip out all my proprietary code you would end up with this example. Hopefully this will be enough to
get you started.
Cheers
Gord
Gordonm,
thank you so very much for your willingness to share your codes with me. YES, I do want it. A little detail about my program:
I am trying to built a mail server just like Yahoo or Hotmail, but it's a mini one It's supposed to receive an email then save the sender address, title, message, and date to SQL server 2000 database. I am writing it in C# and ASP.NET.
Once again, THANK YOU SO MUCH
Sincerely,
tinybunny_8
I was getting frustrated with not being able to find any examples of this so I wrote my own.
It is socket based, uses SMTP and seems to work very nicely. If you are interested let me know and perhaps I can post enough of an example to get people going.
Gord
It would appear so at a cursory glance....
I'll email Randy about that....
You can't receive email with SMTP - as its only for sending email! You use POP3 instead - see http://www.developerfusion.com/show/4039/
I agree with gordonm, if you know how to receive email using SMTP and C#... would you please write one or show me where to start? I am working on project and need a clue so badly.
Thanks
tinybunny_8
I would love to see an example of using Sockets to receive email. Could you please do an article covering that area.
TIA
Gord
You saved my day with this excellent example, thx Randy
In your Write() method, you declare a byte array of size 1024. Is that the maximum string length that you can send to the Write() method at once?
This thread is for discussions of Custom SMTP in C#.