簡體   English   中英

發送電子郵件與VB.net

[英]Send out an email with VB.net

我想在VB.net上創建一個小表單,它會發送一封帶有附件的電子郵件。 我已經嘗試了下面的代碼但是我有很多未解決的編譯錯誤。 下面有更好的方法嗎? 這是我發現的一些代碼,並試圖使其適用於我的目的,但沒有讓它工作。 任何想法或幫助都會有所幫助。

我很感激只是收到所有附件的電子郵件。 謝謝。

MailFormat.Text - 這是編譯中的一個錯誤

'TWO FUNCTIONS
    'SAME EXCEPT FIRST TAKES A STRING FOR ATTACHMENT
    'SECOND TAKES AN ARRAY LIST SO YOU CAN SEND MULTIPLE 
         'ATTACHMENTS
    'FROM: Email address FRom
    'TO: EMAIL address To
    'Subject: Subject; Body: MessageText
    'Optional CC, BCC: CC and bcc recipients
    'SMTPSERVER: Optional, if not specified 
   'local machine is used
    'AttachmentFile (first function: Optional, file name)
    'AttachmentFiles (second function: Optional, list of     
        'attachments in form of an array list)

    Public Sub SendMailOneAttachment(ByVal From As String, _
      ByVal sendTo As String, ByVal Subject As String, _
      ByVal Body As String, _
      Optional ByVal AttachmentFile As String = "", _
      Optional ByVal CC As String = "", _
      Optional ByVal BCC As String = "", _
      Optional ByVal SMTPServer As String = "")

        Dim myMessage As MailMessage

        Try
            myMessage = New MailMessage()
            With myMessage
                .To = sendTo
                .From = From
                .Subject = Subject
                .Body = Body
                .BodyFormat = MailFormat.Text
                'CAN USER MAILFORMAT.HTML if you prefer

                If CC <> "" Then .Cc = CC
                If BCC <> "" Then .Bcc = ""

                If FileExists(AttachmentFile) Then _
                 .Attachments.Add(AttachmentFile)

            End With

            If SMTPServer <> "" Then _
               SmtpMail.SmtpServer = SMTPServer
            SmtpMail.Send(myMessage)

        Catch myexp As Exception
            Throw myexp
        End Try

    End Sub

Public Sub SendMailMultipleAttachments(ByVal From As String,_
    ByVal sendTo As String, ByVal Subject As String, _
    ByVal Body As String, _
    Optional ByVal AttachmentFiles As ArrayList = Nothing, _
    Optional ByVal CC As String = "", _
    Optional ByVal BCC As String = "", _
    Optional ByVal SMTPServer As String = "")

        Dim myMessage As MailMessage
        Dim i, iCnt As Integer

        Try
            myMessage = New MailMessage()
            With myMessage
                .To = sendTo
                .From = From
                .Subject = Subject
                .Body = Body
                .BodyFormat = MailFormat.Text
                'CAN USER MAILFORMAT.HTML if you prefer

                If CC <> "" Then .Cc = CC
                If BCC <> "" Then .Bcc = ""

                If Not AttachmentFiles Is Nothing Then
                    iCnt = AttachmentFiles.Count - 1
                    For i = 0 To iCnt
                        If FileExists(AttachmentFiles(i)) Then _
                          .Attachments.Add(AttachmentFiles(i))
                    Next

                End If

            End With

            If SMTPServer <> "" Then _
              SmtpMail.SmtpServer = SMTPServer
            SmtpMail.Send(myMessage)


        Catch myexp As Exception
            Throw myexp
        End Try
    End Sub

    Private Function FileExists(ByVal FileFullPath As String) _
     As Boolean
        If Trim(FileFullPath) = "" Then Return False

        Dim f As New IO.FileInfo(FileFullPath)
        Return f.Exists

    End Function

試試這個

Imports System.Net.Mail
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New _
        Net.NetworkCredential("username@gmail.com", "password")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("YOURusername@gmail.com")
            mail.To.Add("TOADDRESS")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from GMAIL"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM