【问题标题】:Visual Basic (VB.NET) example code to send SendGrid email by a VB.NET windows app通过 VB.NET Windows 应用程序发送 SendGrid 电子邮件的 Visual Basic (VB.NET) 示例代码
【发布时间】:2015-11-28 14:25:27
【问题描述】:

我刚刚在我的 AWS EC2 Windows server 2019 我的 VS 2019 Pro 上安装了我的 SendGrid 帐户,用于我的 VB.NET Windows 应用程序。

但我能找到的所有示例都是用 C# 编写的。

【问题讨论】:

    标签: visual-studio email smtp sendgrid


    【解决方案1】:

    我遇到了和你一样的问题。
    我已经针对 C#.NET 和 VB.NET 进行了开发和测试。

    C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Configuration;
    using SendGrid;
    using SendGrid.Helpers.Mail;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                TestSendGrid().Wait();
            }
    
            static async Task TestSendGrid()
            {
                try
                {
                    var apiKey = ConfigurationManager.AppSettings["SENDGRID_APIKEY"];
                    var client = new SendGridClient(apiKey);
                    var msg = new SendGridMessage()
                    {
                        From = new EmailAddress("test@example.com", "Test User"),
                        Subject = "Hello World from the SendGrid C#.NET SDK!",
                        PlainTextContent = "Hello, Email!",
                        HtmlContent = "<strong>Hello, Email!</strong>"
                    };
                    msg.AddTo(new EmailAddress("test@example.com", "Test User"));
                    var response = await client.SendEmailAsync(msg);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
    

    VB.NET

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Threading.Tasks
    Imports System.Configuration
    Imports SendGrid
    Imports SendGrid.Helpers.Mail
    
    Module Module1
    
        Sub Main()
            TestSendGrid().Wait()
        End Sub
    
        Private Async Function TestSendGrid() As Task
            Try
                Dim apiKey = ConfigurationManager.AppSettings("SENDGRID_APIKEY")
                Dim client = New SendGridClient(apiKey)
                Dim msg = New SendGridMessage() With {
                    .From = New EmailAddress("test@example.com", "Test User"),
                    .Subject = "Hello World from the SendGrid VB.NET SDK!",
                    .PlainTextContent = "Hello, Email!",
                    .HtmlContent = "<strong>Hello, Email!</strong>"
                }
                msg.AddTo(New EmailAddress("test@example.com", "Test User"))
                Dim response = Await client.SendEmailAsync(msg)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Function
    End Module
    

    参考:https://docs.microsoft.com/en-us/azure/sendgrid-dotnet-how-to-send-email

    【讨论】:

      【解决方案2】:

      我也找不到。特别是在 v3 中。我终于让它在 VB.NET 中工作了。希望这可以节省一些时间。话虽如此,我最终还是改用了 mailgun,因为它的功能比 sendgrid 强大得多……

      Public Class Form1
          Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
              SendEmail()
          End Sub
      
          Private Async Sub SendEmail()
              Dim apiKey = "put your api key here ... should start with sg.something"
              Dim sg = New SendGridAPIClient(apiKey)
      
              Dim from = New Email("billgates@microsoft.com")
              Dim subject = "Hello World from the SendGrid CSharp Library!"
              Dim sto = New Email("barakobama@whitehouse.gov")
              Dim content = New Content("text/plain", "Hello, Email!")
              Dim mail = New Mail(from, subject, sto, content)
              Dim response = Await sg.client.mail.send.post(requestBody:=mail.[Get]())
          End Sub
      End Class
      

      【讨论】:

      • 我应该在 References 中添加什么以获得“SendGridAPIClient”?
      • 在类的顶部导入 SendGrid。不过,在您这样做之前,请使用 NuGet 将 SendGrid 安装到项目中
      【解决方案3】:

      此代码适用于 SendGrid

      Imports SendGrid.Helpers.Mail
      Imports SendGrid    
                  Public Sub SchedularCallback(ByRef data As EmailDataBulk)               
                      Try 
                              SchedularCallbackGrid(data)                     
                      Catch ex As Exception
      
                      End Try
                  End Sub
                   Public Async sub SchedularCallbackGrid()      
                      Dim response As Response
                      Try
      
                          Dim client = New SendGridClient("Send grid api Key")
                          Dim from = New EmailAddress("from sample mailID", "From mail name") '' Login Email and Display Its name in mail,on Inbox
                          Dim subject = "Hello Email!"
                          Dim sto = New EmailAddress("Recepient EmailID", "")
                          Dim plainTextContent = "Content"
                          Dim htmlContent = "Content"
                          Dim msg = MailHelper.CreateSingleEmail(from, sto, subject, plainTextContent, htmlContent)
                          response = Await client.SendEmailAsync(msg)            
      
                      Catch ex As Exception           
                          Throw ex
                      End Try
      
                  End sub
              End Class
      

      【讨论】:

        【解决方案4】:

        有了这个,虽然没有错误,但我没有在 sendgrid 中收到消息。我的统计数据应该显示收到了一封邮件,但什么也没有。我将 C# 代码转换为 vb.net 并创建了一个模块。我将它作为一个模块存在问题,所以我将它更改为一个类,我正在创建它的一个实例并在 button_click 事件中调用它。任何想法为什么我没有收到电子邮件?

        Imports System
        Imports System.Collections.Generic
        Imports System.Linq
        Imports System.Text
        Imports System.Threading.Tasks
        Imports System.Configuration
        Imports SendGrid
        Imports SendGrid.Helpers.Mail
        Class sendgrid1
            Sub Main(ByVal test As String)
                TestSendGrid(test).Wait()
            End Sub
        
            Private Async Function TestSendGrid(ByVal test As String) As Task
                Try
                    Dim apiKey = ConfigurationManager.AppSettings("ApiKey")
                    Dim client = New SendGridClient(apiKey)
                    Dim msg = New Helpers.Mail.SendGridMessage() With {
                        .From = New EmailAddress("admin@pacificwestcapital.net", test),
                        .Subject = "Hello World from the SendGrid VB.NET SDK!",
                        .PlainTextContent = "Hello, Email!",
                        .HtmlContent = "<strong>Hello, Email!</strong>"
                    }
                    msg.AddTo(New EmailAddress("test@test.com", "Test"))
                    Dim response = Await client.SendEmailAsync(msg)
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                End Try
            End Function
        End Class
        Partial Class admin_sendgrid
            Inherits System.Web.UI.Page
            Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        
            End Sub
            Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
                Dim sg As New sendgrid1
                Call sg.Main("Test")
            End Sub
        End Class
        

        【讨论】:

          【解决方案5】:
          Imports System.Net.Sockets
          Imports System.Text
          Imports System.Net.Mail
          Imports System.Threading
          Imports System
          Imports System.IO
          
          Imports SendGrid
          Imports System.Net
          
          
          ' Create the email object first, then add the properties.
          Dim myMessage As SendGridMessage
          myMessage = New SendGridMessage()
          
          ' Add the message properties.
          myMessage.From = New MailAddress("<my email addr>")
          
          ' Add multiple addresses to the To field.
          
          
          
          myMessage.AddTo("<destination email addr 1>")
          myMessage.AddTo("<destination email addr 2>")
          myMessage.AddTo("<destination email addr 3>")
          myMessage.Subject = "Testing the SendGrid Library 2"
          
          'Add the HTML and Text bodies
          myMessage.Html = "<p>Hello World!</p>"
          myMessage.Text = "Hello World plain text!"
          
          Dim credentials As NetworkCredential
          
          
          credentials = New NetworkCredential("apikey", "<my api pw>")
          transportWeb = New Web(credentials)
          transportWeb.DeliverAsync(myMessage)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-05-08
            • 2016-06-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-13
            相关资源
            最近更新 更多