niico
niico

Reputation: 12749

Sending Amazon Simple Email from ASP.net Core MVC 2 app

I have a solution with many projects in. 1 Business services project that sends email using Amazon SES. 1 ASP.net MVC 5 app that connects to the Business Services project.

There are a few MVC 5 projects in the solution - they all send email. Each must include AWS settings in the web config - like this:

<add key="AWSProfileName" value="myprofile" />
<add key="AWSProfilesLocation" value="C:\AWS\mycredentials\creds.txt" />
<add key="AWSRegion" value="eu-west-1" />

This works fine.

In the .net Core MVC 2 app however, I keep getting this error:

System.IO.FileNotFoundException: 'Could not load file or assembly 'AWSSDK.SimpleEmail, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c.......'. The system cannot find the file specified.'

I assume this is because it can't find the above settings. As .net Core apps don't have a web.config I think it looks for these in appconfig.json

I've found an example of ProfileName and Region - but not ProfilesLocation in appsettings.json:

https://aws.amazon.com/blogs/developer/configuring-aws-sdk-with-net-core/

So I've tried this

"AWS": {
    "Profile": "myprofile",
    "Region": "eu-west-1",
    "ProfilesLocation": "C:\\AWS\\mycredentials\\creds.txt"
}

But I keep getting the same error. How can I get AWS.SimpleEmail working from this .net core mvc 2 app?

Thanks.

Upvotes: 2

Views: 2257

Answers (1)

Mohamad Elnaqeeb
Mohamad Elnaqeeb

Reputation: 563

ok for new comers here's how you get AWS service to use your appsettings.json In Startup.cs

services.AddDefaultAWSOptions(Configuration.GetAWSOptions()); 
services.AddAWSService<IAmazonSimpleEmailService>();

Surely add AWS configuration to appsettings

"AWS": { "Profile": "profileName", "Region": "us-east-1" }

Lastly in order to use above configuration in your AWS service you have to add the service with Dependency Injection to your class and use it for Example to send emails with SimpleEmailService in the EmailSender class:

public class EmailSender : IEmailSender 
{ 
   IAmazonSimpleEmailService _SES; 
   public EmailSender(IAmazonSimpleEmailService SES) 
   { 
      _SES= SES; 
   }

and then call it in your function and dont use

AmazonSimpleEmailServiceClient

but use the service

_SES.SendEmailAsync(sendRequest);

Upvotes: 3

Related Questions