Skip to content

Commit

Permalink
Fix(relay): Fix NRE in relay (Resolves #1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnwood committed Apr 27, 2024
1 parent 5443d66 commit 2614b15
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
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

0 comments on commit 2614b15

Please sign in to comment.