使用Visual Basic 2015发送SendGrid电子邮件的Visual Basic示例代码

时间:2015-11-28 14:25:28

标签: visual-studio email smtp sendgrid

我刚刚在Azure Windows 2012服务器VM上安装了我的SendGrid帐户我的VS 2015并创建了一个新的Visual Basic控制台应用程序。

但我能找到的所有例子都在C#中。

4 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

我也找不到任何一个。特别是在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

答案 2 :(得分:0)

  

此代码适用于SendGrid

fetch('template.mst')
.then(response => response.text())
.then(template => {
    Mustache.parse(template);
    var output = Mustache.render(template, data);
    return document.getElementById('target').innerHTML = output;
}).catch(error => console.log('Unable to get the template: ', error.message));

答案 3 :(得分:0)

我也遇到了同样的问题。
我已经针对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