繁体   English   中英

如何在 .NET 应用程序中使用 SendGrid V3 将 email 发送给多个收件人

[英]how to send an email using SendGrid V3 to multiple recipients, in a .NET app

它发送到一个确定。 但不是2个或更多。 我试过用和分隔地址; 和 to_addr 字符串中的空格,但失败了。

class EmailAddress 期望多个地址采用什么格式?

    public async sub  send_message( msg_subject, msg_body, from_addr_text, to_addr_text, optional commands = "")
       dim apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY")
       dim client as  new SendGridClient( apiKey, , , "v3",  )

       dim from = new EmailAddress( from_addr_text, "INSTRUMENT")
       dim subject = msg_subject
       if subject = ""
        subject = " "       ' won't send if empty
       End If
       dim to_addr = new EmailAddress( to_addr_text, "INSTRUMENT")
       dim plainTextContent = msg_body
       dim htmlContent = "<strong>" & msg_body & "</strong>"
       dim msg = MailHelper.CreateSingleEmail(from, to_addr, subject, plainTextContent, htmlContent)
       dim response = await client.SendEmailAsync(msg)
       if instr( commands, "SKIP POPUPS")           ' TEST TEXT MSG !!!
            exit sub
       End If
       popup_information( "MESSAGE SENDING", "Sent..." & vbcrlf & vbcrlf & "SUBJECT:  " & vbcrlf &  subject  &   vbcrlf & vbcrlf & "BODY:  " & vbcrlf &  msg_body )
end sub

不确定使用的是哪个SDK版本,但是如果遵循适用于C#的Sendgrid V3 api SDK,则应该在MailHelper类中找到以下方法

public static SendGridMessage CreateSingleEmailToMultipleRecipients(EmailAddress from, List<EmailAddress> tos, string subject,string plainTextContent,string htmlContent)

它接受List<EmailAddress>将电子邮件发送给多个收件人。 因此,不要在代码中使用以下行

dim msg = MailHelper.CreateSingleEmail(from, to_addr, subject, plainTextContent, htmlContent)

您应该使用以下代码

   var to_addr = new List<EmailAddress>();
   to_addr.Add(new EmailAddress( to_addr_text, "INSTRUMENT"));
   to_addr.Add(new EmailAddress( "secondperson@test.com", "secondperson")); // you can add multiple email in this list 

然后您可以在下面的代码中使用to_addr

dim msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, to_addr, subject, plainTextContent, htmlContent)

请原谅我在C#中的示例,但同样适用于您的VB.NET代码

这里的堆栈溢出答案显示了如何使用依赖注入到使用控制器的 Net 6 Web API 应用程序中实现 SendGrid。

此处引用的示例代码介绍了将 email 发送给多个收件人的场景: https://github.com/Davidmendoza1987/dotnet-sendgrid-examples

暂无
暂无

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

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