Where to store global WebMail settings in MVC3? - asp.net

should I stored my global WebMail variables within Application_Start() event? I read creating a Base Controller is not necessary, and it makes sense because I have used hooks in MVC architecture before. Is there a way I can simply hook these in?
e.g. WebMail.SmtpServer, WebMail.UserName, WebMail.Password, WebMail.From?

How about in the web.config
<system.net>
<mailSettings>
<smtp>
<network
host="myHost"
port="myPortNumber"
userName="username"
password="password" />
</smtp>
</mailSettings>
</system.net>

Related

Google Email Client using .Net framework 4.0

I have created a gmail client using host as smtp.gmail.com and configures all the requires credentials and settings.
I have tested the client by hosting it in my local IIS. It worked great.
Then I hosted the project on public server and tested the client. here I am facing the problem that google stopping the signin and i received a mail saying that "Suspicious sign in prevented".
I have given a strong password and fully qualified email address. still I am getting the same problem.
what might be the cause for problem ?
here is my config file.
<?xml version="1.0"?>
<configuration>
<system.net>
<mailSettings>
<smtp from="xxxxxxxxxx#gmail.com">
<network defaultCredentials="false" host="smtp.gmail.com" port="587" userName="xxxxxxxxxxx#gmail.com" password="xxxxxxxxx" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
<system.web>
<compilation debug="false" targetFramework="4.0"/>
<httpRuntime/>
</system.web>
</configuration>
thanks in advance

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>

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

Storing SMTP outside the Web.Config File

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.

Resources