如何使用SendGrid V3向.NET应用程序中的多个收件人发送电子邮件

时间:2017-11-08 19:35:48

标签: asp.net .net vb.net sendgrid

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

类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

1 个答案:

答案 0 :(得分:0)

不确定您使用的是哪个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代码