I am attempting to send an email with header information and email details included in the message body.
Here is the code I have tried:
The typescript.
/// <summary>
/// Sending an email to the client.
/// </summary>
sendEmail() {
if (this.email.userId) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer ' + abp.auth.getToken());
let url = `${AppConsts.remoteServiceBaseUrl}/EmailComponents/SendEmail?`;
if (this.email.recipientEmailAddress) {
url += `recipientEmail=${encodeURIComponent("" + this.email.recipientEmailAddress)}&`;
}
if (this.email.subject) {
url += `subject=${encodeURIComponent("" + this.email.subject)}&`;
}
if (this.name) {
url += `emailTemplate=${encodeURIComponent("" + this.name)}`;
}
this.http.post(url,
{
headers: headers,
message: this.email.body
})
.subscribe(result => {
this.notify.info(`Email sent successfully.`);
});
}
}
The endpoint controller
/// <summary>
/// Sends an email containing recipient email address, subject, message, and email template.
/// </summary>
/// <param name="recipientEmail">The recipient Email.</param>
/// <param name="subject">The subject.</param>
/// <param name="message">The message</param>
/// <param name="emailTemplate">The email template.</param>
/// <returns>Asynchronous result.</returns>
[HttpPost]
public async Task SendEmail(string recipientEmail, string subject, [FromBody] string message, string emailTemplate)
{
var userId = _abpSession.GetUserId();
var user = await GetCurrentUser(userId);
if (!string.IsNullOrEmpty(user.EmailAddress))
{
//Get smtp details.
var smtpHost = _emailSmtpSetting.FirstOrDefault(a => a.Name == "SMTP Host");
var smtpPort = _emailSmtpSetting.FirstOrDefault(b => b.Name == "SMTP Port");
var fromAddress = _emailSmtpSetting.FirstOrDefault(c => c.Name == "From Address");
var useSsl = _emailSmtpSetting.FirstOrDefault(d => d.Name == "Use SSL");
var useDefaultCredential = _emailSmtpSetting.FirstOrDefault(e => e.Name == "Use default credentials");
var username = _emailSmtpSetting.FirstOrDefault(f => f.Name == "SMTP Username");
var pwd = _emailSmtpSetting.FirstOrDefault(g => g.Name == "SMTP Password");
Dictionary<string, string> smtpSettings = new Dictionary<string, string>
{
{ "SMTP Host", smtpHost.Detail },
{ "SMTP Port", smtpPort.Detail },
{ "From Address", fromAddress.Detail },
{ "Use SSL", useSsl.Detail },
{ "Use default credentials", useDefaultCredential.Detail },
{ "SMTP Username", username.Detail },
{ "SMTP Password", pwd.Detail }
};
await _userEmailer.TryToSendEmail(user, message, subject, recipientEmail, AbpSession.GetTenantId(), emailTemplate, smtpSettings);
}
}
The desired outcome is for the email parameters to reach the endpoint successfully. However, the actual result I am encountering is a 401 unauthorized error.