Skip to content

Commit

Permalink
use mailer
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Dec 21, 2023
1 parent db3d2e1 commit 06e0722
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ public async Task<IActionResult> ResetPasswordAsync([FromBody] PassportRequest r
PassportResult result = await passportService.ResetPasswordAsync(passport).ConfigureAwait(false);

return result.Success
? Response<string>.Success("密码设置成功", result.LocalizationKey!, result.Token)
: Model.Response.Response.Fail(ReturnCode.RegisterFail, result.LocalizationKey!, result.Message);
? Response<string>.Success("密码设置成功", result.Token, result.LocalizationKey!)
: Model.Response.Response.Fail(ReturnCode.RegisterFail, result.Message, result.LocalizationKey!);
}

[HttpPost("Login")]
Expand All @@ -163,7 +163,7 @@ public async Task<IActionResult> LoginAsync([FromBody] PassportRequest request)

return result.Success
? Response<string>.Success("登录成功", result.LocalizationKey!, result.Token)
: Model.Response.Response.Fail(ReturnCode.LoginFail, result.LocalizationKey!, result.Message);
: Model.Response.Response.Fail(ReturnCode.LoginFail, result.Message, result.LocalizationKey!);
}

[Authorize]
Expand Down
4 changes: 2 additions & 2 deletions src/Snap.Hutao.Server/Snap.Hutao.Server/Option/AppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace Snap.Hutao.Server.Option;

public sealed class AppOptions
{
public string AfdianUserToken { get; set; } = default!;

public string JwtRaw { get; set; } = default!;

public string MailerSecret { get; set; } = default!;

public string ReCaptchaKey { get; set; } = default!;

public string RedisAddress { get; set; } = default!;
Expand Down
57 changes: 30 additions & 27 deletions src/Snap.Hutao.Server/Snap.Hutao.Server/Service/MailService.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using MailKit.Net.Smtp;
using MimeKit;
using Snap.Hutao.Server.Option;
using System.Runtime.CompilerServices;

namespace Snap.Hutao.Server.Service;

public sealed class MailService
{
private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger<MailService> logger;
private readonly SmtpOptions smtpOptions;
private readonly string mailerSecret;

public MailService(AppOptions appOptions, ILogger<MailService> logger)
public MailService(IHttpClientFactory httpClientFactory, AppOptions appOptions, ILogger<MailService> logger)
{
this.httpClientFactory = httpClientFactory;
this.logger = logger;
smtpOptions = appOptions.Smtp;

logger.LogInformation("Initialized with UserName:{UserName} Password:{Password}", smtpOptions.UserName, smtpOptions.Password);
mailerSecret = appOptions.MailerSecret;
}

public Task SendRegistrationVerifyCodeAsync(string emailAddress, string code)
Expand Down Expand Up @@ -204,29 +203,33 @@ string BuildBodyHeader()

private async Task SendMailAsync(MailOptions options)
{
MimeMessage mimeMessage = new()
using (HttpClient httpClient = httpClientFactory.CreateClient())
{
Subject = options.Subject,
From =
{
new MailboxAddress("DGP Studio", smtpOptions.UserName),
},
To =
{
new MailboxAddress(options.Address, options.Address),
},
Body = new TextPart("html")
MailData data = new()
{
Text = ComposeMailBody(options),
},
};

using (SmtpClient client = new())
{
await client.ConnectAsync(smtpOptions.Server).ConfigureAwait(false);
await client.AuthenticateAsync(smtpOptions.UserName, smtpOptions.Password).ConfigureAwait(false);
await client.SendAsync(mimeMessage).ConfigureAwait(false);
await client.DisconnectAsync(true).ConfigureAwait(false);
From = "DGP Studio <[email protected]>",
To = options.Address,
Subject = options.Subject,
BodyHtml = ComposeMailBody(options),
};

httpClient.DefaultRequestHeaders.Authorization = new("SECRET", mailerSecret);
await httpClient.PostAsJsonAsync("https://mailer.snapgenshin.cn/api/sendEmail", data).ConfigureAwait(false);
}
}

private sealed class MailData
{
[JsonPropertyName("from")]
public string From { get; set; } = default!;

[JsonPropertyName("to")]
public string To { get; set; } = default!;

[JsonPropertyName("subject")]
public string Subject { get; set; } = default!;

[JsonPropertyName("bodyHtml")]
public string BodyHtml { get; set; } = default!;
}
}

0 comments on commit 06e0722

Please sign in to comment.