What is the purpose of specifying from attribute in smtp element? - c#

When sending mail using system.net. Having specified from attribute in system.net->mailsetting->smtp. In send method we have to pass from address once again, so what is the purpose of specifying it in the config?
<smtp deliveryMethod="Network" **from="xyz#xyz.com"**>
<network host="smtpout.secureserver.net" port="80" userName="xyz#xyz.com" password="xyz-password" />
</smtp>

If the from address never changes, this is a good place to set it.
Many applications send email from a single account, so configuring it there instead of hard coding it makes sense.
This value is used in the parameterless constructor of MailMessage.

Related

Web Config Error Causing 500 Internal Server Error

I have this inside my Web.config:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="admin#xxxxxx.com">
<network defaultCredentials="false" host="mail.xxxxxx.com" password="xxxxxxx" port="25" userName="admin#xxxxxx.com" enableSsl="false" />
</smtp>
</mailSettings>
</system.net>
This causes any request to my website becomes
500 Internal Server Error
This makes the hosting server assumes that my Web.config is not valid. Before it was fine, my Web site can run with no problem. I cannot see any syntax error in it?
What is going on here?
Posting from comment thread as answer: You likely have a special character in your password that may require XML encoding.

Verify Service Proxy Useage

Referencing this answer, I'm attempting to establish a proxy connection for a service reference, but am having trouble verifying that it's actually being used.
In my app.config, I have
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy
usesystemdefault="False"
proxyaddress="http://proxyaddresst.com:80"
bypassonlocal="False"/>
</defaultProxy>
</system.net>
but would like to be able to list the address of the proxy being used in the program itself, through
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " Proxy: " + ???);
Is there a way to do this? It seems simple, but so did setting up the proxy in the first place, and the program isn't working as expected. I'd like to either verify the proxy is in use (and discount it as necessary) or verify that it isn't (and then look for another way to integrate it).

C# ASP.NET MVC Access SMTP settings from config file

I seem to be stuck. I have an asp.net mvc 4 app and I want to send email from a service class. The service class is in a C# library project separate from the mvc project in the solution. I want to set the email configuration in a separate file that is called from a config file. Should the following go in the web.config in the mvc project or create an app.config file in the service project?
<system.net>
<mailSettings>
<smtp configSource="MailSettings.config" />
</mailSettings>
</system.net>
Then I have this in a separate file called MailSettings.config:
<smtp from="abc#email.com">
<network host="smtp.server.com" port="587" userName="abc#email.com" password="password" enableSsl="true" />
</smtp>
I have tried creating an app.config file with just the system.net stuff for the service project, but the mail settings are always null when I try:
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)ConfigurationManager.GetSection("system.net/mailSettings");
SmtpClient SmtpServer = new SmtpClient(settings.Smtp.Network.Host); //get null exception for settings.Smtp.Network.Host)
Also, I've tried including the mail settings in the app.config file to rule out having the MailSettings.config file being the issue and that still generates a null pointer.
I tried an example for accessing it from the web.config file like so:
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
In the service class though the WebConfigurationManager is out of context and so is the Request.ApplicationPath. So if I do have to get it from the web.config file, would I have to pass the http request object to the service class? That seems like a bad idea.
Just use:
SmtpClient SmtpServer = new SmtpClient();
This constructor will pick the configuration directly.
As the MSDN documentation for the parameterless constructor of SmtpClient 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. For more information, see <mailSettings> Element (Network Settings).

ASP.net SMTP Mail though Proxy

How to setup SMTPClient in ASP.net with C# to send email with provided proxy address ? or sending by detecting the system default proxy
I used the the following code in web.config but didnt work
<system.net>
<defaultProxy enabled="true">
<proxy bypassonlocal="False" proxyaddress="http://192.168.101.3:8080/" />
</defaultProxy>
</system.net>
You can't send SMTP email through an HTTP proxy server.
Speak to the person administering your Internet gateway/firewall to ask what host they want you to direct outbound email to.
Based on this article, you simply have to declare your proxy configuration into your Web.config and try to change the port:
If you are still having problems them try changing port number to 587
Edited my answer after your edit.

Sending mail with ASP.NET

I use the standard web.config 'mailSettings' as the backbone for my ASP.NET emails. However, I find it to being increasingly limited in capability. For example, I want to send emails from many different user accounts (support, sales, management etc.) and it doesn't seem like mailSettings supports this. Is there any way to avoid using mailSettings so that I can use multiple accounts? Any disadvantages (and if not, then why would anyone use mailSettings?)
<mailSettings>
<smtp from="support#company.com">
<network host="smtp.gmail.com" port="587" userName="support#company.com" password="abcPassword"/>
</smtp>
</mailSettings>
If you're sending your mail using a System.Net.Mail.MailMessage, you should be able to override the From in the web.config by setting the MailMessage.From to a new MailAddress.

Categories