Sendgrid attachments in C#

28 views Asked by At

I tried different forms of encoding and adding attachments, without any success. Is there any other way to pass attachments in sendgrid? The issue is mail is sent, however there is no sign of attachments

public async Task<Result> SendEmailAsync(Email email, CancellationToken cancellationToken)
    {
        var from = new EmailAddress(email: $"noreply@{_options.Value.EmailDomain}", _options.Value.EmailName);

        var subject = email.Subject;
        var message = email.Message;

        var msg = new SendGridMessage()
        {
            From = from,
            Subject = subject,
            PlainTextContent = message,
        };

        foreach (var to in email.RecipientAddresses.Select(recipientAddress => new EmailAddress(recipientAddress)))
        {
            msg.AddTo(to);
        }

        if (email.AttachedFilesNames != null)
        {
            foreach (var fileLink in email.AttachedFilesNames)
            {
                var dmzFile = new DmzFile
                {
                    FileNameWithExtension = fileLink
                };

                var getFile = await _fileSource.GetFileAsync(email.SchoolId, dmzFile, cancellationToken);

                if (getFile != null)
                {
                    await msg.AddAttachmentAsync(fileLink, getFile, cancellationToken: cancellationToken);
                }
            }
        }

        var response = await _sendGridClient.SendEmailAsync(msg, cancellationToken);

        if (response.IsSuccessStatusCode)
            return Result.Ok();

        var responseMessage = await response.Body.ReadAsStringAsync(cancellationToken);

        return new Error(response.StatusCode.ToString(), responseMessage);
    }
0

There are 0 answers