Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7803469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:35:39+00:00 2026-06-02T01:35:39+00:00

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

  • 0

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 (smtp.gmail.com) settings dont work. i have to change settings everytime to (relay-hosting.secureserver.net) 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
                           };
    mMailMessage.To.Add(maTo);
    var mSmtpClient = new SmtpClient
                                 {
             UseDefaultCredentials = false,
             Host = mc.Host,
             Credentials = CredentialCache.DefaultNetworkCredentials,
             DeliveryMethod = SmtpDeliveryMethod.Network};
             mSmtpClient.Send(mMailMessage);

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:35:40+00:00Added an answer on June 2, 2026 at 1:35 am

    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 127.0.0.1 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("username@mydomain.com", "****"); 
                mailClient.Host = "smtp.gmail.com"; 
                mailClient.Port = 587; 
                mailClient.EnableSsl = true; 
                mailClient.DeliveryMethod = SmtpDeliveryMethod.Network; 
                mailClient.UseDefaultCredentials = false; 
                mailClient.Credentials = basicCredential; 
    
    
                MailMessage message = new MailMessage(); 
    
                MailAddress fromAddress = new MailAddress("info@mydomain.com", "Me myself and I "); 
                message.From = fromAddress; 
                //here you can set address    
                message.To.Add("to@you.com"); 
                //here you can put your message details 
    
                mailClient.Send(message);
    

    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;
    

    Edit

    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
    

    Ref:
    Request.UserHostName
    How to get the IP address of a machine in C#
    get remote Ip address or machine name

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Sending email in .NET through Gmail I don't know what is the
I am using this code to send an SMTP email via the yahoo SMTP
Possible Duplicate: Sending email through Gmail SMTP server with C# I would like my
I am wondering if there is a way to send an email (SMTP) through
I know it is possible to send email through php using mail(), however I
I'm trying to send an email via some C# ASP.NET code. This is being
I am trying to send an email using the following very standard code. However,
I have a VPS with cPanel and I'm using PHPMailer to send email through
I'm trying to send an email with Perl through SSL. The best package I've
I am trying to send an email through php. I have tried sending to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.