dcsimg
November 27, 2020
Hot Topics:

SendMail with Attachments

  • By Sam Huggill
  • Send Email »
  • More Articles »

The first thing that we are going to do is to create the User Interface, based around one form, that will interact with out Sendmail module. Open VB and start a new Standard EXE Project. Add a form (frmDemo) and add the following controls to the form:

txtServer, txtFromAddress, txtToAddress, txtSubject, txtAttach, txtBody, lblStatus, cmdSend, cmdSelect, cmdClose, txtOutput, rtfAttach, Winsock1, cmdDialog

Now add the following code to the form:

Option Explicit

Private Sub cmdClose_Click()
Unload Me
End Sub

Private Sub cmdSelect_Click()

cmdDialog.ShowOpen
txtAttach = cmdDialog.filename

End Sub

Private Sub cmdSend_Click()

cmdSend.Enabled = False

If ValidateEntry = False Then 
  MsgBox _
    "Either the server name or to address were left _
    empty.", _
    vbCritical + vbOKOnly, Me.Caption
  cmdSend.Enabled = True
  Exit Sub
End If

If txtAttach.Text <> "" Then
  lblStatus = "Encoding file attachment"
  Base64EncodeFile txtAttach.Text, rtfAttach, txtOutput
End If

lblStatus = "Connecting to POP Server"
ConnectToServer txtServer.Text, Winsock1

End Sub

Private Sub Form_Load()

txtAttach = ""
txtBody = ""
txtFromAddress = ""
txtServer = ""
txtSubject = ""
txtToAddress = ""

End Sub

Private Sub Winsock1_Connect()

lblStatus = "Connected to POP Server"
Wait 0.5
lblStatus = "Sending mail"

If txtAttach.Text = "" Then
  SendMail txtFromAddress, txtToAddress, txtSubject, _
    txtBody, Winsock1
Else
  SendMail txtFromAddress, txtToAddress, txtSubject, _
    txtBody, Winsock1, txtAttach, txtOutput
End If

lblStatus = "Mail sent"
cmdSend.Enabled = True
lblStatus = "Status:"

End Sub

Private Sub Winsock1_Error(ByVal Number As Integer, _
  Description As String, ByVal Scode As Long, _
  ByVal Source As String, ByVal HelpFile As String, _
  ByVal HelpContext As Long, CancelDisplay As Boolean)
  
MsgBox "Error Number: " & Number & vbCrLf & _
  Description & vbCrLf & Source, vbCritical + _
  vbOKOnly, _
  Me.Caption
End Sub

Private Function ValidateEntry() As Boolean

ValidateEntry = True
If txtServer.Text = "" Or txtToAddress = "" _
   Then ValidateEntry = False

End Function




Page 2 of 3



This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.


Thanks for your registration, follow us on our social networks to keep up-to-date