This seems to be pretty straight forward. I need to send email from some ASP.NET applications. I need to do this consistently without strange errors and without CPU utilizat
We have used hMailserver with great success. The configuration can take a while to get used to but it has been a great -- and free -- mail server product.
If you want to roll your own (which I did years ago when I was having a bear of a time with CDONTS), you can start with the code below and customize to your heart's content. It uses the TcpClient to create a TCP connection directly to the mailserver. Not that I'd recommend this when there are so many established and debugged solutions, but I found this very useful for debugging and determining where the problem was with the prefab MS mail components.
private void Send_Email()
{
#region Email Sending Function
string strMail = "";
try
{
// See RFC821 http://www.faqs.org/rfcs/rfc821.html for more specs
// TcpClient is an abstraction of a TCP Socket connection
TcpClient myTCP = new TcpClient();
// Connect to the mail server's port 25
myTCP.Connect(mailserver, 25);
// Open a network stream which sends data to/from the TcpClient's socket
System.Net.Sockets.NetworkStream ns = myTCP.GetStream();
// The data to send to the mail server, basically a raw SMTP mail message
strMail = "HELO\n";
strMail += "MAIL FROM:from@address.com\n";
strMail += "RCPT TO:" + recipient + "\n";
strMail += "DATA\n";
strMail += "Subject: mySubject\n";
strMail += "To:" + recipient + "\n";
strMail += "From: \"From Real Name\" \n";
strMail += "\n";
strMail += " ---------------------------------------\n";
strMail += "Name: " + txtName.Text + "\n";
strMail += "Address1: " + txtAddress1.Text + "\n";
strMail += "Address2: " + txtAddress2.Text + "\n";
strMail += "City: " + txtCity.Text + "\n";
strMail += "State: " + txtState.Text + "\n";
strMail += "Zip: " + txtZip.Text + "\n";
strMail += "Email: " + txtEmail.Text + "\n";
strMail += "Dealer: " + txtDealer.Text + "\n";
strMail += " ---------------------------------------\n";
strMail += "THIS IS AN AUTOMATED EMAIL SYSTEM. DO NOT REPLY TO THIS ADDRESS.\n";
strMail += "\n.\n";
// Defines encoding of string into Bytes (network stream needs
// an array of bytes to send -- can't send strings)
ASCIIEncoding AE = new ASCIIEncoding();
byte[] ByteArray = AE.GetBytes(strMail);
// send the byte-encoded string to the networkstream -> mail server:25
ns.Write(ByteArray, 0, ByteArray.Length);
//ns.Read(ByteArray, 0, ByteArray.Length);
//lblStatus.Text = ByteArray.ToString();
// close the network stream
ns.Close();
// close the TCP connection
myTCP.Close();
}
catch(Exception ex)
{
throw new Exception("Couldn't send email: " + ex.Message);
}
#endregion
}