ASP.net SMTP Mail though Proxy - c#

How to setup SMTPClient in ASP.net with C# to send email with provided proxy address ? or sending by detecting the system default proxy
I used the the following code in web.config but didnt work
<system.net>
<defaultProxy enabled="true">
<proxy bypassonlocal="False" proxyaddress="http://192.168.101.3:8080/" />
</defaultProxy>
</system.net>

You can't send SMTP email through an HTTP proxy server.
Speak to the person administering your Internet gateway/firewall to ask what host they want you to direct outbound email to.

Based on this article, you simply have to declare your proxy configuration into your Web.config and try to change the port:
If you are still having problems them try changing port number to 587
Edited my answer after your edit.

Related

Default Proxy ASP.NET Core Web API

I need to configure a default proxy to enable Application Insights.
In an ASP.NET Web Api the default proxy can be set in the web.config like below:
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy proxyaddress="someaddress" bypassonlocal="True" />
</defaultProxy>
</system.net>
With this setting AI works fine. Unfortunately I could not find an option to set the proxy in appsettings.json. Is there any possibility to define the default proxy in an ASP.NET Core Web API?
Or do I have to configure the proxy in IIS, when the application is getting hosted with it?
Edit:
I am setting the default proxy now like this:
WebRequest.DefaultWebProxy = new WebProxy(new Uri("someaddress"), true)
{
UseDefaultCredentials = true
};
When getting the proxy with
WebRequest.DefaultWebProxy.GetProxy(new Uri("https://dc.services.visualstudio.com/v2/track"))
it returns the correct proxy.
Strange things happens when Fiddler is running.
As soon as Fiddler is running the application is logging successfully to AI. When closing Fiddler the logging breaks again.
Has anyone an idea?
Do you mean you want to configure HttpClient proxy globally?
If so, I am afraid it is impossible. You may consider creating an encapsulate class with accepting Proxy configuration and return a HttpClient, you could load the configuration from appsettings.json or anywhere
Perhaps your traffic is going through Fiddler Proxy so when Fiddler shuts down so as your connection to other services.

How to capture a server side web api call in fiddler?

I have got the a service with the following topology.
A (Web application) ---calls---> B (Local Web Api) ---calls---> C (Remote Web Api)
I am trying to capture the traffic from B to C in Fiddler, but nothing is logged.
B is a web site hosted locally on my dev box in IIS. It's running on an app pool with a credential other than mine as it has a different set of permissions. Traffic going out from B is not getting logged, even after I have done the following redirect:
<system.net>
<defaultProxy
enabled = "true"
useDefaultCredentials = "true">
<proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
</defaultProxy>
What can I do to capture traffic leaving B?
The problem is that the .NET Framework is hardcoded to bypass the proxy for localhost addresses. Change the target address to localhost.fiddler and the traffic will be captured.

Verify Service Proxy Useage

Referencing this answer, I'm attempting to establish a proxy connection for a service reference, but am having trouble verifying that it's actually being used.
In my app.config, I have
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy
usesystemdefault="False"
proxyaddress="http://proxyaddresst.com:80"
bypassonlocal="False"/>
</defaultProxy>
</system.net>
but would like to be able to list the address of the proxy being used in the program itself, through
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " Proxy: " + ???);
Is there a way to do this? It seems simple, but so did setting up the proxy in the first place, and the program isn't working as expected. I'd like to either verify the proxy is in use (and discount it as necessary) or verify that it isn't (and then look for another way to integrate it).

What is the purpose of specifying from attribute in smtp element?

When sending mail using system.net. Having specified from attribute in system.net->mailsetting->smtp. In send method we have to pass from address once again, so what is the purpose of specifying it in the config?
<smtp deliveryMethod="Network" **from="xyz#xyz.com"**>
<network host="smtpout.secureserver.net" port="80" userName="xyz#xyz.com" password="xyz-password" />
</smtp>
If the from address never changes, this is a good place to set it.
Many applications send email from a single account, so configuring it there instead of hard coding it makes sense.
This value is used in the parameterless constructor of MailMessage.

getting "remote server cannot be resolved" on localhost does not get fixed by defaultProxy?

here is the sitution, i am testing on my localhost from my machine at home (no proxy server and windows default firewall) and retrieving api.flickr.com xml file, when I come to work (that uses an ISA server to connect) I get "remote server could not be resolved" so I added these lines to the web.config
<system.net>
<defaultProxy>
<proxy
usesystemdefault="False"
proxyaddress="http://localhost"
bypassonlocal="True"
/>
<bypasslist>
<add address="[a-z]+\.flickr\.com\.+" />
</bypasslist>
</defaultProxy>
</system.net>
that returns:System.Net.WebException: The remote server returned an error: (404) Not Found.
what went wrong? thanks
There are two possible scenarios here:
1: If you are building a client app (e.g. Console or WinForms) and want to access http://localhost using WebClient or HttpWebRequest without any intervening proxies, then bypassonlocal="True" should accomplish this. In other words, your app.config should look like this:
<defaultProxy>
<proxy
usesystemdefault="False"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
2: if, however, you're trying to get your ASP.NET app (running on http://localhost) to be able to correctly resolve URIs either with a proxy or without one, then you'll need to set up proxy info correctly in your web.config (or in machine.config so you won't have to change your app's web.config), so ASP.NET will know that you are running a proxy or not running one. Like this:
Home:
<defaultProxy>
<proxy
usesystemdefault="False"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
Work:
<defaultProxy>
<proxy
usesystemdefault="False"
proxyaddress="http://yourproxyserver:8080"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
It's also possible to use proxy auto-detection, to pick up settings from the registry, etc. but I've always shied away from those approaches for servers apps... too fragile.
BTW, if you find that things are configured correctly, and you still get the error, the first thing I'd recommend is to code up a quick test which manually sets the proxy before your WebClient/HttpWebRequest call, instead of relying on configuration to do it. Like this:
WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebClient wc = new WebClient();
wc.Proxy = proxyObject;
string s = wc.DownloadString ("http://www.google.com");
If the requests don't go through your work proxy correctly even when you're using code, even if the proxy is correctly configured in your code, then the proxy itself may be the problem.
In WebClient to download data from local no issues but downloading from internet is problem so configure the following
In your Web.config add below lines and replace your Intenet proxy Address and port
<system.net>
<defaultProxy useDefaultCredentials="true" enabled="true">
<proxy usesystemdefault="False" proxyaddress="http://your proxy address:port" bypassonlocal="True" />
</defaultProxy>
<settings>
<servicePointManager expect100Continue="false" />
</settings> </system.net>
now your program logic work for downloading content from internet and public URLS.

Categories