skhurams skhurams - 1 year ago 145
ASP.NET (C#) Question

sending emails from localhost and online

I know how to send email through smtp in code in c#

if i set a gmail smtp it works fine in localhost
but when i upload and make it online then gmail ( settings dont work. i have to change settings everytime to ( at godaddy after uploading

Now my question is! Is there any way i can find out if im on localhost in code or online then change the settings dynamically im storing my settings in db
my working code is

mm.LoadByPrimaryKey(4);//get body , subject etc from db
mc.LoadByPrimaryKey(1);// get settings from db (host, from , port etc)

var maTo = new MailAddress(strEmail, userName);
var mMailMessage = new MailMessage
Subject = mm.Subject,
Body = strBody,
IsBodyHtml = true,
Priority = MailPriority.High,
From =new MailAddress(mc.AdminEmailAddress),
DeliveryNotificationOptions= DeliveryNotificationOptions.OnFailure
var mSmtpClient = new SmtpClient
UseDefaultCredentials = false,
Host = mc.Host,
Credentials = CredentialCache.DefaultNetworkCredentials,
DeliveryMethod = SmtpDeliveryMethod.Network};

i dont want to change my settings everytime, wether im online or developing in localhost environment

i want this flow and how do i know my application is online or localhost in code behind

if(myconnection ==localhost) then fetch gmail credentials
else if (myconnection==online) then fetch godaddys credentials

Answer Source

Ref: Determine if ASP.NET application is running locally and How secure is Request.IsLocal?

As per the comment the best way to check for localhost using HttpContext.Current.Request.IsLocal property. It is same as like Request.IsLocal is the same as checking for or ::1.

use this:

bool isLocal = HttpContext.Current.Request.IsLocal;

if(isLocal) then fetch gmail credentials 
else if (myconnection==online) then fetch godaddys credentials 

Sample code snippet to send mail through gmail is:

SmtpClient mailClient = new SmtpClient(); 
            //This object stores the authentication values      
            System.Net.NetworkCredential basicCredential = 
                new System.Net.NetworkCredential("", "****"); 
            mailClient.Host = ""; 
            mailClient.Port = 587; 
            mailClient.EnableSsl = true; 
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network; 
            mailClient.UseDefaultCredentials = false; 
            mailClient.Credentials = basicCredential; 

            MailMessage message = new MailMessage(); 

            MailAddress fromAddress = new MailAddress("", "Me myself and I "); 
            message.From = fromAddress; 
            //here you can set address    
            //here you can put your message details 


if these setting match to configuration then you are definitly able to send mail through gmail.

Check your this section of settings.

 NetworkCredential NetCrd = new NetworkCredential(youracc, yourpass);   
  smtpClient.UseDefaultCredentials = false;    
  smtpClient.Credentials = NetCrd;


follow these links, these are not the exact reason of your problem but guide to solve this on your local machine:
I can receive mail but not send from an account I set up in windows live mail
sending email using gmail account on local machine

Another aspect is that you can check the local machine ip or machine name to figure out either website on remote server or your own machine using the HttpRequest Class HttpRequest.UserHostAddress Property.

If Ip matches to ip request object returns ip address to your machine address.

if (ip match to your machine)// local host else run your remote settings

How to get the IP address of a machine in C#
get remote Ip address or machine name

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download