Results 1 to 20 of 20

Thread: [RESOLVED] send email with vb6 with smtp

  1. #1

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Resolved [RESOLVED] send email with vb6 with smtp

    I have develop an application that need to notify admin when somethings goes wrong through email?

    How do i send an email :

    1 without popping up outlook to the user?
    2 and make sure that the message was sent?

    am not sending mass email or what ever, just less that 2kb email message that needs to be sent describing the error that has occurred in the app.

    thanks
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: send email with vb6 with smtp

    You can either use CDO or vbsendmail for sending emails.

    You can find many samples if you search this forums.
    http://www.vbforums.com/search.php?searchid=2910939

    Check the CodeBank too: http://www.vbforums.com/forumdisplay.php?f=91

  3. #3
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: send email with vb6 with smtp

    I am using this code to send emails to customers without any intervention on the part of the operator. It requires that Microsoft Outlook is running, though.
    Code:
    Dim oApp As Outlook.Application
    Dim oEmail As Outlook.MailItem
    Set oApp = New Outlook.Application
    
    Screen.MousePointer = vbHourglass
    '==============================================
    ' FEDERAL PACKAGE
    '==============================================
    
    fax = Dir("Z:\DAD\FAXES\FAX1.TXT")
    If fax <> "" Then
        Set oEmail = oApp.CreateItem(olMailItem)
        With oEmail
        .To = "[email protected]"
        .Subject = "Production Report"
        .BodyFormat = olFormatPlain
        .Body = "Attached is the Production Report"
        .Attachments.Add "Z:\DAD\Faxes\Fax1.txt", olByValue
        .Recipients.ResolveAll
        .Save
        .Send
        End With
        Set oEmail = Nothing
    End If
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  4. #4

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: send email with vb6 with smtp

    dose it require outlook installed or it requires outlook running.
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  5. #5
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: send email with vb6 with smtp

    In my case, yes, you will get an error if Outlook is not installed and running.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  6. #6

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: send email with vb6 with smtp

    Ok thanks, but i can use your technique. it will fail if another version of outlook was installed than the one used to test it.
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: send email with vb6 with smtp

    CDO would be a better choice, especially on a server. You're not really supposed to be installing Outlook on servers for automated use, and CDO was provided specifically to handle such things.

    My guess: no code handout, solution not accepted. True?

  8. #8

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: send email with vb6 with smtp

    ok thanks, i finnaly settled for CDO. But one question, will it work in win7 and vista ?
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  9. #9
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: send email with vb6 with smtp

    Yes, but be careful to set the Configration properly.

    There is a lot of bad sample code on the Web that naively relies on CDO initializing based on the current user's Outlook Express or Outlook profile. These often fail on user's computers where Outlook Express does not exist or is not set up for the user. Windows 7 doesn't support OE at all, and Vista had a modified version (Windows Mail) that tends to protect its settings from casual use in this way.

    Configuration CoClass

    In many cases, the default configuration settings are sufficient to send and post messages successfully. The default configuration settings depend on the software installed on the computer on which you are using the CDO component. When you send messages without associating a Configuration object, these defaults are used; for example, if the computer has the SMTP service installed, the default configuration for sending messages is to write them into files in the SMTP pickup directory. Additionally, various settings can be loaded from Microsoft&#174; Outlook&#174; Express if it is installed. For more information about default settings, see the configuration fields section of the reference.
    You might start here if you haven't read the documentation yet.
    Last edited by dilettante; Feb 28th, 2011 at 11:43 AM.

  10. #10

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: send email with vb6 with smtp

    see the code i used

    vb Code:
    1. Public Function SendMail(sTo As String, sSubject As String, sFrom As String, _
    2.     sBody As String, sSmtpServer As String, iSmtpPort As Integer, _
    3.     sSmtpUser As String, sSmtpPword As String, bSmtpSSL As Boolean) As String
    4.    
    5.     On Error GoTo SendMail_Error:
    6.    
    7.     Dim lobj_cdomsg      As CDO.Message
    8.     Set lobj_cdomsg = New CDO.Message
    9.     lobj_cdomsg.Configuration.Fields(cdoSMTPServer) = sSmtpServer
    10.     lobj_cdomsg.Configuration.Fields(cdoSMTPServerPort) = iSmtpPort
    11.     lobj_cdomsg.Configuration.Fields(cdoSMTPUseSSL) = bSmtpSSL
    12.     lobj_cdomsg.Configuration.Fields(cdoSMTPAuthenticate) = 1
    13.     lobj_cdomsg.Configuration.Fields(cdoSendUserName) = sSmtpUser
    14.     lobj_cdomsg.Configuration.Fields(cdoSendPassword) = sSmtpPword
    15.     lobj_cdomsg.Configuration.Fields(cdoSMTPConnectionTimeout) = 30
    16.     lobj_cdomsg.Configuration.Fields(cdoSendUsingMethod) = 2
    17.     lobj_cdomsg.Configuration.Fields.Update
    18.     lobj_cdomsg.To = sTo
    19.     lobj_cdomsg.From = sFrom
    20.     lobj_cdomsg.Subject = sSubject
    21.     lobj_cdomsg.TextBody = sBody
    22.     'lobj_cdomsg.AddAttachment ("filepath")
    23.     lobj_cdomsg.Send
    24.     Set lobj_cdomsg = Nothing
    25.     SendMail = "ok"
    26.     Exit Function
    27. SendMail_Error:
    28.     SendMail = Err.Description
    29. End Function
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  11. #11
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: send email with vb6 with smtp

    Or you can use smpt:

    Code:
    Imports System.Net.Mail
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim MyMailMessage As New MailMessage()
            MyMailMessage.From = New MailAddress(TextBox6.Text)
            MyMailMessage.To.Add(TextBox1.Text)
            MyMailMessage.Subject = TextBox2.Text
            MyMailMessage.Body = TextBox3.Text
            Dim SMPT As New SmtpClient(ComboBox1.Text)
            SMPT.Port = ComboBox2.Text
            SMPT.EnableSsl = True
            SMPT.Credentials = New System.Net.NetworkCredential(TextBox4.Text, TextBox5.Text)
            SMPT.Send(MyMailMessage)
            MsgBox("The mail has been send!", MsgBoxStyle.Information, "Mail has been send!")
        End Sub
    End Class

  12. #12

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: send email with vb6 with smtp

    mitko29

    this is not vb.net, its vb6
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  13. #13
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: send email with vb6 with smtp

    Quote Originally Posted by coolcurrent4u View Post
    mitko29

    this is not vb.net, its vb6
    I am sorry wrong topic.

  14. #14
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: send email with vb6 with smtp

    There are Enums defined already for cdoSMTPAuthenticate (CdoProtocolsAuthentication) and cdoSendUsingMethod (CdoSendUsing), so no need for magic numbers.

  15. #15

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: send email with vb6 with smtp

    dilettante

    what do you mean actually, can you please list the enums, thanks?
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  16. #16
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: send email with vb6 with smtp

    In line 12 and line 16 above you use 1 and 2. Instead you could use the Enum values cdoBasic and cdoSendUsingPort.

    You can easily see these things using the object Browser in the VB6 IDE and they are fully described in the documentation. They are, like most Const and Enum values, meant to be used in order to make your programs easier to maintain because a person reading these programs can tell what is being done (without having to go run to the documentation themselves to look up every line of code used).

  17. #17

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: send email with vb6 with smtp

    ok thanks for the tip
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  18. #18
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: send email with vb6 with smtp

    Why don't you create a simple .net class that sends the mail, something like mitko29 showed. Call that code from vb6 use [Comvisible] in .net. So you have all code of you're own
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  19. #19

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: send email with vb6 with smtp

    the thing is tha if i use .net, then i need .net libraries to use the program. That is the reason i still develop in vb6. what if my user does not have internet connection to download .net library?
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  20. #20
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: send email with vb6 with smtp

    wow.........

    You always can make a installer for your application and then to include in it the .net framework you need.

    I will tell you something about visual basic 6 too :
    The program language was made many years ago, and if you want to keep the good work with all programing stuff you have to evolve into c# or vb.net,maybe the syntax is a little strange at the beginning but you will get used.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width