I seem to be stuck. I have an asp.net mvc 4 app and I want to send email from a service class. The service class is in a C# library project separate from the mvc project in the solution. I want to set the email configuration in a separate file that is called from a config file. Should the following go in the web.config in the mvc project or create an app.config file in the service project?
<system.net>
<mailSettings>
<smtp configSource="MailSettings.config" />
</mailSettings>
</system.net>
Then I have this in a separate file called MailSettings.config:
<smtp from="abc#email.com">
<network host="smtp.server.com" port="587" userName="abc#email.com" password="password" enableSsl="true" />
</smtp>
I have tried creating an app.config file with just the system.net stuff for the service project, but the mail settings are always null when I try:
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)ConfigurationManager.GetSection("system.net/mailSettings");
SmtpClient SmtpServer = new SmtpClient(settings.Smtp.Network.Host); //get null exception for settings.Smtp.Network.Host)
Also, I've tried including the mail settings in the app.config file to rule out having the MailSettings.config file being the issue and that still generates a null pointer.
I tried an example for accessing it from the web.config file like so:
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
In the service class though the WebConfigurationManager is out of context and so is the Request.ApplicationPath. So if I do have to get it from the web.config file, would I have to pass the http request object to the service class? That seems like a bad idea.
Just use:
SmtpClient SmtpServer = new SmtpClient();
This constructor will pick the configuration directly.
As the MSDN documentation for the parameterless constructor of SmtpClient says:
This constructor initializes the Host, Credentials, and Port properties for the new SmtpClient by using the settings in the application or machine configuration files. For more information, see <mailSettings> Element (Network Settings).
Related
I want to make a request to a resource (that resource is behind a proxy). I have the proxy address and the port as well. I have tried with NetworkCredentialn no success, with CacheCredentials no success. WebException is:
ProtocolError
The remote server returned an error: (407) Proxy Authentification Required
I always get error at this line:
WebResponse response = request.GetResponse();
I have already done this:
Package manager in Visual Studio 2015 "407 (Proxy Authentication Required)"
I tried to configure my App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>
From our corporate network, we usually employ this code:
WebProxy proxy = new WebProxy("http://your.proxy.server:8080", true);
proxy.Credentials = new NetworkCredential("user", "password");
WebRequest.DefaultWebProxy = proxy;
The idea is you put this code somewhere at the beginning of your program (or in the App start if you're on IIS) and then every single request will take the default proxy configuration.
No change in web.config is required. AFAICT, in web.config you cannot set the credentials.
In my experience, it works also for web services and WCF communications.
We built an ASP.NET app that we deployed on the client's server, using their SMTP server.
I'm trying to have Elmah send error emails using the ErrorEmailModule. This uses the SMTP settings as configured in the web.config, but somehow sending emails is not working (nothing is being sent).
The SMTP settings are configured like this:
<system.net>
<mailSettings>
<smtp from="support#xxxxxx.nl">
<network host="xxxxxxx.management.local" port="25" enableSsl="true" defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>
To test if this is a problem with Elmah or with the config I created a little test application that uses the same setting and tries to send an email:
string from = "support#xxxxxxxx.nl";
string to = ConfigurationManager.AppSettings["ErrorReportingEmail"];
var message = new MailMessage(from, to, "Test", "Test");
var smtpClient = new SmtpClient();
smtpClient.Send(message);
This doesn't work either. SmtpClient doesn't throw an error, it just acts like it sent an email, but I'm not receiving anything.
I then tried sending an email using 2 different email testing tools, called MailTester and MultiMail, I entered the same SMTP settings, and they can send emails just fine.
So my question is: why can I send emails from these mail tester apps but not from my ASP.NET app?
try this set deliveryMethod = network.
<system.net>
<mailSettings>
<smtp from="support#xxxxxx.nl" deliveryMethod="Network">
<network host="xxxxxxx.management.local" port="25" enableSsl="true" defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>
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.
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.
I use the standard web.config 'mailSettings' as the backbone for my ASP.NET emails. However, I find it to being increasingly limited in capability. For example, I want to send emails from many different user accounts (support, sales, management etc.) and it doesn't seem like mailSettings supports this. Is there any way to avoid using mailSettings so that I can use multiple accounts? Any disadvantages (and if not, then why would anyone use mailSettings?)
<mailSettings>
<smtp from="support#company.com">
<network host="smtp.gmail.com" port="587" userName="support#company.com" password="abcPassword"/>
</smtp>
</mailSettings>
If you're sending your mail using a System.Net.Mail.MailMessage, you should be able to override the From in the web.config by setting the MailMessage.From to a new MailAddress.