【问题标题】:Send out an email with VB.net使用 VB.net 发送电子邮件
【发布时间】:2014-01-06 19:37:33
【问题描述】:

我正在尝试在 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

【问题讨论】:

标签: vb.net


【解决方案1】:

试试这个

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

【讨论】:

  • 这是否会发送给所有电子邮件帐户。比如前景。我在州工作,我们有所有的前景账户。或者这仅适用于 gmail?
  • 是的,它将向所有帐户发送电子邮件
  • SmtpServer.Host = "smtp.gmail.com" --> 我认为这可能是我出错了(事实上我很确定)。我可以在某个选项卡控件的 Outlook 中找到这些信息吗?
  • 这是您的主机详细信息(即)来自详细信息。您可以使用任何主机,具体取决于您要使用的电子邮件
  • 在 Outlook 2010 中,我快速查看并没有看到是否列出了主机详细信息。我会再看一下,我认为它会在某个地方的选项下。
猜你喜欢
  • 2010-12-24
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 2016-11-01
相关资源
最近更新 更多