Reputation: 166
What I'm trying to do is send an email adding the header information and email details and send the email message as the body.
This is the code that I've already tried:
The typescript.
/// <summary>
/// Send 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 to the recipient email address.
/// </summary>
/// <param name="recipientEmail">The recipientEmail.</param>
/// <param name="subject">The subject.</param>
/// <param name="message">The message.</param>
/// <param name="emailTemplate">The email template.</param>
/// <returns>The 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 expected result is the email parameters getting to the endpoint successfully. The actual result that I'm getting is 401 unauthorised.
Upvotes: 1
Views: 1182
Reputation: 1117
This may not resolve the issue fully for you, and I'm making an assumption as you've not posted your full controller, that you are using an api controller, and your controller is something like this:
[Route("[controller]")]
[ApiController]
public class EmailComponentsController : ControllerBase
{
[HttpPost]
public async Task SendEmail(string recipientEmail, string subject, [FromBody] string message, string emailTemplate)
{
// your code here...
}
}
When you POST, MVC pipeline will match the method based on the HTTP verb, in this case POST, so you shouldn't put the method name in your URL...
This works:
somedomain.com/EmailComponents?recipientEmail=testEmail&subject=testSubject
This won't:
somedomain.com/EmailComponents/sendemail?recipientEmail=testEmail&subject=testSubject
This should produce a 404 though, not 401 response.
Have you tried removing the authentication form your controller to make it accessible without authentication, to narrow the problem down? Adding the [AllowAnnonymous]
attribute to the class or method?
Upvotes: 2