繁体   English   中英

SendEmailAsync 与附件

[英]SendEmailAsync with Attchment

我需要一些帮助,将 IEmailSender 接口应用于我的名为 EmailSender 的 class。

该接口应用了一种名为 SendEmailAsync(String email, String subject, String message) 的方法,该方法完美运行!

在某些情况下,我想发送带有 base64 字符串类型附件的电子邮件,是否可以使用相同的方式或类似的方式,或者有没有我没想到的方式来操作 class?

电子邮件发件人.cs

public class EmailSender: IEmailSender
    {
        public EmailOptions Options { get; set; }

        public EmailSender(IOptions<EmailOptions> emailOptions)
        {
            Options = emailOptions.Value;
        }

        public Task SendEmailAsync(string email, string subject, string message)
        {
            return Execute(Options.SendGridKey, subject, message, email);
        }

        private Task Execute(string sendGridKey, string subject, string message, string email)
        {
            var client = new SendGridClient(sendGridKey);
            var msg = new SendGridMessage()
            {
                From = new EmailAddress("no-reply@---", "no-reply"),
                Subject = subject,
                PlainTextContent = message,
                HtmlContent = message
            };
            msg.AddTo(new EmailAddress(email));
            try
            {
                return client.SendEmailAsync(msg);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} First exception caught.", ex);
            }
            return null;
        }
    }

调用时:

 await _emailSender.SendEmailAsync(
                                   _db.Users.Where("aa@aa.com"
                                    "email subject",
                                    "blah" + blah ");

如果您能够编辑文件,更好的方法是重载您的方法,该方法采用额外的可选参数attachmentattachments 在界面中有这个,并在您的EmailSender class 中正确实现它。

由于您使用的是SendGrid ,因此这是我找到的示例链接 - sendgrid-use-cases-attachments

一种不太理想的方法可能是将您的附件以base64 编码字符串作为您的消息的一部分 如果需要,添加它以及一些其他信息,例如长度、文件名、类型等。现在,您没有更改任何源代码,但您还有其他事情要担心 -

  • 以这种方式允许的最大附件大小是多少? 我想有一些消息长度限制
  • 您将如何访问客户端的附件? 它只是一个字符串,您需要以某种方式读取、解码并作为原始预期文件呈现。
  • email go 会正常通过还是被标记为垃圾邮件

编辑 - 第一个的潜在示例。

// Add this method in EmailSender
public Task SendEmailAsync(string email, string subject, string message, string attachment=null)
{
    return Execute(Options.SendGridKey, subject, message, email, attachment);
}

// Modify this method if it's not part of any interface
private Task Execute(object sendGridKey, string subject, string message, string email, string attachment=null)
{
    // Add the following after the line "msg.AddTo(new EmailAddress(email));"
    if ((!string.IsNullOrEmpty(attachment)) && File.Exists(attachment))
    {
        var bytes = File.ReadAllBytes(attachment);
        var file = Convert.ToBase64String(bytes);
        msg.AddAttachment(Path.GetFileName(attachment), file);
    }
 }

请记住,我无法测试它。 另外,我对你的代码有任务感到不舒服,但它们不是异步的,为什么 EmailOptions 有公共设置等等。但是如果你的代码有效,并且我的修改也有效,那么你应该可以说 -

  • await _emailSender.SendEmailAsync(email, subject, message);
  • await _emailSender.SendEmailAsync(email, subject, message, filepath); 其中文件filepath将是一个字符串,如"D:/MyFiles/somefile.txt"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM