Search code examples
asp.netweb-configmailto

Set a "mailto" address in web.config file of asp.net app?


I have a simple landing page on an ASP.NET web app that is supposed to allow the user to email a support center by clicking a link.

Currently, I have two issues: one, the typical "mailto:address" functionality is not working at all. And two, I've been asked to replace the hard-coded address on the page with a reference to an address that's set in web.config (as we expect the address to change soon).

I have tried the following code in the .aspx file, which results in nothing when the link is clicked:

<a href="mailto:support@myorg.org">System Support</a>

As for setting the email in the web.config file, I'm not sure what setting can be used to do that at all.

So, expected result: clicking the link opens a new email to our org's support address, which is specified not on the page but as a reference to a system variable (I was told it should be on web.config...).

Actual result: nothing happens at all.

I'm pretty new to this, so any help at all is appreciated. Cheers


Solution

  • IF the link is not working, that is most likely not related to asp.net but to your mail software. Can you click other mailto: links? For the latter, you can add arbitrary settings to the web config and it will roughly look like this

    web.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="SupportLink" value="mailto:support@myorg.org" />
      </appSettings>
    </configuration>
    

    Code:

    var supportAddress = ConfigurationManager.AppSettings["SupportLink"];
    

    ...and then ofc you have to use it. I suggest reading a few asp.net tutorials