我知道如何在 C# 代码中通过 smtp 发送电子邮件
如果我设置一个 gmail smtp,它在本地主机上工作正常 但是当我上传并使其在线时,gmail (smtp.gmail.com) 设置不起作用。上传后我每次都必须将设置更改为 (relay-hosting.secureserver.net) 在 godaddy
现在我的问题是!有什么办法可以确定我是在代码中还是在本地主机上在线然后动态更改设置我将我的设置存储在数据库中 我的工作代码是
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);
我不想每次都更改我的设置,无论是在线还是在本地主机环境中开发
我想要这个流程,我怎么知道我的应用程序是在线的还是代码隐藏的本地主机
if(myconnection ==localhost) then fetch gmail credentials
else if (myconnection==online) then fetch godaddys credentials
最佳答案
引用: Determine if ASP.NET application is running locally和 How secure is Request.IsLocal?
As per the comment the best way to check for
localhost
usingHttpContext.Current.Request.IsLocal
property. It is same as like Request.IsLocal is the same as checking for 127.0.0.1 or ::1.
使用这个:
bool isLocal = HttpContext.Current.Request.IsLocal;
if(isLocal) then fetch gmail credentials
else if (myconnection==online) then fetch godaddys credentials
通过 gmail 发送邮件的示例代码片段是:
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);
如果这些设置与配置匹配,那么您肯定能够通过 gmail 发送邮件。
检查您的这部分设置。
NetworkCredential NetCrd = new NetworkCredential(youracc, yourpass);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = NetCrd;
编辑
点击这些链接,这些不是您问题的确切原因,而是在您的本地计算机上解决此问题的指南:
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
另一方面,您可以使用 HttpRequest Class 检查本地机器 ip 或机器名称,以确定远程服务器上的网站或您自己的机器。 HttpRequest.UserHostAddress Property .
如果 Ip 与 ip 请求对象匹配,则将 ip 地址返回到您的机器地址。
if (ip match to your machine)// local host else run your remote settings
引用:
Request.UserHostName
How to get the IP address of a machine in C#
get remote Ip address or machine name
关于c# - 从本地主机和在线 asp.net 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10205268/