45

I have some code that generates an email and I want to include the actual server machine name that is running in the body of the email. (It's a clustered web environment, so I want to find out what actual machine is sending the email.)

I know I can use Request.Url.AbsoluteUri, but that gives me the URL (like www.mysite.com/MyController/Action) instead of the raw actual machine name - which might be MACH3234k323).

Is there a way that I can get the actual server machine's name?

BWA
  • 5,672
  • 7
  • 34
  • 45
leora
  • 188,729
  • 360
  • 878
  • 1,366

3 Answers3

104

Use System.Environment.MachineName

Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72
  • 3
    Great; note that the name returned is apparently always **all-uppercase** (even when it wasn't assigned that way). – mklement0 Oct 22 '13 at 17:40
  • 4
    System.Environment.MachineName.ToLower will fix that! :-) – Jeff Jan 15 '14 at 18:02
  • 2
    @Jeff How can someone upvote the "fix" of ToLower? This will NOT fix the case that the machine name was assigned with upper and lower case, and plain wrong if it was assigned all-uppercase. If you meant it as humor, you could also propose to hard-code the machine name, with the added advantage that such code runs faster. Instead, I'll upvote mklement0 mentioning the bug – Roland Nov 15 '19 at 16:42
23

The name of the machine will be here:

System.Environment.MachineName
Chris Adams
  • 1,008
  • 1
  • 11
  • 22
2
Request.ServerVariables["SERVER_NAME"]
kravits88
  • 12,431
  • 1
  • 51
  • 53