ASPNetFAQ.com: What is ASP.NET?

Technology posts on ASP.NET, IIS, Windows (+ a little Linux), Cloud Servers, Hosting, and more!
  • Blog Home
Search the site...

Sending Authenticated Email from ASP.NET C#

Tweet
Share
0 Shares

I previously wrote an article on what code you can use to send email using ASP.NET C#. That article assumed that the local SMTP service was allowing anonymous relay for code on the local server (i.e. 127.0.0.1). Below is a slightly revised code sample allowing for authentication against an SMTP server in case your server has that feature enable to prevent (or at least track) spam sending from code on the local server.

sendmail.aspx

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server"><title></title></head>
<body>
    <form id="form1" runat="server">
    <div>
        Message to: <asp:TextBoxID="txtTo" runat="server"></asp:TextBox><br/>
        Message from: <asp:TextBoxID="txtFrom" runat="server"></asp:TextBox><br/>
        Subject: <asp:TextBoxID="txtSubject" runat="server"></asp:TextBox><br/>
        Message Body: <br/>
        <asp:TextBoxID="txtBody" runat="server" Height="171px" TextMode="MultiLine" Width="270px">
        </asp:TextBox><br/>
        <asp:ButtonID="Btn_SendMail" runat="server" onclick="Btn_SendMail_Click" Text="Send Email"/>
        <br/>
        <asp:LabelID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>




sendmail.aspx.cs

using System;
using System.Web.UI.WebControls;
using System.Net.Mail;

publicpartialclass SendMail : System.Web.UI.Page
{
    protectedvoid Btn_SendMail_Click(object sender, EventArgs e)
    {
        MailMessage mailObj = new MailMessage(
            txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
        string userName = "ftpUser";
        string password = "ftpPassword";
        SmtpClient SMTPServer = new SmtpClient("localhost");
        SMTPServer.Credentials = new System.Net.NetworkCredential(userName, password);
        try
        {
            SMTPServer.Send(mailObj);
            Label1.Text = "Sent!";
        }
            catch (Exception ex)
        {
            Label1.Text = ex.ToString();
        }
    }
}

More from my site

  • PUT/POST/DELETE Verb Errors On SitePUT/POST/DELETE Verb Errors On Site
  • Orchard: Custom Content in Sub-FoldersOrchard: Custom Content in Sub-Folders
  • IIS7 Mime MappingsIIS7 Mime Mappings
  • Resolving a “There is a duplicate ‘system.web.extensions/scripting/scriptResourceHandler’ section defined” ErrorResolving a “There is a duplicate ‘system.web.extensions/scripting/scriptResourceHandler’ section defined” Error
  • Log Parser: Pulling Valuable Data From IIS LogsLog Parser: Pulling Valuable Data From IIS Logs
Tweet
Share
0 Shares
ASP.NET, cytanium, Development/Coding, Email, Hosting, OrcsWeb

4 comments on “Sending Authenticated Email from ASP.NET C#”

  1. vini says:
    July 8, 2011 at 2:38 am

    System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required. at System.Net.Mail.RecipientCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at Default2.Btn_SendMail_Click(Object sender, EventArgs e) in c:\Users\Vinay\Documents\Visual Studio 2010\WebSites\WebSite1\Default2.aspx.cs:line 25

    am getting this error please help

  2. Brad says:
    July 8, 2011 at 5:53 am

    Asumming your username and password are correct for the SMTP Service, it sounds like you may have a service that requires SSL. If so, try adding the line below just before the Credentials statement.

    SmtpClient.EnableSsl = True

  3. ANup says:
    August 31, 2011 at 3:42 am

    SMTPServer.EnableSsl = true;

  4. ANup says:
    August 31, 2011 at 3:42 am

    SMTPServer.EnableSsl = true;

Proverbs 19:20

"Get all the advice and instruction you can, so you will be wise the rest of your life."

A Note On WordPress Hosting

Our main focus is of course .NET, but with a mix of Linux, virtualization, and other technologies. But if you're really looking for the best WordPress hosting specifically, read my WordPress host review to save yourself hassle AND money!




Recent Posts

  • What makes good web hosting?
  • jQuery Mobile C# ASP.NET and N5 Networks Software Repository
  • Open Source Bug Tracking Software and the Orchard Project
  • ASP.NET Development with Dreamweaver MX: Visual QuickPro Guide
  • Kendo UI Sample, ASP.NET Ajax Tutorial & More

Tags

ASP.NET Automation centos CMS css cytanium Development/Coding Email gmail Hosting htaccess http https IIS javascript Learning Linux logparser MySQL nginx openssl OrcsWeb performance PowerShell redirect RHEL security server SherWeb smtp SQL/Databases ssl System Administration telnet terminal tip Troubleshooting Ubuntu virtualization Visual Studio web farm web hosting Windows windows server Wordpress

Categories

  • ASP.net development
  • Development/Coding
  • Hosting
  • IIS (Internet Information Services)
  • SQL/Databases
  • System Administration
  • Virtualization
(c) ASPNETFAQ.com