首页 > 解决方案 > 使用 .net 在 Visual Studio 中发送电子邮件

问题描述

我的代码可以使用 smtp 工作到一定程度,它可以连接,但我无法对其进行授权。我现在什至通过 API 进行了连接。仍然无法发送电子邮件。我尝试了多种方法。这是在 VB.Net 上使用 VS2019 中的 VB。代码如下。谢谢

Imports GemBox.Email
Imports GemBox.Email.Smtp

    Private Sub btmSendEmail_Click(sender As Object, e As EventArgs) Handles btmSendEmail.Click
        Try
            Dim message As New MailMessage(emailFrom, emailTo)

            ' Add subject.
            message.Subject = "Send HTML Email with Image and Attachment"
            message.BodyText = "Testing"
            ' Add HTML body with CID embedded image.
            'Dim cid As String = "image001@gembox.com"
            '  message.BodyHtml = "<html><body>" &_
            '  "<p>Hi ,</p>" &_
            '  "<p>This message was created and sent with:</p>" &_
            '  "<p><img src='cid:" & cid & "'/></p>" &_
            '  "<p>Read more about it on <a href='https://www.gemboxsoftware.com/email'>GemBox.Email Overview</a> page.</p>" &_
            '  "</body></html>"

            ' Add image as inline attachment.
            '  message.Attachments.Add(New Attachment("%#gembox-email-logo.png%") With {.ContentId = cid})

            ' Add file as attachment.
            '   message.Attachments.Add(New Attachment("%#sample-file.pdf%"))

            ' Create new SMTP client and send an email message.
            Using smtp As New SmtpClient("smtp.gmail.com")
                smtp.Connect()
                'Fails below step
                smtp.Authenticate(emailFrom.ToString, emailPsw.ToString)
                smtp.SendMessage(message)
            End Using

        Catch ep As Exception
            MsgBox(ep.ToString)
        End Try
End Sub

标签: emailsmtp

解决方案


推荐阅读