-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Comments
I think you can refer: jstedfast/MailKit#649 |
Hi @maliming. Thanks for your reply. So refer to jstedfast/MailKit#649. May I know how do I apply: var options = FormatOptions.Default.Clone (); into _emailSender.Send? |
There is no similar option in the abp framework, you can send it directly using the MailKit. |
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? |
Yes. |
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? |
@xinwanc You can clone and modify // 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;
}
} |
Hi @ryancyq . Thanks for your code. But I dont understand why I am encountering error in this code: |
You can take reference from Abp.MailKit/MailKitEmailSender You need to override the SMTP client when importing |
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](https://user-images.githubusercontent.com/38121289/51580903-0d4ba380-1f01-11e9-9058-c1702fcd0077.png)
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.
The text was updated successfully, but these errors were encountered: