Email Configuration Settings not being fetched - c#

I have application that fetches email configuration settings such as host (SMTP Server name), username (SMTP Username) and Password from App.Config File as shown below
<system.net>
<mailSettings>
<smtp from="name#example.com"><network host="smtp.gmail.com" userName="test123#gmail.com" port="25" password="PassworD"/>
</smtp>
</mailSettings>
</system.net>
Now i wish to configure the settings that i have set in database and NOT from App.Config File. From database the credentials are not available in SMTPClient's properties .FYI, they are saved in database as well as the values are also correct.
Please Help!
Thanks

When you instantiate the SMTPClient just set the values specifically there. This overrides the settings from your app.config/web.config file.
var msg = new System.Net.Mail.MailMessage("from#yoursite.com", "to#somesite.com", "Subject", "Body text...");
var c = new System.Net.Mail.SmtpClient("smtp.gmail.com", 25);
c.Credentials = new System.Net.NetworkCredential("test123#gmail.com", "PassworD");
c.Send(msg);

when you were using the web.config version the settings were automatically filled in for you. Now as you have saved it in DB, you need to specify the settings.

Related

"The SMTP host was not specified." - but it is specified?

I'm slightly baffled here - I'm receiving the following error:
The SMTP host was not specified.
Even though my code appears to be correct (from what I can see).
I am able to do it manually by including all the details inside of the controller, e.g.
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
smtpClient.Port = 587;
... etc
But I shouldn't have to do this, as I want to use the details inside mailSettings (Making it re-usable for various different controllers).
mailSettings in my Web.Config file:
<system.net>
<mailSettings>
<smtp from="example#gmail.com" deliveryMethod="Network" >
<network host="smtp.gmail.com" defaultCredentials="true"
port="587" enableSsl="true" userName="example#gmail.com"
password="example"/>
</smtp>
</mailSettings>
</system.net>
My Controller action:
[HttpPost]
public ActionResult SubmitFeature(FormData formData)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress("example#gmail.com"));
mail.Body = "Test";
smtpClient.Send(mail);
return View("Example");
}
Is there anything I'm missing which may be causing this? I haven't messed around with any other settings in Web.Config, they are as is when setting up a new MVC5 project.
In a clean MVC project, I am unable to replicate your issue. Following the ScottGu blog post here, I was able to get a gmail sent email without issue (VS 2013, .NET 4.5.1, MVC 5). Note the the <system.net> element is a top level element and not nested inside of AppSettings or <system.web>.
Important
There are a few web.config files in your solution, ensure that the mailSettings is inserted into the root level web.config (and not the one located in the Views folder)
Web.Config
<configuration>
<system.net>
<mailSettings>
<smtp from="myEmail#gmail.com">
<network host="smtp.gmail.com"
port="587"
enableSsl="true"
userName="myEmail#gmail.com"
password="SuperSecretPwd"
defaultCredentials="false" /> <!--This must be false on Gmail-->
</smtp>
</mailSettings>
</system.net>
</configuration>
Controller
var smtpClient = new SmtpClient();
var msg = new MailMessage();
msg.To.Add("MyOtherAddress#yahoo.com");
msg.Subject = "Test";
msg.Body = "This is just a test email";
smtpClient.Send(msg);
It is unclear if some of the extra attributes you have included are causing issues (thought they shouldn't) such as delivery method. Also, is there a setting for allowing SMTP access or is that just for IMAP/POP delivery?
If you can test and are successful in a clean project, then this would point to either a web.config transformation problem or some other setting(s) in your project overriding the web.config settings that you have in place.
The solution was mentioned in the Chat, but never edited into the answer above.
Make sure that you make these settings in the web.config of the Root Level and not in the Views folder.
#Tommy: ...this looks like the web.config from your Views folder and not the web.config at the root of the application
If your code is right so,Turn on "Less secure app access" in your Gamil email account

Modify SmtpSection in web.config in c# code in asp.net?

I have set up the following in my web.config to have multiple smtp settings defined:
<sectionGroup name="mailSettings">
<section name="smtp_1" type="System.Net.Configuration.SmtpSection" requirePermission="false"/>
<section name="smtp_2" type="System.Net.Configuration.SmtpSection" requirePermission="false"/>
</sectionGroup>
</configSections>
<mailSettings>
<smtp_1 deliveryMethod="Network" from="xxx#hotmail.com">
<network host="smtp.live.com" port="587" userName="xxx#hotmail.com" password="xxx" defaultCredentials="false"/>
</smtp_1>
<smtp_2 deliveryMethod="Network" from="yyy#gmail.com">
<network host="smtp.gmail.com" port="465" userName="yyy#gmail.com" password="yyy" defaultCredentials="false"/>
</smtp_2>
</mailSettings>
I have been able to retrieve any of the settings using code and I have set up a function to iterate through the config file and pull out each smtp setting and add to a dropdownlist.
The only thing that doesn't want to work is actually writing back to the web.config (either adding a new smtp or modifying an existing one). I have used the following C# code:
Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~");
SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_1");
smtpSection.From = "zzz#hotmail.com";
smtpSection.Network.Port = 25;
smtpSection.Network.UserName = "xxx";
smtpSection.Network.Password = "xxx";
webConfig.Save();
But this returns the error:
The configuration is read only.
This code works perfectly fine, but of course it modifies the primary smtp settings in the system.net (where you cannot have multiple smtp settings).
System.Net.Configuration.MailSettingsSectionGroup mailSection = webConfig.GetSectionGroup("system.net/mailSettings") as System.Net.Configuration.MailSettingsSectionGroup;
mailSection.Smtp.From = "xxx#hotmail.com";
mailSection.Smtp.Network.Host = "smtp.live.com";
mailSection.Smtp.Network.Port = 25;
webConfig.Save();
Been racking my brain over this and can't seem to find the answer. Feels like it's going to be something so simple (or just not possible). If not, will probably need to encrypt settings and store in the DB.
Thanks! :)

