Storing SMTP outside the Web.Config File - asp.net

For some time now I've been storing my connection and app settings in an external file and just referencing that file via my web.config. This works really well because it allows me to keep separate connection strings and app settings. This is really handy since I find during development I will often make many changes to the webconfig and I hate having to manage the environment specific values every time I need to update my web.config.
Is there anyway I can achieve this with he SMTP configuration sections in the web.config.

Sure, you can use the configSource attribute.
Example:
<system.net>
<mailSettings>
<smtp configSource="MailSettings.config"/>
</mailSettings>
</system.net>
Then put your mailSettings configuration data in MailSettings.config
So then your MailSettings.config file would have something like:
<network
host="relayServerHostname"
port="portNumber"
userName="username"
password="password" />
Update: looks like it may need to actually go in the smtp node to work properly, so I've updated the above code to indicate that - same idea, only this one should work. :)

I'm not sure if what I have here is only for newer versions of .NET. I got a runtime error using the accepted answer.
Please update the accepted answer with the code block below if working with newer versions of .NET. The smtp node should also be in the separate file - not just the network node. The whole smtp node in the actual Web.config file is replaced by the file you put there - unlike in the appSettings where it seems to add to it.
Web.config -
<system.net>
<mailSettings>
<smtp configSource="your-source-file">
</smtp>
</mailSettings>
</system.net>
Your file -
<smtp from="noreply#example.com">
<network
host="your-host"
port="your-port"
userName="your-user-name"
password="your-password"/>
</smtp>

My software stores it in the registry, even in production.

Related

SMTP windows server 2008 Asp.net Pickup Directory

I have configured an smtp server over a IIS 6.0 in windows server 2008.
I have already granted:
-Administrators
- NT AUTHORITY\LOCAL SERVICE
- NT AUTHORITY\NETWORK SERVICE
- IUSR_MACHINE_NAME
- MY Domain User
The folder structure for the SMTP service is the following:
- Badmail
- Drop
- Pickup
- Queue
I have a mvc application that send emails via pickup directory, the configuration is the following:
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="E:\Email\dev\Pickup" xdt:Transform="Replace" />
</smtp>
the problem is when I set up the pickup directory the application never writes the email files to that specific folder, the odd thing is that I'm not getting any exception either, I guess this could be an issue related to some user permissions but I already have granted the full control to everyone in that folder as well.
The fact is that I'm not getting get any email.
this environment isn't for production, and is just for testing purposes,
Any Advice?
Try below code, this works for me, I have been using it in my project and when sending mail, the eml file gets created here.
<system.net>
<mailSettings>
<!-- Method#2: Dump emails to a local directory -->
<smtp from="admin#abc.com" deliveryMethod="SpecifiedPickupDirectory">
<network host="localhost" />
<specifiedPickupDirectory pickupDirectoryLocation="F:\Project\Mails\" />
</smtp>
</mailSettings>
</system.net>

System.Net.Mail.SmtpException: Cannot get IIS pickup directory

