Set two smtp servers in config file - asp.net

I am trying to set two smtp servers in web.config file but get error
Unrecognized configuration section system.net/mailSettings/smtp_1.
How correctly to do that?
<configuration>
<configSections>
<sectionGroup name="mailSettings">
<section name="smtp_1" type="System.Net.Configuration.SmtpSection"/>
<section name="smtp_2" type="System.Net.Configuration.SmtpSection"/>
</sectionGroup>
</configSections>
<system.net>
<mailSettings>
<smtp_1 from="no-reply1#web2pdfconvert.com" deliveryMethod="specifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Users\Administrator\Projects\temp\wp" />
<network host="smtp...." enableSsl="true" userName="..." password="..." port="587" />
</smtp_1>
<smtp_2 from="no-reply2#web2pdfconvert.com" deliveryMethod="specifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Users\Administrator\Projects\temp\wp" />
<network host="smtp...." port="25" />
</smtp_2>
</mailSettings>
</system.net>
</configuration>

MailSettings is not intended for this purpouse: this section is the place in configuration where you can store SMTP params, so you won't need to change them programmatically when you create a new SmtpClient.
If you want you can create your own section but not change the original one, like this:
<configuration>
<configSections>
<sectionGroup name="myMailSettings">
<section name="smtp_1" type="System.Net.Configuration.SmtpSection"/>
<section name="smtp_2" type="System.Net.Configuration.SmtpSection"/>
</sectionGroup>
</configSections>
<myMailSettings>
<smtp_1 from="no-reply1#web2pdfconvert.com" deliveryMethod="specifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Users\Administrator\Projects\temp\wp" />
<network host="smtp...." enableSsl="true" userName="..." password="..." port="587" />
</smtp_1>
<smtp_2 from="no-reply2#web2pdfconvert.com" deliveryMethod="specifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Users\Administrator\Projects\temp\wp" />
<network host="smtp...." port="25" />
</smtp_2>
</myMailSettings>
....
and finally don't forget to write some code to use that data!

Related

how to update a section in Web.Config using transforms?

In my Web.Config I have the following
<configuration>
<configSections>
/*some code*/
</configSections>
<appSettings>
<add key="KeyName" value="KeyValue"/>
</appSettings>
</configuration>
And I have used below code when deploying a web application project.
{
"QA": {
"ConfigChanges": [
{
"KeyName": "/configuration/appSettings/add[#key='KeyName']",
"Attribute": "value",
"Value": "NewKeyValue"
}
]
}
}
Above code finds the configuration with appkey name and overwrites it.
But how do we configure if it doesn't have any key, like smtp settings?
<system.net>
<mailSettings>
<smtp from="" deliveryMethod="Network">
<network host="smtp.gmail.com" port="57" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
I tried like below like using key, but I got the error.
{
"KeyName":"/configuration/system.net/mailSettings/smtp/network[#defaultCredentials='true']",
"Attribute": "host",
"Value": "test.com"
}
Failure while updating port of /configuration/system.net/mailSettings/smtp/network[#defaultCredentials='true']: 25
Can you please help me on this.

SMTP Pickup directory not working

I'm trying to save my email in a local directory.
This is my code present in the web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<network host="localhost" />
<specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
</smtp>
</mailSettings>
</system.net>
I don't get errors but the mail is not present in the directory specified. It is possible that the mail ends up in a (spam)filter?
I suspect the fact that you have both a <network> and a <specifiedPickupDirectory> element in there is the problem. Remove the <network>.
Here's a working example:
web.config
<system.net>
<mailSettings>
<smtp from="HelloWorld#yourdomain.com" deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\dcs\smtp" />
</smtp>
</mailSettings>
</system.net>
Code behind (VB):
Protected Sub startMailing(sender As Object, e As EventArgs) Handles Button2.Click
Dim msg As New System.Net.Mail.MailMessage()
Dim smtpSrvr As New SmtpClient
msg.To.Add("wylie#acme.com")
msg.Subject = "Fish Goes paddling Down the stream."
msg.Body = "Row Row Row Row"
smtpSrvr.Send(msg)
End Sub

Email cron job (asp.net) with Quartz.Net not working

I am trying to send peroidic emails with Quartz.net but with no success. Firstly, the email code portion below is verified to work as I opened a new project to test it out. When I opened the application.log, I can see the Log.DebugFormat messages and not exceptions was catched, however no email was being sent out. Any advice? Thanks.
public void Execute(IJobExecutionContext context)
{
try
{
Log.DebugFormat("{0}****{0}Job {1} fired # {2} next scheduled for {3}{0}***{0} Fired by {4}",
Environment.NewLine,
context.JobDetail.Key,
context.FireTimeUtc.Value.ToString("r"),
context.NextFireTimeUtc.Value.ToString("r"),
"James");
Log.DebugFormat("{0}***{0}Hello World!!!{0}***{0}", Environment.NewLine);
// Try send email
var mail = new Email();
mail.IsBodyHtml = true;
mail.MailAddresses = "ong1980#hotmail.com";
mail.MailSubject = "Test Cron Job";
var mailMessage = mail.CreateMailMessage();
var Th = new Thread(() => Email.Send(mailMessage, 6, 3000, true));
Th.Start();
}
catch (Exception ex)
{
Log.DebugFormat("{0}***{0}Failed: {1}{0}***{0}", Environment.NewLine, ex.Message);
}
}
App.Config:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler,
System, Version=1.0.5000.0,Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>
<quartz>
<add key="quartz.scheduler.instanceName" value="ServerScheduler"/>
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
<add key="quartz.threadPool.threadCount" value="10"/>
<add key="quartz.threadPool.threadPriority" value="2"/>
<add key="quartz.jobStore.misfireThreshold" value="60000"/>
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
</quartz>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="xxx#hotmail.com">
<network host="smtp.live.com" port="25" userName="xxx#hotmail.com" password="xxxxxxxxx" />
</smtp>
</mailSettings>
</system.net>
</configuration>
Ok happen that inside the /Quartz.Net/Quartz.Server.exe.config which should reside in the program files(x86), I miss out the connectionstring and mailsettings. Hope it can be a help for someone else.

How to use sections in an app.config that's in a web.config?

My web.config looks like this:
<configuration>
<configSections>
<appSettings configSource="Exampleapp.config" />
<connectionStrings configSource="ExampleProd.config" />
</configSections>
<configuration>
My app.config (Exampleapp.config) looks like this:
<configSections>
<sectionGroup name="exampleSettings">
<section name="blah" type="System.Configuration.NameValueSectionHandler" />
<section name="foo" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<exampleSettings>
<blah>
<add key="me" value="1" />
</blah>
<foo>
<add key="you" value="2" />
</foo>
</exampleSettings>
I'm getting an error that says: XML document cannot contain multiple root level elements. How do I resolve this?
As error clearly specifies,
You only need to have one root element in XML file.
Put you two parent elements in one parent element like,
<rootEle>
<configSections>
<sectionGroup name="exampleSettings">
<section name="blah" type="System.Configuration.NameValueSectionHandler" />
<section name="foo" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<exampleSettings>
<blah>
<add key="me" value="1" />
</blah>
<foo>
<add key="you" value="2" />
</foo>
</exampleSettings>
</rootEle>
It should work.
According to this post, you get that error because the XML document must only have one root element and you currently have two. Try something like:
<?xml version="1.0" encoding="utf-8"?>
<config>
*Your code goes here*
</config>

How to encrypt one entry in web.config

ASP.NET 4
I've used RSA key encryption for connection strings in web.config on my web farm. However, there's one more custom password entry that I'd like to encrypt. How should I encrypt it with RSA key without having the rest configurations being encrypted. Please advise, thanks.
Example:
<appSettings>
...
<add key="Host" value="www.foo.com" />
<add key="Token" value="qwerqwre" />
<add key="AccountId" value="123" />
<add key="DepartmentId" value="456" />
<add key="Password" value="asdfasdf" />
<add key="SessionEmail" value="foo#foo.com" />
<add key="DefaultFolder" value="789" />
</appSettings>
You could put the password into a separate section and encrypt this section only. For example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<appSettings>
<add key="Host" value="www.foo.com" />
<add key="Token" value="qwerqwre" />
<add key="AccountId" value="123" />
<add key="DepartmentId" value="456" />
<add key="SessionEmail" value="foo#foo.com" />
<add key="DefaultFolder" value="789" />
</appSettings>
<secureAppSettings>
<add key="Password" value="asdfasdf" />
</secureAppSettings>
</configuration>
and then (note that I am using DPAPI in my example so adapt the provider for RSA):
aspnet_regiis -pef secureAppSettings . -prov DataProtectionConfigurationProvider
Once encrypted the file will look like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<appSettings>
<add key="Host" value="www.foo.com" />
<add key="Token" value="qwerqwre" />
<add key="AccountId" value="123" />
<add key="DepartmentId" value="456" />
<add key="SessionEmail" value="foo#foo.com" />
<add key="DefaultFolder" value="789" />
</appSettings>
<secureAppSettings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd.......</CipherValue>
</CipherData>
</EncryptedData>
</secureAppSettings>
</configuration>
The way you would access those settings in your application once the file is encrypted is still the same and completely transparent:
var host = ConfigurationManager.AppSettings["Host"];
var password = ConfigurationManager.AppSettings["Password"];
In c# and .Net 4.5 I had to use this to read the encrypted setting:
string password = ((System.Collections.Specialized.NameValueCollection)ConfigurationManager.GetSection("secureAppSettings"))["Password"];
but otherwise works a treat.
You can't encrypt a single entry - the infrastructure only allows for encryption of whole config sections.
One option is to place the entry in its own config section and encrypt that.

Resources