通过 VB.NET windows 应用程序发送 SendGrid 电子邮件的 Visual Basic (VB.NET) 示例代码
Visual Basic (VB.NET) example code to send SendGrid email by a VB.NET windows app
我刚刚在我的 AWS EC2 Windows 服务器 2019 我的 VS 2019 Pro 上为我的 VB.NET windows 应用程序安装了我的 SendGrid 帐户。
但是我能找到的所有示例都是用 C# 编写的。
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)
我也找不到。特别是在 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
This code is working for 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
我遇到了和你一样的问题
我已经针对 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
有了这个,虽然没有错误,但我没有将消息发送到 sendgrid。我的统计数据应该显示收到邮件,但什么也没有。我将 C# 代码转换为 vb.net 并制作了一个模块。我在将它作为一个模块时遇到了问题,所以我将它更改为 class 并且我正在创建它的一个实例并在 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
我刚刚在我的 AWS EC2 Windows 服务器 2019 我的 VS 2019 Pro 上为我的 VB.NET windows 应用程序安装了我的 SendGrid 帐户。
但是我能找到的所有示例都是用 C# 编写的。
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)
我也找不到。特别是在 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
This code is working for 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
我遇到了和你一样的问题
我已经针对 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
有了这个,虽然没有错误,但我没有将消息发送到 sendgrid。我的统计数据应该显示收到邮件,但什么也没有。我将 C# 代码转换为 vb.net 并制作了一个模块。我在将它作为一个模块时遇到了问题,所以我将它更改为 class 并且我正在创建它的一个实例并在 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