choosing which domain account to use for sending email

I have code for sending email to clients.
System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient();
MailMessage Message = new MailMessage("From", "To", "Subject", "Body");
Client.Send(Message);
With following in App.config.
<system.net>
<mailSettings>
<smtp from="support#MyDomain1.com">
<network host="smtp.MyDomain1.com" port="111" userName="abc" password="helloPassword1" />
</smtp>
</mailSettings>
<mailSettings>
<smtp from="support#MyDomain2.com">
<network host="smtp.MyDomain2.com" port="222" userName="xyz" password="helloPassword2" />
</smtp>
</mailSettings>
</system.net>
Problem is that in my code how can I differentiate which mailsetting to use for a given mail, meaning when I want to send mail from MyDomain1 account vs MyDomain2 account
There's one thread that contains the explanation how to achieve this.
Setting multiple SMTP settings in web.config?
Wanted originally to post it as an answer, but the system automatically converted it into a comment. Seems I have to write longer messages.
Glad it helped :)

ConfigurationManager.AppSettings["SMTP"]; null exception

i took this code to send an email. But i dont understand what that configurationManager does, and why it gives me the exception. Here is the full code:
MailMessage mail = new MailMessage();
mail.To.Add("makovetskiyd#yahoo.co.uk");
mail.From = new MailAddress("makovetskiyd#yahoo.co.uk");
mail.Subject = "Test Email";
string Body = "Welcome to CodeDigest.Com!!";
mail.Body = Body;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Send(mail);
i also changed the last lines to this:
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.Send(mail);
but it would still show a mistake, saying it didnt find IIS server..or something like that
You should not use AppSettings an the ConfigurationManager for SMTP configuration. The preferred way is to configure SMTP through the <mailSettings> section in web.config. For example, the configuration for a small website could look like this:
<system.net>
<mailSettings>
<smtp from="info#example.com">
<network host="localhost" port="25" defaultCredentials="false">
</smtp>
</mailSettings>
</system.net>
This will allow you to new up an SmtpClient and just send the message without further configuration.
ConfigurationManager provides access to configuration files for client applications.
I guess the reason of the error is that the config file of your application does not have a SMTP key in the application settings section.
<appSettings>
<add key="SMTP" value="..." />
</appSettings>
The reason is that there is no such SMTP configuration in the config file. I think you'd better check your web.config. And to make your application stronger, you need to add a default host in case of the config file is incorrect.
string defaultHost = "www.foo.com";
smtp.Host = ConfigurationManager.AppSettings["SMTP"] ?? defaultHost;

How can I save an email instead of sending when using SmtpClient?

I am using SmtpClient to send an email with an attachment.
However for a certain batch we need to somehow save the MailMessage instead of sending them.
We are then thinking/hoping to manually upload the messages to the users drafts folder.
Is it possible to save these messages with the attachment intact (impossible, I would have thought). Or alternatively upload the messages to a folder in the users account?
If anyone has any experience of this, I'd much appreciate a bit of help or a pointer.
When testing in ASP.NET we save our emails to a folder rather then send them through an email server. Maybe you could change yourweb.config settings like this for your batch?
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
</smtp>
</mailSettings>
</system.net>
Additional Info:
MSDN: <specifiedPickupDirectory> Element (Network Settings)
Configuring SmtpClient to drop emails in a folder on disk
As well as the SpecifiedPickupDirectory information of the other answers, if you want to ensure your emails are sent to a folder relative to the site root - handy in testing on build servers where you don't know the paths - you can add a quick check in your email sending code:
SmtpClient client = new SmtpClient();
...
// Add "~" support for pickupdirectories.
if (client.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory && client.PickupDirectoryLocation.StartsWith("~"))
{
string root = AppDomain.CurrentDomain.BaseDirectory;
string pickupRoot = client.PickupDirectoryLocation.Replace("~/", root);
pickupRoot = pickupRoot.Replace("/",#"\");
client.PickupDirectoryLocation = pickupRoot;
}
And your tests will look something like this (make sure you use App_Data so IIS can write to the folder):
// Arrange - get SitePath from AppDomain.Current.BaseDirectory + ..\
string pickupPath = Path.Combine(SitePath, "App_Data", "TempSmtp");
if (!Directory.Exists(pickupPath))
Directory.CreateDirectory(pickupPath);
foreach (string file in Directory.GetFiles(pickupPath, "*.eml"))
{
File.Delete(file);
}
// Act (send some emails)
// Assert
Assert.That(Directory.GetFiles(pickupPath, "*.eml").Count(), Is.EqualTo(1));
This can help - Adding Save() functionality to Microsoft.Net.Mail.MailMessage
The main ideia, make an extension to MailMessage ,that by reflection making a save method.
You can configure this with the system.net setting in your web.config / app.config file.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="mail.mydomain.com" port="25" />
</smtp>
<!-- Use this setting for development
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Temp" />
</smtp>
-->
</mailSettings>
</system.net>
Also, here's a link with info on migrating from System.Web.Mail to System.Net.Mail.
A bug also requires adding as a workaround in some versions of the framework. So the completed version looks like:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
<network host="localhost" />
</smtp>
</mailSettings>
</system.net>

Categories

Resources