Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Sending E-Mail via VB

Options
  • 22-04-2003 5:14pm
    #1
    Registered Users Posts: 383 ✭✭


    hey all,

    does any one know any _easy_ way to send email (just plain text, no attachments) via VB.

    All the googling I did, all I came up with was either using compliated WinSock programms or even worse IMAP servers and stuff.

    Any one know an easier way?

    TIA


Comments

  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Winsock is what allows Win32 to open a socket or listen to a port, which is what is necessary in all TCP/IP based applications. IMAP is a TCP/IP based protocol for accessing email (via Winsock).

    VB comes with OCX controls that allow easy access to IMAP and, on a lower level, Winsock. As for protocol; email is sent using the SMTP protocol, not IMAP.

    Bare in mind you still have to connect to a mail server to send your mail. Anyhow, I dredged up this code that I had played about some time ago (please note the constants contain dummy values):
    Option Explicit
    
    Public iRequest As Integer
    
    Const sServer = "mail.foobar.net"
    Const sFrom = "from@test.net"
    Const sTo = "to@test.com"
    Const sSubject = "Test Subject"
    Const sBody = "Test message..."
    
    Private Sub Command1_Click()
        Winsock1.RemoteHost = sServer
        Winsock1.RemotePort = 25
        Winsock1.Connect
    End Sub
    
    Private Sub Winsock1_Connect()
        iRequest = 1
        Call setRequest
    End Sub
    
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        Dim sData As String
        Winsock1.GetData sData, vbString
        Text1.Text = Text1.Text & sData
        Call setRequest
    End Sub
    
    Public Sub setRequest()
        Dim Temp As String
        Select Case iRequest
            Case 1
                Temp = "HELO " & sServer
            Case 2
                Temp = "MAIL FROM: " & sFrom
            Case 3
                Temp = "RCPT TO: " & sTo
            Case 4
                Temp = "DATA"
            Case 5
                Temp = "To: " & sTo & vbCrLf & "From: " & sFrom & vbCrLf & "Subject: " & _
                    sSubject & vbCrLf & vbCrLf & sBody & vbCrLf & "."
            Case 6
                Temp = "QUIT"
            Case 7
                Winsock1.Close
                Beep
                Exit Sub
            Case Else
                Exit Sub
        End Select
        Temp = Temp & vbCrLf
        Text1.Text = Text1.Text & Temp
        iRequest = iRequest + 1
        Winsock1.SendData Temp
    End Sub
    


  • Registered Users Posts: 437 ✭✭Spunj


    If you are using Win2k / Xp than you could take a look at CDO (Collaboration Data Objects) as an alternative. They are really easy to use and extremely powerful.

    heres an example:
    Dim iMsg As CDO.Message
    Set iMsg = New CDO.Message
    
    With iMsg
         .From = "Mr. Bleh <bleh@bleh.com>"
         .To = "The Stuff <stuff@stuff.com>"
         .Subject = "Happy Easter"
         .HTMLBody = "<html>(etc....)The message.....</html>"
         .TextBody = "The message"
         .Send
    End With
    
    Set iMsg = Nothing
    
    You can also do custom headers etc like so:
    Dim flds As ADODB.Fields
    Set flds = iMsg.Fields
         .Item("urn:schemas:mailheader:Errors-to") = "errors@mydomain.com"
         .Item("urn:schemas:mailheader:Reply-To") = "reply@mydomain.com"      
         .Item("urn:schemas:mailheader:Disposition-Notification-To") = "receipt@mydomain.com"
         .Update
    End With
    Set flds = Nothing
    


    There's masses of information on the web about CDO once you know what you are looking for.

    edit..oh yeah, if you need to send via a certain SMTP server you can do it by setting the following:
    Dim iConf As CDO.Configuration
    Set iConf = New CDO.Configuration
    Dim flds As ADODB.Fields
    Set flds = iConf.Fields
    With flds
         .Item(cdoSendUsingMethod) = cdoSendUsingPort
         .Item(cdoSMTPServer) = "smtp.mydomain.com"
         .Item(cdoSMTPServerPort) = 25
         .Update
    End With         
    Set iMsg.Configuration = iConf
    

    Spunj


  • Registered Users Posts: 383 ✭✭cherrio


    Corinthian, thank you so much, it works perfect :) thanks to Spunj also for the input.


  • Registered Users Posts: 1,722 ✭✭✭Thorbar


    I had to write a few apps for sending mails with CDO for work and I must say its fairly handy once you get your head around all the different objects their using. One thing tho I think you'll have to have the profile set up on your computer in outlook if you plan on setting emails from that email name. You'll also need access to an IMAP server. Have a look at the javamail api its fairly decent too but if your stuck with vb then I'd suggest using CDO. Best place to start looking for info is the msdn on microsoft.com.


Advertisement