Mail settings in Web.config - asp.net

Is it possible to set a "to" attribute in the mailSettings element in the Web.config file?

No, it isn't
Here is the docs for mailSettings: http://msdn.microsoft.com/en-us/library/w355a94k.aspx
Set the default "to" in an AppSetting instead and use that from you mail sending logic.
This is an example taken from the msdn docs:
<mailSettings>
<smtp deliveryMethod="network" from="ben#contoso.com">
<network
host="localhost"
port="25"
defaultCredentials="true"
/>
</smtp>
</mailSettings>

You can add a Key
<appSettings>
<add key="EmailToAddress" value="1337#gmail.com"/>
</appSettings>
And from your codebehind you can get it like this
var toAddress= ConfigurationManager.AppSettings["EmailToAddress"];

No, you can only specify where the mail is coming from:
MSDN Link

No it's not, but this would be highly restrictive as you'd likely want to send emails to many different people, perhaps based on some other data.
If you're only sending to one address all the time (such as an admin account) then I recommend just putting the address into the web.config as an 'appSetting' key and reading that instead.

Related

Can an ASP.NET Web.Config file have a dynamic value?

Not really sure if i'm asking this correctly or not, but can a web.config file have a data driven value.? For example, in my mailSettings section, i have this
<system.net>
<mailSettings>
<smtp deliveryMethod="network" xdt:Transform="Replace">
<network host="11.222.33.4" userName="MyUsername" />
</smtp>
</mailSettings>
</system.net>
i would like the host="11.222.33.44" to come from a database.. Is this possible.?
You can read the SMTP configuration from web.config file. I don't think there can be a dynamic variable in web.config file
SmtpClient client = new SmtpClient();
string host = smtp.Host; // you will get the host value here. Then update the host value here with the database host value
You could ignore web.config altogether and create an smtp client on the fly. Get your value from a database however you like then
System.Net.Mail.SmtpClient c = new System.Net.Mail.SmtpClient(< host name or ip address >);
c.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

<MailSettings> Use deliveryMethods 'Network' and 'SpecifiedPickupDirectory' simultaneously

Exactly as it says on the tin: is it possible to specify both delivery methods in a single configuration? I'd figure it'd be a quick (and cheap) solution to log all e-mail messages that were sent directly by the standard .NET SmtpClient.
What I'm trying to achieve here is a solution in which every e-mail sent by an defaultly SmtpClient instance is both submitted directly to a configured SMTP server and stored in a pickup directory, which merely will act as a storage point for logging the sent e-mail messages.
In other words: Is a configuration as follows possible?
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="source#domain.com">
<network host="127.0.0.1" />
</smtp>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\SmtpLog" />
</smtp>
</mailSettings>
</system.net>
Don't believe this is possible in the manner you suggest - see Scott Gutherie's response on http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx (search page for "multiple SMTP servers"). Old message but still current from what I can tell.
I think the system.net mailSettings in web.config are supposed to be considered the default settings.
However you can do it in code easily enough (plenty of examples out there) and you could do it so that you can configure in web.config using custom settings.

Can I set ntlm authentication for a mail server via the web.config file?

Is it possible to specify that you want to use NTLM authentication for SMTP using system.net, all via the web.config file? If so, what needs to be done?
I've been told adding the following will cause NTLM or Kerberos to be used if the SMTP server requires it, but our SMTP server is not authenticating. (I'm no SMTP expert).
<configuration>
<system.net>
<mailSettings>
<smtp>
<network host="yourhost.com" userName="user#yourhost.com" password="yourpassword" port="yourport" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
E.g. Should there be authentication modules specified?
You may have a look at this blog

Smtp Config in asp.Net

Hai,
I have developed small asp.net application.i send mail through this application.so my web config file have following coding
<network host="smtp.gmail.com" port="587" userName="username#domainname.com" password="*****" />
</smtp>
</mailSettings>
we have google apps.we don't have mail server.
i try to sent mail,i getting time out error.
regards
mariyappan.J
If you are using System.Net.Mail.MailMessage try this:
yourMailMessage.EnableSsl = true;
This could be:
Firewall related
IIS User account related
Have you tried smtp.googlemail.com Reading this it doesn't matter
I realise that's a bit of a generic answer but your config settings are correct so logically it can only be related to the network, or the user.

How do you configure Password Recovery control in ASP.Net?

I have a password recovery control that is set up to ask the user their security question. I have configured the from email and subject, but I am receiving a runtime error stating SMTP host is not specified.
Can someone tell me where I need to set the host?
Thanks
Add a system.net section to your web.config with the following:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="yourmailserveraddress"/>
</smtp>
</mailSettings>
</system.net>

Resources