From 43bf2bacb4f505fb61efb3a6ee691c621b36af02 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Sun, 13 Oct 2024 09:48:28 +0800 Subject: [PATCH] Enhance environment variable retrieval and defaults - Modify `Helper.cs` to retrieve `EmailAccountPassword` from process-level environment variables and set a default SMTP port of `25`. - Update `EnvHelper.cs` `Get` method to support a `defaultValue` parameter, returning it if the environment variable is not found. --- src/Moonglade.Function.Email/Core/Helper.cs | 4 ++-- src/Moonglade.Function.Email/EnvHelper.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Moonglade.Function.Email/Core/Helper.cs b/src/Moonglade.Function.Email/Core/Helper.cs index 53b2200..8c4bf40 100644 --- a/src/Moonglade.Function.Email/Core/Helper.cs +++ b/src/Moonglade.Function.Email/Core/Helper.cs @@ -12,8 +12,8 @@ public static EmailHelper GetEmailHelper(string functionAppDirectory) var smtpSettings = new SmtpSettings(EnvHelper.Get("SmtpServer"), EnvHelper.Get("SmtpUserName"), - EnvHelper.Get("EmailAccountPassword", EnvironmentVariableTarget.Process), - EnvHelper.Get("SmtpServerPort")); + EnvHelper.Get("EmailAccountPassword", target: EnvironmentVariableTarget.Process), + EnvHelper.Get("SmtpServerPort", 25)); if (EnvHelper.Get("EnableTls")) { diff --git a/src/Moonglade.Function.Email/EnvHelper.cs b/src/Moonglade.Function.Email/EnvHelper.cs index 859800a..658d91f 100644 --- a/src/Moonglade.Function.Email/EnvHelper.cs +++ b/src/Moonglade.Function.Email/EnvHelper.cs @@ -7,7 +7,7 @@ public static class EnvHelper { public static ICollection AllKeys => Environment.GetEnvironmentVariables().Keys; - public static T Get(string name, EnvironmentVariableTarget? target = null) + public static T Get(string name, T defaultValue = default, EnvironmentVariableTarget? target = null) { if (string.IsNullOrEmpty(name)) { @@ -17,7 +17,7 @@ public static T Get(string name, EnvironmentVariableTarget? target = null) string matchedKey = AllKeys.Cast().FirstOrDefault(key => string.Equals(key, name, StringComparison.OrdinalIgnoreCase)); if (matchedKey == null) { - return default; + return defaultValue; } var value = target == null