4

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 
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
skhurams
  • 2,133
  • 7
  • 45
  • 82

3 Answers3

3

Why don't you use the Web.Config?

<system.net> 
<mailSettings> 
<smtp from="YYYYY@xxxxxx.com"> 
<network 
host="mail.xxxxxx.com" 
port="25" 
password="password" 
userName="user@xxxxxx.com" 
defaultCredentials="false" 
/> 
</smtp> 
</mailSettings> 
</system.net> 
Pleun
  • 8,856
  • 2
  • 30
  • 50
  • i just want the user to edit credentials and i have given a setting page to admin he gets data from db edit it and save it that why i m not saving it on web.config – skhurams Apr 18 '12 at 08:05
  • Well, that you did not specify in your original question. Also your comments to the other answers indicate an entirely different matter (how can I know if it is localhost or not). If you want to have good answers your questions should be as to the point as possible. better to ask an extra question than to mix them – Pleun Apr 18 '12 at 09:03
3

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

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • i dont want to send email through gmail on online. Please consider this (its my client requirement) relay-hosting.secureserver.net is godaddys smtp i cant use it on localhost it gives ip not authenticated type of error, which means when im on localhost i have to use other smtp like gmail, now i cant test my emails on localhost – skhurams Apr 18 '12 at 08:31
  • i want this flow if(myconnection ==localhost) then fetch gmail credentials else if (myconnection==online) then fetch godaddys credentials how can i know in my code that im online or in localhost environment my email code is working fine dont send me the code – skhurams Apr 18 '12 at 08:40
  • Or just use the HttpRequest.IsLocal Property – Pleun Apr 18 '12 at 12:40
  • @Pleun: i explored about this and you are correct.. i have updated the right information.. thanks – Niranjan Singh Apr 18 '12 at 13:09
1

If you just want to check if you are running on localhost or not, then you can do this kind of thing:

if (Request.UrlReferrer.DnsSafeHost.ToLower() == "localhost")
{
  // Do it the gmail way
}
else
{
  // Use the online stuff
}

The nice thing about this is that you don't need to check the actual URL unless you want to (so you don't need to change any config files or anything).

I have used this "in anger" as it were and it works fine.

Obviously the question about storing gmail credentials in your live code is a separate issue and was not part of your question. Actually I think you said you are storing them in the database which is fine.

EDIT: When I've used this, I check that the Request.UrlReferrer.DnsSafeHost is my actual live domain to prevent a simple attack where somebody posts to my page from a different source.

If you JUST want to check where you are running and don't care how you got there (not necessarily a good idea), you can use Request.Url.DnsSafeHost.

Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
  • isn't this a complicated way to imitate HttpRequest.IsLocal Property – Pleun Apr 18 '12 at 12:42
  • Lol! I think it probably is. Although when I've used it I've always checked for the specific URL of the live server (as mentioned in my edit from a couple of hours ago), which `HttpRequest.IsLocal` can't do. – Tom Chantler Apr 18 '12 at 13:10