Open Closed

Unable to send emails with attachments. #3607


User avatar
0
Tony_Albutt created
  • ABP Framework version: v5.3.3
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace: Setting value for 'Abp.Mailing.Smtp.Host' is null or empty!
  • Steps to reproduce the issue:"

Hi

I have run into a small problem when sending SMTP emails from a background job.

I have created a Email reader background job, that is scheduled to process the inbox emails for the tenants IMAP settings using MailKit. This is working well.

One of the functions is for me to send emails, with the attachments.

The only way to do so using Volo.Abp.Emailing, is to use the mailMessage

await _emailSender.SendAsync(mailMessage, true);

For emails without attachments, to use the tenants SMTP email credentials, I need to change the currentTenant

This works for emails, without attachments

                using (_currentTenant.Change(args.TenantId, args.TenantName))
                {
                    await _emailSender.SendAsync(
                        args.EmailAddress,
                        args.Subject,
                        args.Body,
                        args.IsHtml
                    );
                }

However, this does not work when using mailMessage, as suggested in your documentation.

using (_currentTenant.Change(args.TenantId, args.TenantName))
{
	await _emailSender.SendAsync(mailMessage, true)
}

This throws an error

-		$exception	{"Setting value for 'Abp.Mailing.Smtp.Host' is null or empty!"}	Volo.Abp.AbpException
+		Data	{System.Collections.ListDictionaryInternal}	System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
+		InnerException	null	System.Exception
		Source	"Volo.Abp.Emailing"	string
		
		StackTrace	
		   at Volo.Abp.Emailing.EmailSenderConfiguration.<GetNotEmptySettingValueAsync>d__6.MoveNext()
		   at Volo.Abp.Emailing.Smtp.SmtpEmailSender.<BuildClientAsync>d__4.MoveNext()
		   at Volo.Abp.Emailing.Smtp.SmtpEmailSender.<SendEmailAsync>d__5.MoveNext()
		   at Volo.Abp.Emailing.EmailSenderBase.<SendAsync>d__9.MoveNext()
		   at ITX.MailBot.MailBotAppService.<ForwardEmailWithAttachments>d__41.MoveNext() in 
		   C:\Users\antho\source\repos\ITX_New\aspnet-core\src\ITX.Application\MailBot\MailBotAppService.cs:line 412

+		TargetSite	{Void MoveNext()}	System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

When I remove the _currentTenant.Change(args.TenantId, args.TenantName code, so that I use the default credentials, I get a different error

-		$exception	{"Failure sending mail."}	System.Net.Mail.SmtpException
+		InnerException	{"Cannot access a closed file."}	System.Exception {System.ObjectDisposedException}
		Message	"Failure sending mail."	string
		Source	"Volo.Abp.Emailing"	string
		StackTrace	   
			at Volo.Abp.Emailing.Smtp.SmtpEmailSender.<SendEmailAsync>d__5.MoveNext()
			at Volo.Abp.Emailing.EmailSenderBase.<SendAsync>d__9.MoveNext()
			at ITX.MailBot.MailBotAppService.<ForwardEmailWithAttachments>d__41.MoveNext() 
			in C:\Users\antho\source\repos\ITX_New\aspnet-core\src\ITX.Application\MailBot\MailBotAppService.cs:line 412
+		TargetSite	{Void MoveNext()}	System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

This is my code for forward the email

        private async Task<bool> ForwardEmailWithAttachments(MimeMessage email, string emailAddress, InboxArgs args)
        {
            var message = email;
            var mailMessage = new MailMessage(
                FromEmailAddress,
                emailAddress,
                "FWD: " + email.Subject,
                 email.HtmlBody
                );

            if (email.Attachments.Any())
            {
                foreach (var mailAttachment in message.Attachments)
                {
                    string fileName = mailAttachment.ContentDisposition?.FileName ?? mailAttachment.ContentType.Name;
                    string FileType = Path.GetExtension(fileName);
                    string contentType = "application/" + FileType;
                    var part = (MimePart)mailAttachment;
                    using var stream = System.IO.File.Create(fileName);
                    part.Content.DecodeTo(stream);
                    stream.Position = 0;
                    mailMessage.Attachments.Add(new System.Net.Mail.Attachment(stream, fileName, contentType));
                }
                using (_currentTenant.Change(args.TenantId, args.TenantName))
                {
                    //TODO : TA : 2022-08-24 : The code below throws an error
                    await _emailSender.SendAsync(mailMessage, true);
                }
            }
            return true;
        }

I don't want to change the default SMTP yet and use my own.

Are you able to help?

Thanks and regards Tony


1 Answer(s)
  • User Avatar
    1
    Tony_Albutt created

    Not to warry

    I have implemented my own

    Have a good one

    Regards Tony

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09