Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke IEmailSender to send email that contains international characters/non-ASCII characters #4218

Closed
xinwanc opened this issue Jan 23, 2019 · 9 comments

Comments

@xinwanc
Copy link

xinwanc commented Jan 23, 2019

I am currently invoking IEmailSender to send emails. However, when I try to send email to address that contains international characters through _emailSender.Send method. For example: ēfg456@gmail.com. However, I am receiving an exception which is shown below:
image

I am using Mailgun or Mailtrap as service provider for sending emails. I have contacted both of the service providers and they replied that they do support sending emails to address that contains international characters. Mailtrap has tested the email example given on their site and there is no issue.

Hope you can provide any advise regarding this matter. Thanks.

@maliming
Copy link
Member

I think you can refer: jstedfast/MailKit#649

@xinwanc
Copy link
Author

xinwanc commented Jan 23, 2019

Hi @maliming. Thanks for your reply. So refer to jstedfast/MailKit#649. May I know how do I apply:

var options = FormatOptions.Default.Clone ();
options.International = true;

into _emailSender.Send?

@maliming
Copy link
Member

There is no similar option in the abp framework, you can send it directly using the MailKit.

@xinwanc
Copy link
Author

xinwanc commented Jan 23, 2019

Sorry I would like to clarify: do you mean in default AbpFramework does not support non-ASCII characters or? To support international characters I need to send directly using MailKit instead of IEmailSender?

@maliming
Copy link
Member

Yes.

@xinwanc
Copy link
Author

xinwanc commented Jan 24, 2019

Ok. Noted. Thanks for your prompt reply. May I know that will IEmailSender in AbpFramework enhance to support sending email to address containing international characters in future? Or should say that should this to be taken into consideration?

@ryancyq
Copy link
Contributor

ryancyq commented Jan 24, 2019

@xinwanc You can clone and modify MailKitEmailSender

// using  Abp.MailKit;

[DependsOn(typeof(AbpMailKitModule))]
public class YourModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.ReplaceService<IEmailSender, MyMailKitEmailSender>(DependencyLifeStyle.Transient);
        }
    }
public class MyMailKitEmailSender : EmailSenderBase
    {
        private readonly IMailKitSmtpBuilder _smtpBuilder;

        public MailKitEmailSender(
            IEmailSenderConfiguration smtpEmailSenderConfiguration,
            IMailKitSmtpBuilder smtpBuilder)
            : base(
                  smtpEmailSenderConfiguration)
        {
            _smtpBuilder = smtpBuilder;
        }

        public override async Task SendAsync(string from, string to, string subject, string body, bool isBodyHtml = true)
        {
            using (var client = BuildSmtpClient())
            {
                var message = BuildMimeMessage(from, to, subject, body, isBodyHtml);
                var options = FormatOptions.Default.Clone ();
                options.International = true;
                await client.SendAsync(options, message);
                await client.DisconnectAsync(true);
            }
        }

        public override void Send(string from, string to, string subject, string body, bool isBodyHtml = true)
        {
            using (var client = BuildSmtpClient())
            {
                var message = BuildMimeMessage(from, to, subject, body, isBodyHtml);
                var options = FormatOptions.Default.Clone ();
                options.International = true;
                client.Send(options, message);
                client.Disconnect(true);
            }
        }

        protected override async Task SendEmailAsync(MailMessage mail)
        {
            using (var client = BuildSmtpClient())
            {
                var message = MimeMessage.CreateFromMailMessage(mail);
                var options = FormatOptions.Default.Clone ();
                options.International = true;
                await client.SendAsync(options, message);
                await client.DisconnectAsync(true);
            }
        }

        protected override void SendEmail(MailMessage mail)
        {
            using (var client = BuildSmtpClient())
            {
                var message = MimeMessage.CreateFromMailMessage(mail);
                var options = FormatOptions.Default.Clone ();
                options.International = true;
                client.Send(options, message);
                client.Disconnect(true);
            }
        }

        protected virtual SmtpClient BuildSmtpClient()
        {
            return _smtpBuilder.Build();
        }

        private static MimeMessage BuildMimeMessage(string from, string to, string subject, string body, bool isBodyHtml = true)
        {
            var bodyType = isBodyHtml ? "html" : "plain";
            var message = new MimeMessage
            {
                Subject = subject,
                Body = new TextPart(bodyType)
                {
                    Text = body
                }
            };

            message.From.Add(new MailboxAddress(from));
            message.To.Add(new MailboxAddress(to));
            
            return message;
        }
    }

@xinwanc
Copy link
Author

xinwanc commented Jan 31, 2019

Hi @ryancyq . Thanks for your code. But I dont understand why I am encountering error in this code:
image
It says i cannot implicity convert from MailKit.Net.Smtp.SmtpClient to System.Net.Mail.SmtpClient

@ryancyq
Copy link
Contributor

ryancyq commented Feb 1, 2019

You can take reference from Abp.MailKit/MailKitEmailSender

You need to override the SMTP client when importing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants