Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(relay): Fix NRE in relay (Resolves #1438) #1440

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 15 additions & 30 deletions Rnwood.Smtp4dev/ClientApp/src/components/settingsdialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
</el-form-item>

<el-form-item label="Auth Types when not secure connection" prop="server.smtpEnabledAuthTypesWhenNotSecureConnection">

<el-select v-model="server.smtpEnabledAuthTypesWhenNotSecureConnection"
multiple
style="width: 100%" :disabled="server.lockedSettings.smtpEnabledAuthTypesWhenNotSecureConnection">
Expand All @@ -107,7 +107,7 @@
</el-form-item>

<el-form-item label="Auth Types when secure connection" prop="server.smtpEnabledAuthTypesWhenSecureConnection">

<el-select v-model="server.smtpEnabledAuthTypesWhenSecureConnection"
multiple
style="width: 100%" :disabled="server.lockedSettings.smtpEnabledAuthTypesWhenSecureConnection">
Expand Down Expand Up @@ -244,20 +244,20 @@

<el-form-item label="Auto-Relay Recipients" v-show="isRelayEnabled" prop="isRelayEnabled">
<el-icon v-if="server.lockedSettings.relayAutomaticEmails" :title="`Locked: ${server.lockedSettings.relayAutomaticEmails}`"><Lock /></el-icon>

<div v-for="(email, index) in server.relayAutomaticEmails" :key="index">
<el-form-item :prop="'relayAutomaticEmails[' + index + '].value'" :rules="{required: true, message: 'Required'}">
<el-input v-model="server.relayAutomaticEmails[index]">
<template #append>
<el-button @click="server.relayAutomaticEmails.splice(index, 1)" :disabled="server.lockedSettings.relayAutomaticEmails">
Remove
</el-button>
</template>
</el-input>
</el-form-item>
</div>
<el-button size="small" @click="server.relayAutomaticEmails.push('')" :disabled="server.lockedSettings.relayAutomaticEmails">New Auto-Relay Recipient</el-button>
</el-form-item>
<div v-for="(email, index) in server.relayAutomaticEmails" :key="index">
<el-form-item :prop="'server.relayAutomaticEmails[' + index + ']'" :rules="{required: true, message: 'Required'}">
<el-input v-model="server.relayAutomaticEmails[index]">
<template #append>
<el-button @click="server.relayAutomaticEmails.splice(index, 1)" :disabled="server.lockedSettings.relayAutomaticEmails">
Remove
</el-button>
</template>
</el-input>
</el-form-item>
</div>
<el-button size="small" @click="server.relayAutomaticEmails.push('')" :disabled="server.lockedSettings.relayAutomaticEmails">New Auto-Relay Recipient</el-button>

</el-tab-pane>
<el-tab-pane label="Users">

Expand Down Expand Up @@ -395,21 +395,6 @@
}
}

relayAutomaticEmails: any[] = [];

@Watch("server.relayAutomaticEmails")
updateAutomaticEmailsForValidation() {

if (!this.server) {
return;
}

this.relayAutomaticEmails = this.server.relayAutomaticEmails.map((v, i) => {
return { value: v };
});

}

async save() {
this.saving = true;
this.error = null;
Expand Down
14 changes: 7 additions & 7 deletions Rnwood.Smtp4dev/Server/ScriptingHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public IReadOnlyCollection<string> GetAutoRelayRecipients(ApiModel.Message messa
{
JsValue result = jsEngine.Evaluate(shouldRelayScript);

List<string> recpients = new List<string>();
List<string> relayRecipients = new List<string>();
if (result.IsNull())
{

Expand All @@ -105,22 +105,22 @@ public IReadOnlyCollection<string> GetAutoRelayRecipients(ApiModel.Message messa
{
if (result.AsString() != String.Empty)
{
recpients.Add(result.AsString());
relayRecipients.Add(result.AsString());
}
}
else if (result.IsArray())
{
recpients.AddRange(result.AsArray().Select(v => v.AsString()));
relayRecipients.AddRange(result.AsArray().Select(v => v.AsString()));
}
else if (result.AsBoolean())
{
recpients.Add(recipient);
relayRecipients.Add(recipient);
}

log.Information("AutomaticRelayExpression: (message: {message.Id}, recipient: {recipient}, session: {session.Id}) => {result} => {recipients}", message.Id, recipient,
session.Id, result, recpients);
log.Information("AutomaticRelayExpression: (message: {messageId}, recipient: {recipient}, session: {sessionId}) => {result} => {relayRecipients}", message.Id, recipient,
session.Id, result, relayRecipients);

return recpients;
return relayRecipients;

}
catch (JavaScriptException ex)
Expand Down
5 changes: 5 additions & 0 deletions Rnwood.Smtp4dev/Server/Smtp4devServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ public RelayResult TryRelayMessage(Message message, MailboxAddress[] overrideRec
log.Information("Relaying message to {recipient}", recipient);

using SmtpClient relaySmtpClient = relaySmtpClientFactory(relayOptions.CurrentValue);

if (relaySmtpClient == null)
{
throw new ApplicationException("Relay server options are incomplete.");
}
var apiMsg = new ApiModel.Message(message);
MimeMessage newEmail = apiMsg.MimeMessage;
MailboxAddress sender = MailboxAddress.Parse(
Expand Down
2 changes: 1 addition & 1 deletion Rnwood.Smtp4dev/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddSingleton<Func<RelayOptions, SmtpClient>>(relayOptions =>
{
if (!string.IsNullOrEmpty( relayOptions.SmtpServer))
if (string.IsNullOrEmpty( relayOptions.SmtpServer))
{
return null;
}
Expand Down