Sending Emails with attachments

Started by John Montenigro, April 27, 2014, 07:09:44 PM

Previous topic - Next topic

José Roca

> ' Set the sendusing field to 'cdoSendUsingExchange'.
>    Config("http://schemas.microsoft.com/cdo/configuration/sendusing") = 3

If you look at the examples that I have posted, for example:


      Flds = iConf.Fields
      DIM schema AS STRING
      schema = "http://schemas.microsoft.com/cdo/configuration/"
      Flds.Item(schema & "sendusing").Value = 2
      Flds.Item(schema & "smtpserver").Value = "smtp.gmail.com"
      Flds.Item(schema & "smtpserverport").Value = 465
      Flds.Item(schema & "smtpauthenticate").Value = 1
      Flds.Item(schema & "sendusername").Value = "mymail@gmail.com"
      Flds.Item(schema & "sendpassword").Value =  "mypassword"
      Flds.Item(schema & "smtpusessl").Value = 1
      Flds.Update


you can deduct that you have to use:


Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = 3


And this exemplifies one of the main problems of trying to translate VB code. VB uses all kinds of tricks and shortcuts, such as the Item and Value properties being defaults that you can omit and the compiler adds it for you. If you don't know that, how are you going to guess that to translate it you have to add the missing Item and Value properties?

No wonder millions of VB programmers have been using COM during many years and most haven't learnerd anything in the process.

José Roca

#31
The use of the compound syntax also hides clarity. The steps are the following:


DIM pConfig AS CDO_IConfiguration
pConfig = NEWCOM "CDO.Configuration"

DIM pFields AS ADOFields
pFields = pConfig.Fields

DIM pField AS AdoField
pField = pFields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")
pField.Value = 3


This code creates a CDO Configuration object:

DIM pConfig AS CDO_IConfiguration
pConfig = NEWCOM "CDO.Configuration"

This one gets a reference to the ADO fields collection of the CDO Configuration object:

DIM pFields AS ADOFields
pFields = pConfig.Fields

This one gets a reference to the wanted field in the fields collection:

DIM pField AS AdoField
pField = pFields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")

An this one sets the wanted value to that field:

pField.Value = 3

Doing it step by step, it is clear what it does.

Using:

Config("http://schemas.microsoft.com/cdo/configuration/sendusing") = 3

it is as clear as mud.

Compound syntax and defaut values save typing at the cost of clarity.

José Roca

The C++ example is also difficult to understand if you're a beginner in COM programming, but at least it shows the needed steps:


// Get the sendusing field.
FieldsPtr Flds = Config->GetFields();
FieldPtr Fld1 = Flds->GetItem("http://schemas.microsoft.com/cdo/configuration/sendusing");

// Set the sendusing field to cdoSendUsingExchange.
Fld1->Value = "3";


FieldsPtr Flds = Config->GetFields();

is equivalent to

DIM pFields AS ADOFields
pFields = pConfig.Fields

in the code that I have posted

FieldPtr Fld1 = Flds->GetItem("http://schemas.microsoft.com/cdo/configuration/sendusing");

is equivalent to

DIM pField AS AdoField
pField = pFields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")

and

// Set the sendusing field to cdoSendUsingExchange.
Fld1->Value = "3";

is equivalent to

pField.Value = 3


Michael Stefanik

Time for a shameless plug. If you want to avoid all of the "fun" with CDO and want a simple, API oriented solution, try SocketTools. There's a thread I had posted over on the PowerBASIC forums that answers a similar question a while back. It handles all of the SSL/TLS stuff, message composition, etc. and three's no dependency on CDO, COM and so on. Here's an updated version of the example code:


#COMPILE EXE
#DIM ALL

#INCLUDE "CSTOOLS8.INC"

FUNCTION PBMAIN () AS LONG
    Dim mailServer As SMTPSERVER
    Dim mailMessage As SMTPMESSAGE
    Dim strHostName As String
    Dim strUserName As String
    Dim strPassword As String
    Dim strSender As String
    Dim strRecipient As String
    Dim strSubject As String
    Dim strMessage As String
    Dim strAttachment As String
    Dim nResult As Long

    SmtpInitialize($CSTOOLS8_LICENSE_KEY)

    strHostName = "smtp.gmail.com"
    strUserName = "username@gmail.com"
    strPassword = "secret"

    ' Configure the server options and user credentials for GMail
    mailServer.lpszHostName = StrPtr(strHostName)
    mailServer.nHostPort = %SMTP_PORT_SUBMIT
    mailServer.lpszUserName = StrPtr(strUserName)
    mailServer.lpszPassword = StrPtr(strPassword)
    mailServer.dwOptions = %SMTP_OPTION_SECURE

    strSender = "User Name <username@gmail.com>"
    strRecipient = "Robert Smith <rsmith@company.com>"
    strSubject = "This is a test message from PowerBasic"
    strMessage = "This is a test, this is only a test"
    strAttachment = "C:\Users\UserName\Documents\SomeFile.zip"

    ' Compose the email message with a file attachment
    mailMessage.lpszFrom = StrPtr(strSender)
    mailMessage.lpszTo = StrPtr(strRecipient)
    mailMessage.lpszSubject = StrPtr(strSubject)
    mailMessage.lpszText = StrPtr(strMessage)
    mailMessage.lpszAttach = StrPtr(strAttachment)

    ' Submit the message to GMail for delivery
    nResult = SmtpSubmitMessage(mailServer, mailMessage, 0, 0)

    If nResult = %SMTP_ERROR Then
        ? "Unable to send message to " + strRecipient
    Else
        ? "Message was sent to " + strRecipient
    End If
END FUNCTION

Mike Stefanik
sockettools.com