I am try to move my email settings to web config, but i don't know how to call the setting from web config.
This is my newpassword web-config setting:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from=""testo" <admin#test.com>" >
<network host="mail.test.com" userName="admin#test.com" password="waiff75E-" port="25"/>
</smtp>
</mailSettings>
</system.net>
And this is my previous code
const string username = "test#smartguroo.com";
const string password = "password";
SmtpClient smtpclient = new SmtpClient();
MailMessage mail = new MailMessage();
MailAddress fromaddress = new MailAddress("admin#test.com", loggedinUser.Text + "test");
smtpclient.Host = "mail.test.com";
smtpclient.Port = 25;
mail.From = fromaddress;
mail.To.Add(userEmail.Text);
mail.Subject = ("New post on your wall from " + loggedinUser.Text + " ");
// mail.Attachments.Add(new mail);
mail.IsBodyHtml = true;
mail.Body = "";
Remove the following lines lines since you want your settings in the web.config file to drive it from the configuration point of view.
smtpclient.EnableSsl = false;
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
smtpclient.Send(mail);
And just call the Send method on the SmtpClient
smtpclient.Send(mail);
All the previous concerns are configured into your web.config file, as you have done so. (Copied verbatim)
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from=""testo" <admin#test.com>" >
<network host="mail.test.com" userName="admin#test.com" password="password" port="25"/>
</smtp>
</mailSettings>
</system.net>
In your webconfig
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<!-- Markup removed for clarity. -->
<add key="mailAccount" value="xyz" />
<add key="mailPassword" value="password" />
</appSettings>
<system.web>
Reference in c# via
var credentials = new NetworkCredential(
ConfigurationManager.AppSettings["mailAccount"],
ConfigurationManager.AppSettings["mailPassword"]
);
This was using this identity tutorial
Related
I was building a project that sends e-mails from my website to an account using SMTP with the Asp.NET Framework with MVC, but, when I try to send the e-mail it catches the next exception
The SMTP server requires a secure connection, or the client did not authenticate. The server response was: 5.7.0 Authentication Required
This is the code inside the try
MailMessage message = new MailMessage();
message.From = new MailAddress("myemailfortesting#gmail.com");
message.To.Add(new MailAddress(contact.Email));
message.Subject = "Test";
message.IsBodyHtml = true;
message.Body = "Name:" + contact.Name+ "Message:" + contact.Text;
using (SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.Credentials = new NetworkCredential("myemailfortesting#gmail.com", "p455w0rD");
smtpClient.EnableSsl = true;
smtpClient.Send(message);
}
The connection string in Views/Share/Web.config
<system.net>
<mailSettings>
<smtp from="myemailfortesting#gmail.com">
<network host="smtp.gmail.com" port="587" userName="myemailfortesting#gmail.com" password="p455w0rD" defaultCredentials="true" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
I've already turn on the Less secure apps in my destination Google account.
What could be wrong here?
I am trying to setup SMTP relay to send mails from the web application without username and password. I read that you can setup locally on your IIS and use "No Authentication"
In web.config, these are my settings:
<appSettings>
<add key="SmtpServerAddress" value="localhost" />
<add key="SmtpServerPort" value="25" />
<add key="SmtpServerTimeout" value="30" />
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="">
<network host="localhost" port="25" />
</smtp>
</mailSettings>
And my code-behind to send email is :
SmtpClient sc = new SmtpClient();
sc.Host = "localhost";
sc.Port = 25;
sc.UseDefaultCredentials = true;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
sc.Send(mm);
}
catch (Exception ex)
{
throw ex;
}
When I submit the click event, to send mail, I am getting "remote server not found".
Could you shed some light on this ?
You cannot add localhost and start sending emails (unless there actually IS an SMPT server running). You still need a "real" SMTP server. Those settings a nothing more then adding a default SMTP server in the Web.Config.
If you don't set those settings in IIS, you send mail like this. There are may examples of this on SO.
using (SmtpClient client = new SmtpClient())
{
client.Host = "mail.fakedomain.nl";
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("fake#fakedomain.nl", "abcd123");
//send mail
}
But if you set the default settings in IIS, you can do this
using (SmtpClient client = new SmtpClient())
{
//send mail
}
It saves a few lines of code.
Are you trying this on your development computer? If so you could install hMailServer
Currently my WPF application has the SMTP set up as followed:
string MailTo = txtBoxEmail.Text;
string MailFrom = "datfakeemaildoe#gmail.com ";
string Subject = "Cool Subject";
string Password = "*******";
SmtpClient smtpmail = new SmtpClient();
smtpmail.Host = "smtp.gmail.com";
smtpmail.Port = 587;
smtpmail.EnableSsl = true;
smtpmail.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpmail.UseDefaultCredentials = false;
smtpmail.Credentials = new NetworkCredential(MailFrom, Password);
MailMessage message = new MailMessage(MailFrom, MailTo, Subject, MessageTosend);
message.IsBodyHtml = true;
However, I'd like to manage all of this in the config rather than the .cs if possible.
Currently, I set up my App.Config as followed:
<configuration>
<appSettings>
<add key="host" value="smtp.gmail.com" />
<add key="port" value="587" />
<add key="MailFrom" value="datfakeemaildoe#gmail.com" />
<add key="Password" value="*******" />
</appSettings>
</configuration>
Problem is, how to I implement it properly now in the .cs
I tried this:
using System.Configuration;
using System.Net.Configuration;
using System.Net;
using System.Net.Mail;
...
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
...
string MessageTosend = #"there is html in here, trust me";
MailAddress Sender = new MailAddress(ConfigurationManager.AppSettings["MailFrom"]);
string MailTo = txtBoxEmail.Text;
string Subject = "Cool Subject";
SmtpClient smtp = new SmtpClient()
{
host = ConfigurationManager.AppSettings["host"],
port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network;
UseDefaultCredentials = false;
Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailFrom"], ConfigurationManager.AppSettings["Password"])
};
MailMessage message = new MailMessage(ConfigurationManager.AppSettings["MailFrom, MailTo, Subject, MessageTosend);
message.IsBodyHtml = true;
...
BUT ConfigurationManager is generating an error that says does not exist in the current context, even though I have...
using System.Configuration;
using System.Net.Configuration;
using System.Net;
using System.Net.Mail;
...already. I just want to be able to change SMTP settings in the config rather than having to update the code behind for my application in case things change later.
Not sure if I'm doing it wrong or if I'm just missing something entirely. I referenced this to know how to use the app.config.
As stated on Scott Gu's blog, there's already a section in .config specifically for these settings, no need to do it in App Settings. The .NET framework automatically reads these settings in without the need to write C# to do it. Here's the MSDN documentation.
Example:
<system.net>
<mailSettings>
<smtp from="test#foo.com">
<network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
No need to specify any code for loading of the configuration.
Figured out the ConfigurationManager problem.
While <system.net><mailSettings> ... works, so does my original <appSettings>. The reason why ConfigurationManager was running into a issue was because System.Configuration.dll was missing in my references (source)
Adding this, I was able to call the keys I added in my App.config
Then I changed the way it was used in the .cs
SmtpClient smtpmail = new SmtpClient();
smtpmail.Host = ConfigurationManager.AppSettings["host"];
smtpmail.Port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
smtpmail.EnableSsl = true;
smtpmail.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpmail.UseDefaultCredentials = false;
smtpmail.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailFrom"], ConfigurationManager.AppSettings["Password"]);
MailMessage message = new MailMessage(ConfigurationManager.AppSettings["MailFrom"], MailTo, Subject, MessageTosend);
message.IsBodyHtml = true;
Worked like a charm. :)
This is my web.config mail settings:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="smthg#smthg.net">
<network defaultCredentials="true" host="localhost" port="587" userName="smthg#smthg.net" password="123456"/>
</smtp>
</mailSettings>
</system.net>
and here's how I try to read the values from web.config
var smtp = new System.Net.Mail.SmtpClient();
var credential = new System.Net.Configuration.SmtpSection().Network;
string strHost = smtp.Host;
int port = smtp.Port;
string strUserName = credential.UserName;
string strFromPass = credential.Password;
But credentials are always null. How can i access these values?
Since no answer has been accepted, and none of the others worked for me:
using System.Configuration;
using System.Net.Configuration;
// snip...
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string username = smtpSection.Network.UserName;
It is not necessary to use the ConfigurationManagerand get the values manually. Simply instantiating an SmtpClient is sufficient.
SmtpClient client = new SmtpClient();
This is what MSDN says:
This constructor initializes the Host, Credentials, and Port properties for the new SmtpClient by using the settings in the application or machine configuration files.
Scott Guthrie wrote a small post on that some time ago.
By using the configuration, the following line:
var smtp = new System.Net.Mail.SmtpClient();
Will use the configured values - you don't need to access and assign them again.
As for the null values - you are trying accessing the configuration values incorrectly. You are just creating an empty SmtpSection instead of reading it from configuration.
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("<the section name>");
var credentials == smtpSection.Network;
I think if you have defaultCredentials="true" set you will have the credentials = null as you are not using them.
Does the email Send when you call the .Send method?
So
This is my web config mail settings:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="smthg#smthg.net">
<network defaultCredentials="false" host="localhost" port="587"
userName="smthg#smthg.net" password="123456"/>
</smtp>
</mailSettings>
</system.net>
and this is cs
SmtpClient smtpClient = new SmtpClient();
string smtpDetails =
#"
DeliveryMethod = {0},
Host = {1},
PickupDirectoryLocation = {2},
Port = {3},
TargetName = {4},
UseDefaultCredentials = {5}";
Console.WriteLine(smtpDetails,
smtpClient.DeliveryMethod.ToString(),
smtpClient.Host,
smtpClient.PickupDirectoryLocation == null
? "Not Set"
: smtpClient.PickupDirectoryLocation.ToString(),
smtpClient.Port,
smtpClient.TargetName,
smtpClient.UseDefaultCredentials.ToString)
);
//You can access the network credentials in the following way.
//Read the SmtpClient section from the config file
var smtp = new System.Net.Mail.SmtpClient();
//Cast the newtwork credentials in to the NetworkCredential class and use it .
var credential = (System.Net.NetworkCredential)smtp.Credentials;
string strHost = smtp.Host;
int port = smtp.Port;
string strUserName = credential.UserName;
string strFromPass = credential.Password;
make sure you have reference to System.Net in your application
Set defaultCredentials="false", because when it's set to true, no credentials are used.
Here is the code i wrote to send email,
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
sc.UseDefaultCredentials = false;
try
{
m.From = new MailAddress(Sender);
m.To.Add(new MailAddress(Receiver));
m.Subject = Subject;
m.IsBodyHtml = true;
m.Body = Body;
sc.Send(m);
}
catch (Exception ex) { _Exceptions.ManageExceptions(ex); }
And the config file settings:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="xxx#xxx.com">
<network host="192.168.0.170" userName="setsdom01\user1" password="xxx" port="25" />
</smtp>
</mailSettings>
</system.net>
It executes when i run it from my machine, i try on different PC and it is giving me the following message: ...message rejected as spam by content filtering..
What could be the problem?
I think you need to add NetworkCredential backend code.
var AuthenticationDetails = new NetworkCredential("xxx#", "xxxx");
sc.Credentials = AuthenticationDetails;
This might work
Also look this How to Enable and Configure the Spam Confidence Level Thresholds may be helpful