Link to home
Start Free TrialLog in
Avatar of LoG
LoG

asked on

Specify a SMTP server to send mail to via VBS

I need your help to create an email generation VBscript.  In this script, I will be parsing a text document for information to include in the body of the outgoing emails.  Parsing the text document will be very straight forward, but the problem comes when I need to generate the email message.  I do not have an in-house mail or IIS server available.  So, I need to find out how to use VBS to send an email message to a specified mail server (either by IP or DNS name).  To connect to this mail server I will have to pass credentials so that they do not think I am trying to relay.  Anyone here that knows how to do this?

To recap, the solution must:  
Work under VBscript (possibly WSH)
Send credentials to an outside ISP SMTP server
Send the email to this mail server
Require no third party software
Run without ASP

The reward, 200 points!
Avatar of marko020397
marko020397

Take a look at CDO and CDONTS components included with Windows. You will find a lot of examples. Even on Experts Exchange: https://www.experts-exchange.com/questions/11016941/Sending-E-mail-in-VBS-SMTP.html
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

This is what I've used in the past in VB.  I don't know about the credentials piece, it has always sent for me without a problem though.

   
    Const sBodyCRLF As String = "%0d%0a"
    sEMails = "somebody@somemailaddress.com"
   
    sEmailText = "Email Text"
       
    sEmailText = sEmailText & sBodyCRLF & "Another Line of Email Text"
   
    sSubjectLine = "My SubjectLine"
       
   
    Call SendMail(sEMails, "", "", sSubjectLine, sEmailText)
   

Sub SendMail(ByVal strAddress As String, _
Optional ByVal strCC As String, _
Optional ByVal strBCC As String, _
Optional ByVal strSubject As String, _
Optional ByVal strBodyText As String)
    Dim strTemp As String
   
    If Trim(Len(strCC)) Then
    strTemp = "&CC=" & strCC
    End If
   
    If Trim(Len(strBCC)) Then
    strTemp = strTemp & "&BCC=" & strBCC
    End If
   
    If Trim(Len(strSubject)) Then
    strTemp = strTemp & "&Subject=" & strSubject
    End If
   
    If Trim(Len(strBodyText)) Then
    strTemp = strTemp & "&Body=" & strBodyText
    End If
   
    If Len(strTemp) Then
    Mid(strTemp, 1, 1) = "?"
    End If
   
    strTemp = "mailto:" & strAddress & strTemp
   
    ShellExecute 0, "open", strTemp, 0, 0, SW_NORMAL
   
End Sub
Avatar of LoG

ASKER

I am sure this code works just fine for VB, but this script will not be compiled...  Thus the mystery of how this will work...  This must be a VBS or WSH script that will work on any PC Win98 or higher.

I guess I should also clarify that there is currently no mail client being used on this PC.  So, this needs to send a basic SMTP message directly to an SMTP server at the ISP.  In order for the email to go through this server, the script must pass login credentials so that the server does not think this is a relay script (it is not).

If you would like to propose a solution using a MAPI mail program as the sending agent, I will consider that for a solution if it is all done in a script as mentioned above.

(This is not a simple task from what I have tried so far)
Avatar of LoG

ASKER

I am sure this code works just fine for VB, but this script will not be compiled...  Thus the mystery of how this will work...  This must be a VBS or WSH script that will work on any PC Win98 or higher.

I guess I should also clarify that there is currently no mail client being used on this PC.  So, this needs to send a basic SMTP message directly to an SMTP server at the ISP.  In order for the email to go through this server, the script must pass login credentials so that the server does not think this is a relay script (it is not).

If you would like to propose a solution using a MAPI mail program as the sending agent, I will consider that for a solution if it is all done in a script as mentioned above.

(This is not a simple task from what I have tried so far)
Avatar of LoG

ASKER

I am sure this code works just fine for VB, but this script will not be compiled...  Thus the mystery of how this will work...  This must be a VBS or WSH script that will work on any PC Win98 or higher.

I guess I should also clarify that there is currently no mail client being used on this PC.  So, this needs to send a basic SMTP message directly to an SMTP server at the ISP.  In order for the email to go through this server, the script must pass login credentials so that the server does not think this is a relay script (it is not).

If you would like to propose a solution using a MAPI mail program as the sending agent, I will consider that for a solution if it is all done in a script as mentioned above.

(This is not a simple task from what I have tried so far)
ASKER CERTIFIED SOLUTION
Avatar of twalgrave
twalgrave

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- answered by twalgrave
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

twalgrave
Cleanup Volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
Is it possibel to send me a code to use the MAPI Client or outlook express to send the mail alert. Thanks.