1. Home
  2. Knowledge Base
  3. Web hosting
  4. How can I send an e-mail with authentication in C# (asp.net)?

How can I send an e-mail with authentication in C# (asp.net)?

Similarly to the php mail function you can use C# to send emails via a hosted page. You can send email from a web page – just follow the tutorial below.

  1. Make sure you are using a Win account, not one on a Linux server.
  2. Connect to your files via an FTP client.
  3. In the www directory, create two files – one named mail.aspx and one named web.config
    1. In the web.config enter the following and save:
      <configuration>
      <system.web>
      <customErrors mode="Off"/>
      </system.web>
      </configuration>
      
    2. In the mail.aspx file enter the following template and customize it as per the rules below.
      <%@ Import Namespace="System.Net" %>
      <%@ Import Namespace="System.Net.Mail" %>
      <script language="C#" runat="server">
      protected void Page_Load(object sender, EventArgs e)
      {
      MailMessage mail = new MailMessage();
      mail.From = new MailAddress("mailaddress@domain.ext");
      mail.To.Add("mailaddress-receiver@domain.ext");
      mail.Subject = "This is test subject";
      mail.Body = "This is the c sharp mail content";
      SmtpClient smtp = new SmtpClient("yourmailserver.domain.ext");
      NetworkCredential Credentials = new NetworkCredential("mailaddress@domain.ext", "password");
      smtp.Credentials = Credentials;
      smtp.Send(mail);
      lblMessage.Text = "Mail Sent";
      }
      </script>
      <html>
      <body>
      <form runat="server">
      <asp:Label id="lblMessage" runat="server"> </asp:Label>
      </form>
      </body>
      </html>
      
    3. Customize the template like so (and make sure you keep the quotes):
      1. Change the email address in the mail.From line. This is the sender.
      2. Change the email address in the mail.To.Add line. This is the receiver.
      3. To change the subject edit the mail.Subject line.
      4. To change the content of the message edit the mail.Body line
      5. The email address in the NetworkCredential Credentials line is the sender – you need to authenticate with a password
      6. The ‘password’ in the same line is the password of your email address.Password
    4. Save/Upload.
  4. Test! Open the .aspx file in your browser and you should receive your email..aspx file Received email
Updated on 24 March 2020

Was this article helpful?

Need Support?
Can't find the answer you're looking for?
Contact Support

Couldn't find a solution?

support_bottom_contact_alt

Our specialists are available 24/7 to provide you with free support. Feel free to contact Joachim and his colleagues via e-mail or by phone.

support_bottom_contact_alt
Joachim Coessens Specialist Support