Lookup SMTP server

irish634

Registered User.
Local time
Today, 14:02
Joined
Sep 22, 2008
Messages
230
I'm using the old cdo reference to send emails from a database. Can anyone point me to a reference or tell me how to programatically find out what the particular SMTP server is on a particular machine?

Nearly all of the examples I have found hard code the server name and I want this application to be adaptable accross various servers and email clients

Thanks
 
I don't believe you can programmatically solve this one. You need to know the name of the server and it'll be the same as the outgoing mail server that you'd specify if you were setting up an email account. This might be at your ISP like mail.yourisp.com or maybe if your company owns an internet domain, like mail.yourdomain.com or something like that. One of my customers runs a mail server on their intranet, so I can specify that machine's network name, that machine's IP address, or the mail server of their ISP on the internet. Any of these will work.
 
Nearly all of the examples I have found hard code the server name and I want this application to be adaptable accross various servers and email clients

Examples generally hard code data like that for making the example simple.

Every database can have different fields and each programmer has their idea on how to store and retrieve the data. It would be very difficult to make an example that woudl work with every database.

By hard coding an example, it shows the programmer what the end result needs to be in a variable/control/etc.. It is then your chooice on how to best make it fit in your database.


If you are using CDO/STMP, it is already email client independent.

Since you can use any SMTP sever on the internet, are you wantin to server the internet for any "open" (unsecured) SMTP server?
 
Examples generally hard code data like that for making the example simple.

Every database can have different fields and each programmer has their idea on how to store and retrieve the data. It would be very difficult to make an example that woudl work with every database.

By hard coding an example, it shows the programmer what the end result needs to be in a variable/control/etc.. It is then your chooice on how to best make it fit in your database.


If you are using CDO/STMP, it is already email client independent.

Since you can use any SMTP sever on the internet, are you wantin to server the internet for any "open" (unsecured) SMTP server?

Well, I am not sure. What I have run into is where I was using this in an Office 2000 environment:
Code:
    Set objEmail = CreateObject("CDO.Message")
    Set objConfig = CreateObject("CDO.Configuration")


    If sPriority = "High" Then
        objEmail.Fields.Item(cdoImportance) = cdoHigh
        objEmail.Fields.Item(cdoImportance) = 2
        objEmail.Fields("urn:schemas:mailheader:X-MSMail-Priority") = "High"
        objEmail.Fields("urn:schemas:mailheader:X-Priority") = 2
        objEmail.Fields("urn:schemas:httpmail:importance").Value = 2
        objEmail.Fields.Update
    End If

    With objEmail
        Set objEmail.Configuration = objConfig
        .From = sFrom
        .To = sTo
        .Subject = sSubject
        .HTMLBody = sBody
        
        If sCc <> STR_NULL Then objEmail.CC = sCc
        If sBcc <> STR_NULL Then objEmail.BCC = sBcc
        If sReplyTo = STR_NULL Then sReplyTo = sFrom
        .ReplyTo = sReplyTo
        If sAttachment <> STR_NULL Then objEmail.AddAttachment sAttachment
        .Fields.Update
    End With
        
    objEmail.Send

This works fine on Office 2k. When I try to run this on my PC (Office 2003) I have to configure the email as such:
Code:
    With objConfig.Fields
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "my SMtp Server"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Update
    End With

If I don't configure it this way, the code does not work.


Well, I suppose I could find an unsecured server, but I am not sure that's actually kosher is it?
 
Since you are dealing with automation already, try opening up an Outlook application object and see if you can find what you want there. Outlook will include the address of an SMTP handler. You might have to browse the Outlook help for VBA programming in the Outlook context to get the proper name of the property you want.
 
Here's a procedure that should work without requiring you to set any references (works in VBScript, as well):

Code:
' SEND EMAIL VIA SMTP USING CDOEX/CDOSYS
Sub prcSendMail(pstrFrom,pstrTo,pstrCC,pstrBCC,pstrSubject,pstrBody)
  Const cstrSMTPServer = "NAMEOFYOURSMTPSERVER"
  Const cintCDOSendUsingPort = 2
  Dim objConfig, objMsg

  If (Len(pstrFrom & "") > 0) And (Len(pstrTo & "") > 0) And (Len(pstrSubject & "") > 0) And (Len(pstrBody & "") > 0) Then
    Set objConfig = CreateObject("CDO.Configuration")
    objConfig.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cintCDOSendUsingPort
    objConfig.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = cstrSMTPServer
    objConfig.Fields.Update
    Set objMsg = CreateObject("CDO.Message")
    Set objMsg.Configuration = objConfig
    objMsg.From = pstrFrom
    objMsg.To = pstrTo
    If Len(pstrCC & "") > 0 Then objMsg.CC = pstrCC
    If Len(pstrBCC & "") > 0 Then objMsg.BCC = pstrBCC
    objMsg.Subject = pstrSubject
    objMsg.HTMLBody = pstrBody
    objMsg.Send
    Set objMsg = Nothing
    Set objConfig = Nothing
  End If
End Sub

You can also set a reference to the MICROSOFT CDO FOR EXCHANGE 2000 LIBRARY and use early binding:


Code:
Public Sub prcSendMail(pstrFrom As String, pstrRecipient As String, pstrSubject As String, pstrBody As String, Optional pstrAttachPath As String = "", Optional pstrCC As String = "", Optional pstrBCC As String = "") ' Send email using CDOEX/CDOSYS
  Const conCDOSendUsingPort As Integer = 2
  Const conCDOSMTPServer As String = "NAMEOFYOURSMTPSERVER"

  Dim objCDOConfig As CDO.Configuration
  Dim objCDOMessage As CDO.Message

  Set objCDOConfig = New CDO.Configuration
  objCDOConfig.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = conCDOSendUsingPort
  objCDOConfig.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = conCDOSMTPServer
  objCDOConfig.Fields.Update
  Set objCDOMessage = New CDO.Message
  Set objCDOMessage.Configuration = objCDOConfig
  objCDOMessage.AutoGenerateTextBody = True
  objCDOMessage.From = pstrFrom       ' The address listed as "From"
  objCDOMessage.To = pstrRecipient    ' The address listed as "To"
  objCDOMessage.Subject = pstrSubject ' Specify the subject header
  objCDOMessage.HTMLBody = pstrBody   ' Specify the body content
  If pstrAttachPath <> "" Then objCDOMessage.AddAttachment pstrAttachPath ' Attach specified file, if any
  If pstrCC <> "" Then objCDOMessage.CC = pstrCC
  If pstrBCC <> "" Then objCDOMessage.BCC = pstrBCC
  objCDOMessage.Send
  Set objCDOMessage = Nothing
  Set objCDOConfig = Nothing
End Sub
 

Users who are viewing this thread

Back
Top Bottom