Wednesday, April 18, 2012

sending emails from localhost and online asp.net

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




No comments:

Post a Comment