Fellas,
Getting the "Cannot get IIS pickup directory" exception with my app. I have my directory created and specified in the config as follows:
<system.net>
<mailSettings>
<smtp deliveryMethod="PickupDirectoryFromIis" from="donotreply#mysite.com">
<specifiedPickupDirectory pickupDirectoryLocation="C:\inetpub\email\mysite"/>
</smtp>
</mailSettings>
</system.net>
Trying to have the emails placed locally in a directory so I can see them.
Any ideas why it could be throwing that exception?
FWIW, this is IIS7 + Windows 7 and an MVC2 app (C#).
Also, went ahead and gave permissions to the IIS_IUSRS group to that directory.
All,
Seems like I was using the wrong smtp deliveryMethod. I got the PickupDirectoryFromIis and SpecifiedPickup deliveryMethod's mixed up, and hence was using the wrong settings for it.

How do I set up smtp on Vista so I can use System.Net.Mail from local picup directory?

I've tried using the below solution (also found here: How do I set up smtp on Vista so I can use System.Net.Mail?) but I receive "Cannot get IIS pickup directory". I know I am pointing to an existing directory and my web.config are set up correctly, are there permissions I need to set up on the directory before I can write to it?
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory
pickupDirectoryLocation="c:\maildrop"/>
</smtp>
</mailSettings>
Have you checked the file permissions on the maildrop folder? You can enable write for all users and see if that works. If you just want IIS to write to it, I think the user is ASPNET, but I might be wrong on that.
Also this answer seems to show a "\" at the end of the directory path (I would hope asp.net would understand that though)

Web.config in asp.net?

I have a doubt in my web application can i place two nework tags in smtp mailSetting tag.
I PLACED in SMTP two network tags , BUT WHEN I AM SENDING MAIL I AM GETTING THIS ERROR. The element may only appear once in this section. (C:\Inetpub\vhosts\example.com\httpdocs\web.config line 64)
THIS IS MY WEB CONFIG CODE ......
<system.net>
<mailSettings>
<smtp>
<network host="webmail.example.com" port="25" userName="info#example.com" Password="asdf" defaultCredentials="false"/>
<network host="webmail.yyy.com" port="25" userName="info#yyy.com" Password="asdf254" defaultCredentials="false"/>
</smtp>
</mailSettings>
You can use the AppSettings section and add as many configuration values as you like. You can use for example:
<appSettings>
<add key="SMTP1" value="smtpserver1"/>
<add key="SMTP2" value="smtpserver2"/>
<add key="SMTP3" value="smtpserver3"/>
<add key="SMTP4" value="smtpserver4"/>
</appSettings>
and then in your code decide which server to use.
Nope you cannot. Though you can carry as many details in AppSettings section.
The SMTP details section is used to specify default values. To have more than one you need to write some code to read your own custom values from appsessings or even implement your own config section.
If you want different settings for different deployment configurations I would use Web.config Transformation. It was introduced in ASP.NET 4.0.
You can have one default setting for when you build the project on your localhost and when you publish it to the server, another one will be used.
Reference:
http://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx

Multiple ASP.NET Configuration Files

I have found a number of pieces of information on having multiple ASP.NET configuration files for a web deployment. However, I am still unsure if I can accomplish what I want to accomplish.
Basically, the website I am working on can be deployed to three different sites. Depending on the site that it is deployed to, the configuration settings need to be changed. What I would like to do is have configuration files for each possible configuration (full web.config files) and then be able to tell a deployment build which config file to use for a particular deployment (I can edit this manually if necessary).
Is there something as simple as pointing to a different .config file, or do I need to do something more sophisticated?
EDIT: One particular concern that I have is that I also need settings in system.net for mail settings, etc. So, I'm not looking to only override the appSettings.
Thanks
Any configuration section - such as <smtp> - can be "externalized", e.g. stored in an external file.
Other than the file= statement on <appSettings> (which is available only for app settings :-() it's not a "additional" setting - you just point your config system to an external file.
So you could have this in your app.config/web.config:
<system.net>
<mailSettings>
<smtp configSource="smtp.test.config" />
</mailSettings>
</system.net>
and this in your smtp.test.config:
<?xml version="1.0" encoding="utf-8" ?>
<smtp>
<network host="smtp.test.com" port="244" userName="test" password="secret" />
<specifiedPickupDirectory pickupDirectoryLocation="C:\temp\mails"/>
</smtp>
This works as of .NET 2.0 (maybe even 1.x), and it works for every configuration section - but not for configuration section groups like <system.web>.
Of course, you can now create additional config files, like smtp.staging.config and so forth, and now your problem has been reduced to replacing a single line in your web.config.
You can do this using an installation script, a XML preprocessor, or even by human intervention.
It doesn't completely solve the problem as .NET 4 and the web.config transformations hopefully will, but it's a step and a bit of help.
On your main web.config add the following attribute to appSettings
<appSettings file="Web.site1.config">
Then, asp.net will see both files as one. You can edit web.config in order to include a different external file, depending on the Site.
You can do this in .Net Framework 4. ScottGu shows a quick demo of it in his recent talk in Sweden. In his example he has staging, production etc. with each file having (potentially) different content.

Resources