Email setting to database

This public forum is for user-to-user discussions of ASP.NET Maker. Note that this is not support forum.
Post Reply
Danny
User
Posts: 138

Email setting to database

Post by Danny »

Hi,

We work with several Email setting and several customers.

Now we have to rebuild the complete file for every customer because we have to change the email settings.

Is it possible to puth this setting to the database, just like example Dynamic User Levels (migrate to database).

Regards,


Webmaster
User
Posts: 9425

Post by Webmaster »

Just add a table in your database to store them. Problem is: How and when do you want to use the email settings in the database? What are those "email settings"? Different SMTP servers? Senders? Same SMTP server but different usernames and passwords? Explain in detail.


newbie
User
Posts: 2

Post by newbie »

I have the same question. In classic asp, Aspmaker - not Aspmaker.net - it seems the ew.cfg file can be edited and this would be where to edit SMTP server info, but with ASPMaker.net you seem to have to read settings from a file then write them. I thought appsetttings.json would be a good place to save/read from but apparently not.


MichaelG
User
Posts: 1095

Post by MichaelG »

To access the settings in appsettings.json, just use: Configuration["Section:Key"]

assuming you have the following settings in appsettings.json:

{
"Databases": ...,
"Section": {
"Key": "...",
...
},
"Jwt": {
...
},


right4
User
Posts: 20

Post by right4 »

I'm successfully store smtp parameter to appsettings.json but the problems is when deploy to IIS i need to put <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> in web.config,
ewcfg.cs
public static string SmtpServer = Configuration["SMTPConfig:SmtpServer"];

appsettings.json

"SMTPConfig": {
"SmtpServer": "smtp.gmail.com",

error
Application startup exception: System.TypeInitializationException: The type initializer for 'Config'
threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of
an object.


MichaelG
User
Posts: 1095

Post by MichaelG »

Check Tools -> Advanced Settings -> Set environment to 'Development' in web.config


Webmaster
User
Posts: 9425

Post by Webmaster »

You'd better use Class_Init server event (see Server Events and Client Scripts in the help file) to set SMTP settings, e.g. (v2019)

Config.SmtpServer = "xxx.yyy.zzz"; // SMTP server
Config.SmtpServerPort = 1234; // SMTP server port
Config.SmtpSecureOption = "SSL";
Config.SmtpServerUsername = "user"; // SMTP server user name
Config.SmtpServerPassword = "password"; // SMTP server password


Markom
User
Posts: 20

Post by Markom »

I'm using an smtp server with a self signed certificate. Obviously Mailkit cannot handle that with the settings in Aspnetmaker2020. I followed your suggestion to adapt the class_init server event, leaving the SSL line out, but that made the application unable to start. Any suggestion how and where I can follow op in the Mailkit suggestion on Github to adapt / add MyServerCertificateValidationCallback (see below) ?

bool MyServerCertificateValidationCallback (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;

// Note: The following code casts to an X509Certificate2 because it's easier to get the
// values for comparison, but it's possible to get them from an X509Certificate as well.
if (certificate is X509Certificate2 certificate2) {
var cn = certificate2.GetNameInfo (X509NameType.SimpleName, false);
var fingerprint = certificate2.Thumbprint;
var serial = certificate2.SerialNumber;
var issuer = certificate2.Issuer;

return cn == "imap.gmail.com" && issuer == "CN=GTS CA 1O1, O=Google Trust Services, C=US" &&
serial == "0096768414983DDE9C0800000000320A68" &&
fingerprint == "A53BA86C137D828618540738014F7C3D52F699C7";
}

return false;
}

MichaelG
User
Posts: 1095

Post by MichaelG »

Try in Email_Sending, create the Mailer first so that you can add customization your needed:

email.Mailer = new SmtpClient();
//... Customize

Post Reply