3

I am trying to make a small form on VB.net that will send out an email with an attachment(s). I have tried the code below but I have a lot of unsolved compile errors. Is there a better way to do this below. This was some code that I found and tried to make it work for my purposes but didn't get it to work. Any thoughts or help on this will help.

I would be thankful to just get the email sent with out any attachments to start with. Thank you.

MailFormat.Text -- this is one error in the compile

'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
Doug Hauf
  • 3,025
  • 8
  • 46
  • 70

1 Answers1

5

Try this

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
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
  • Will this send out for all email accounts. Such as outlook. I work at a state job and we have all outlook accounts. Or does this just work with gmail? – Doug Hauf Dec 18 '13 at 15:36
  • 1
    Yes It will send email to all accounts – Vignesh Kumar A Dec 18 '13 at 16:07
  • SmtpServer.Host = "smtp.gmail.com" --> I think that this might be were I am going wrong (In fact I am certain). Can I find out this information in outlook on a tab control somewhere? – Doug Hauf Dec 18 '13 at 16:23
  • 1
    This is Your host details (i.e) from details. You can use any host that depends on what email you are going to use – Vignesh Kumar A Dec 18 '13 at 16:28
  • In outlook 2010 I looked quick and didn't see were the host details was listed. I will take another look I would think that it would be under options somewhere. – Doug Hauf Dec 18 '13 at 17:20
  • SmtpServer.Port = 587 SmtpServer.Host = "c.local" --> I think this is were I am having trouble I will have to try it at home with gmail later tonight. Does the Port have to change or does that remain constant on different computers? – Doug Hauf Dec 18 '13 at 18:53
  • Actually I don't have outlook at home. It might be different at work than on the home computer. So I would be just as happy as being able to send one via gmail and outlook.com at home. – Doug Hauf Dec 19 '13 at 01:48