Posts:
Example    jeff
Send Email from ASP.NET Web Applications (System.Net.Mail.SmtpClient)

Sending email is pretty easy but the one sticky part is authenticating on a remote email server. The example below shows one way to provide user credentials to authenticate and be able to relay email messages to remote email domains.

There are a few different ways to send email with .NET 2.0, the static method below is just a simple example of one approach. You can use this method to test sending email with your mail server. I have tested it and it works. Once you verify that you are able to send email, you can build your own customized email classes and methods that are more suited to your needs.

Note: You should always try sending email to a remote domain to be sure that your credentials are allowing you to relay properly. Many times you will not be authenticated but if you are sending email to an email address on the same domain as the email server and sender account, it will let it go as if everything worked fine.

public class TestEmail
{
    public static string SendEmail(string from, string fromDisplay, string to, string sender,
        string replyTo, string subject, string body, System.Text.Encoding subjectEncoding,
        System.Text.Encoding bodyEncoding, bool isBodyHtml, string smtpHost, string smtpUser,
        string smtpPass, int smtpPort, string smtpAuthType)
    {
        string response = null;
        System.Net.Mail.MailAddress maFrom = new System.Net.Mail.MailAddress(from, fromDisplay);
        System.Net.Mail.MailAddress maTo = new System.Net.Mail.MailAddress(to);
        System.Net.Mail.MailAddress maSender = new System.Net.Mail.MailAddress(sender);
        System.Net.Mail.MailAddress maReplyTo = new System.Net.Mail.MailAddress(replyTo);
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(maFrom, maTo);
        msg.Body = body;
        msg.Sender = maSender;
        msg.ReplyTo = maReplyTo;
        msg.Subject = subject;
        msg.BodyEncoding = bodyEncoding;
        msg.IsBodyHtml = isBodyHtml;
        msg.SubjectEncoding = subjectEncoding;

        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpHost);
        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(smtpUser, smtpPass);
        smtp.Credentials = (System.Net.ICredentialsByHost)nc.GetCredential(smtpHost, smtpPort, smtpAuthType);

        try
        {
            smtp.Send(msg);
        }
        catch (Exception ex)
        {
            response = ex.Message;
            response = response.Replace("\r\n", "<br/>");
        }
        return response;
    }

    //Here is an example of invoking this method:

    private void TestEmail()
    {
        string to = "myemail@anotherdomain.com";
        string host = "mail.mydomain.com";
        string from = "myemail@mydomain.com";
        string fromDisplay = "My Name";
        string user = "myemail@mydomain.com";
        string password = "mypassword";
        int port = 25;
        string subject = "Test Email using SendEmail";
        string body = @"<html><body><div style=""font-family:verdana;font-size:10pt;"">
            This is a test email.</div></body></html>";

        // AuthTypes: "Basic", "NTLM", "Digest", "Kerberos", "Negotiate"
        string authType = "Basic";

        string response = Email.SendEmail(from, fromDisplay, to, from, from, subject,
            body, System.Text.Encoding.ASCII, System.Text.Encoding.UTF8,
            true, host, user, password, port, authType);

    }
}

-Jeff