Cannot send mail through SMTP from VB6 program - vb6

I have a legacy VB6 codebase which I would like to extend to include support for sending mails through an external SMTP server (smtp.live.com).
I use CDO for sending the mail. My machine runs Windows 7.
Unfortunately I get a "The transport failed to connect to the server" error message when trying to send send the mail.
Below is the code.
VB6
Dim oNewMessage As CDO.Message
Dim iConf As New CDO.Configuration
Dim oFlds As ADODB.Fields
Dim strbody As String
On Error GoTo errSMPT
iConf.Load cdoDefaults
Set oFlds = iConf.Fields
oFlds(cdoSendUsingMethod) = cdoSendUsingPort
oFlds(cdoSMTPServer) = "smtp.live.com"
oFlds(cdoSMTPServerPort) = 587
oFlds(cdoSMTPConnectionTimeout) = 30
oFlds(cdoSMTPUseSSL) = True
oFlds(cdoSMTPAuthenticate) = cdoBasic
oFlds(cdoSendUserName) = "xxxxxx#hotmail.com"
oFlds(cdoSendPassword) = "mypassword"
oFlds.Update
strbody = "Sample message " & Time
Set oNewMessage = New CDO.Message
Set oNewMessage.Configuration = iConf
With oNewMessage
.To = txtTo.Text
.From = txtFrom.Text
.Subject = "subject"
.TextBody = strbody
.Send
End With
Exit Sub
errSMPT:
MsgBox Err.Description
I don't think that the problem is related to firewall or account security issues since the C# code below works without any problems.
C#
using (MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtText.Text))
{
SmtpClient mailClient = new SmtpClient("smtp.live.com", 587);
mailClient.Credentials = new System.Net.NetworkCredential("xxxxxx#hotmail.com", "mypassword");
mailClient.EnableSsl = true;
mailClient.Send(message);
MessageBox.Show("Message successfully sent!!!");
}
Any help is appreciated!
Thanks
//Peter

I think your problem is here:
oFlds(cdoSMTPUseSSL) = True
This should be an integer instead of a boolean. When VB6 converts true to and int, the value is -1. I suggest you change that line to:
oFlds(cdoSMTPUseSSL) = 1

Related

Why cannot CDO Object for SMTP accept Mail.Sender.SMTPAddress object as .to property?

I am reading senduser emails with Redemption library and trying to send the email through SMTP CDO.Object.
I have been receiving following error, as long as I get the email for "EX" senderEmailType through Mail.Sender.SMTPAddress: CDO.Message.1: The transport lost its connection to the server.
Here is my code snippet (will lead to an error as long as mailReceiver argument is taken through Mail.Sender.SMTPAddress Redemption property - even converted to string it leads to the same error - cStr(Mail.Sender.SMTPAddress)) Although when I change the mailReceiver Property to the same email as string - it does not generate any error like this (maiLReceiver = "xx#like.com"):
sub sendMailSMTP(mailSubject, mailSender, mailReceiver, CC, BCC, replyTo, HTMLBody, attachmentsPath_List, REMOTE_SMTP, SMTP, PORT_SMTP, SCHEMAS)
HTML_CID = ""
set CDOObject = CreateObject("CDO.Message")
with CDOObject
.subject = mailSubject
.from = mailSender
.to = mailReceiver
.CC = CC
.BCC = BCC
.replyTo = replyTo
.HTMLBody = HTMLBody & HTML_CID
.TextBodyPart.Charset = "utf-8"
.AddAttachment LOGO_MAIL
.Attachments(1).Fields.Item("urn:schemas:mailheader:content-disposition") ="attachment;filename="& replace(split(LOGO_MAIL, "\")(ubound(split(LOGO_MAIL, "\"))),".jpg","")
.Attachments(1).Fields.Update
for each attachmentPath in attachmentsPath_List
.AddAttachment attachmentPath
next
end with
with CDOObject.Configuration.Fields
.Item(SCHEMAS & "sendusing") = REMOTE_SMTP
.Item(SCHEMAS & "smtpserver") = SMTP
.Item(SCHEMAS & "smtpserverport") = PORT_SMTP
.Item(SCHEMAS & "smtpconnectiontimeout") = 60
.Update
end with
CDOObject.Send
set CDOObject = Nothing
end sub
Could anyone point me to the right direction, what may be the reason for that?

Connecting to IBM MQ from UFT

I have been trying to read and write messages to MQ from UFT. I am using the dotnet factory instance. i have reached till a point where i am able to connect to MQ while i am facing a problem in accessing the queue and read and write messages.
The code is as follows.
strQMgrName = "queue manager name"
strMQMDllPath = "C:\\Program Files (x86)\\IBM\WebSphere MQ\\bin\\amqmdnet.dll"
Set oMqEnvironment = DotNetFactory.CreateInstance("IBM.WMQ.MQEnvironment",strMQMDllPath)
oMqEnvironment.Hostname = "host name"
oMqEnvironment.Port = "port number"
oMqEnvironment.Channel = "channel name"
Set oMQC = DotNetFactory.CreateInstance("IBM.WMQ.MQC",strMQMDllPath)
' qmanager name,channel name, connection name
Set oMqQMgr = DotNetFactory.CreateInstance("IBM.WMQ.MQQueueManager",strMQMDllPath,strQMgrName)
oMqQMgr.isConnected ' gives true
Now i wan to use the method
public MQQueue AccessQueue(string queueName, int openOptions)
of the instance IBM.WMQ.MQQueueManager. can someone guide me in doing the same and let me know how i can push messages and read messages from the mentioned queue
Thank you
After a lot of googling and reading IBM documentation , i was able to put and get messages in MQ from UFT. .. Below in the code snippet that worked for me .. Hope this helps some one .. I was working on PUT and GET separately, hence there might be some repetition in the code.
Prerequisite for this.. You have to download MQ Client from IBM site and install it on PC where UFT is installed.. IBM MQ client is a freeware
Dim oMQEnvironment
Dim oMQM
Dim oMQC
Dim oMQMessage
Dim oMQQueue
Dim intOpenOptions
strMQMDLLPath = "C:\\Program Files (x86)\\IBM\WebSphere MQ\\bin\\amqmdnet.dll"
strHostName= "Host Name"
intPort = "Port Number"
strChannel ="Channel Name"
strMessage ="UFT Test Message"
strMQManager = "Quemanager Name"
strMessageQueue = "Queue Name"
' This part of the code is blatantly copied from one of the posts online. Will post the link once i find it again
'for application(UFT) to connect to a queue manager in client mode
Set oMQEnvironment = DotNetFactory.CreateInstance("IBM.WMQ.MQEnvironment",strMQMDLLPath)
'Initize the Environment
With oMQEnvironment
.HostName = strHostName
.Port = intPort
.Channel = strChannel
End with
On Error Resume Next
'Create MQ Instatnces
Set oMQM = DotnetFactory.CreateInstance("IBM.WMQ.MQQueueManager",strMQMDLLPath)
'Check if MQM Connected
If Err.Number <> 0 Then
Reporter.ReportEvent micFail , "Step: Creating MQM Object" , "Unable to Connect to MQ Manager at" & strHostName
'Exit Test
End If
Set oMQC = DotnetFactory.CreateInstance("IBM.WMQ.MQC",strMQMDLLPath)
Set oMQMessage = DotnetFactory.CreateInstance("IBM.WMQ.MQMessage",strMQMDLLPath)
'Declare Q open options
intOpenOptions = oMQC.MQOO_OUTPUT or oMQC.MQOO_FAIL_IF_QUIESCING ' 16 + 8192
'Open the Q to post the messages
If strRemoteMQManager = "" Then
Set oMQQueue = oMQM.AccessQueue(strMessageQueue , intOpenOptions)
Else
Set oMQQueue = oMQM.AccessQueue(strMessageQueue , intOpenOptions ,strRemoteMQManager, "","" )
End If
'Format Message
With oMQMessage
.CharacterSet = 819
.WriteString(strMessage)
End with
'Post Message
With oMQQueue
.Put(oMQMessage)
.Close()
End With
Now for getting the message from MQ
Dim oMQEnvironment
Dim oMQM
Dim oMQC
Dim oMQMessage
Dim oMQQueue
strMQMDLLPath = "C:\\Program Files (x86)\\IBM\WebSphere MQ\\bin\\amqmdnet.dll"
strHostName= "host name"
intPort = "port number"
strChannel ="channel name"
strMessage ="UFT Test Message"
strMessageQueue = "message queue intended to access"
strMQManager = "mq manager name"
strRemoteMQManager=""
'Create MQ Instances
Set oMQC = DotnetFactory.CreateInstance("IBM.WMQ.MQC",strMQMDLLPath)
'set the properties of the Queue manager
Set properties = DotNetFactory.CreateInstance("System.Collections.Hashtable")
properties.Add oMQC.HOST_NAME_PROPERTY, strHostName
properties.Add oMQC.PORT_PROPERTY, intPort
properties.Add oMQC.CHANNEL_PROPERTY, strChannel
'access the queue manager
Set oMQM = DotnetFactory.CreateInstance("IBM.WMQ.MQQueueManager",strMQMDLLPath,strMQManager,properties)
'here We are trying to browse the message one by one and keep the messages on the queue.
'Declare Q open options
Set oMQQueue = oMQM.AccessQueue(strMessageQueue,oMQC.MQOO_BROWSE)
Set oMQGetMessageOptions = DotNetFactory.CreateInstance("IBM.WMQ.MQGetMessageOptions",strMQMDLLPath)
oMQGetMessageOptions.Options = oMQC.MQGMO_BROWSE_FIRST
Set oMQMessage = DotnetFactory.CreateInstance("IBM.WMQ.MQMessage",strMQMDLLPath)
oMQQueue.Get oMQMessage,oMQGetMessageOptions
Set mqGetNextMsgOpts = DotNetFactory.CreateInstance("IBM.WMQ.MQGetMessageOptions",strMQMDLLPath)
mqGetNextMsgOpts.Options = oMQC.MQGMO_BROWSE_NEXT
browseMessages = true
Do while browseMessages
on error resume next
messageText = oMQMessage.ReadString(oMQMessage.MessageLength)
'Print messageText
Set oMQMessage = DotnetFactory.CreateInstance("IBM.WMQ.MQMessage",strMQMDLLPath)
oMQQueue.Get oMQMessage,mqGetNextMsgOpts
if Err.Number <> 0 then browseMessages =false
'Clear both MsgID and CorrelID for next use.
oMQMessage.MessageId = oMQC.MQMI_NONE
oMQMessage.CorrelationId = oMQC.MQCI_NONE
Loop
'Cleanup
Set oMQQueue = Nothing
Set oMQMessage= Nothing
Set oMQOpenOptions= Nothing
Set oMQM= Nothing
Set oMQEnvironment = Nothing

How do I force VB6 to POST using TSL encryption?

The company where I work has an old VB6 application that they want to force to use TSL, rather than SSL. I looked at the code, and told them they should be fine. The code does a post to the client website using HTTPS. It doesn't specify what encryption to use.
This is the relevant code:
Sub PostXML()
Dim XMLHttpRequest As MSXML2.XMLHTTP
Dim TempString As String
Dim strURL As String
Dim strArgs As String
strURL = gPostWebServer & "/" & gPostFile
'ARB 1/8/2004 This is to trap if send fails and allow it to continue.
On Error GoTo errorHandler:
If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing
Set XMLHttpRequest = New MSXML2.XMLHTTP
strArgs = "?Username=" & gPostUserName & "&Password=" & gPostPassword
XMLHttpRequest.Open "POST", strURL & strArgs, False
XMLHttpRequest.send dom_GlobalXMLObject
If XMLHttpRequest.Status >= 400 And XMLHttpRequest.Status <= 599 Then
TempString = "Client Website is not available. Order was not posted successfully ..."
flgOrderPostSuccess = False
strOrderPostError = TempString
Else
TempString = XMLHttpRequest.responseText
'Parse the response
Dim sValid As String
Dim sComments As String
Dim sTimeStamp As String
Dim oRoot As MSXML2.IXMLDOMElement
Dim lNodes As MSXML2.IXMLDOMNodeList
Dim oNodes As MSXML2.IXMLDOMElement
Dim lNodes1 As MSXML2.IXMLDOMNodeList
Dim oNodes1 As MSXML2.IXMLDOMElement
Dim lNodes2 As MSXML2.IXMLDOMNodeList
Dim oNodes2 As MSXML2.IXMLDOMElement
Call Set_Global_XML_Object
dom_GlobalXMLObject.loadXML (TempString)
dom_GlobalXMLObject.Save (Report_Folder & "\Response.xml")
'Get the root of the XML tree.
Set oRoot = dom_GlobalXMLObject.documentElement
If Not oRoot Is Nothing Then
Set lNodes = oRoot.childNodes
For Each oNodes In lNodes
Select Case oNodes.nodeName
Case "Acknowledgement"
Set lNodes1 = oNodes.childNodes
For Each oNodes1 In lNodes1
Select Case oNodes1.nodeName
Case "Received"
sTimeStamp = Trim(oNodes1.nodeTypedValue)
Case "Validated"
sValid = Trim(oNodes1.nodeTypedValue)
Case "Errors"
Set lNodes2 = oNodes1.childNodes
For Each oNodes2 In lNodes2
Select Case oNodes2.nodeName
Case "Description"
sComments = sComments & vbCrLf & Trim(oNodes2.nodeTypedValue)
End Select
Set oNodes2 = Nothing
Next
Set lNodes2 = Nothing
End Select
Set oNodes1 = Nothing
Next
Set lNodes1 = Nothing
End Select
Next
If UCase(sValid) = "YES" Then
TempString = sTimeStamp & " " & "Order uploaded successfully"
flgOrderPostSuccess = True
strOrderPostError = ""
Else
TempString = "Order had following problems:" & vbCrLf
TempString = TempString & sComments
strOrderPostError = TempString
End If
Else 'Non XML response
TempString = Replace(TempString, vbCr, vbCrLf)
TempString = "Order had following problems:" & vbCrLf & TempString
strOrderPostError = TempString
End If
End If
Call FillLogTextBox("-----------------------------------------------" & vbCr)
Call FillLogTextBox(TempString)
Call FillLogTextBox("-----------------------------------------------" & vbCr)
Set oRoot = Nothing
Set lNodes = Nothing
Set oNodes = Nothing
Set lNodes1 = Nothing
Set oNodes1 = Nothing
Set lNodes2 = Nothing
Set oNodes2 = Nothing
Set XMLHttpRequest = Nothing
Exit Sub
errorHandler:
TempString = Err.DESCRIPTION
If InStr(1, TempString, "Method") > 0 Or InStr(1, Err.DESCRIPTION, "failed") > 0 Then
TempString = "Client Website was not found. Order was not posted successfully..."
Call FillLogTextBox(TempString)
Call FillLogTextBox("-----------------------------------------------" & vbCr)
Exit Sub
End If
End Sub
When the client switched from SSL to TSL last weekend, everything worked, except the posts from this one old VB6 app. (So I'm told, anyways. This isn't an application I've supported before.)
We have other VB6 apps that I maintain, but none do a POST out of VB6. All of them use BizTalk for posting.
The client has given us until next Wednesday to fix our app. So, the powers that be want me to force the app to use TSL.
Normally, I don't have problems with VB6, but I've never tried forcing the encryption used to POST. Generally, when we did POST out of the other VB6 apps, they negotiated with Windows on their own, and took care of things. While I've seen successful attempts to force VB6 to use TSL when sending an email, I've never seen anyone do it for POSTing.
All that being said, does anyone know how to force VB6 to use TSL when POSTing?
Thanks
With SChannel you cannot control available/used protocols and ciphers at an application level, you have to configure SChannel protocols/ciphers on the Win2003 box at system level. Here is KB on the subject: http://support.microsoft.com/kb/245030
To disable SSLv3 for both inbound and outbound connections merge something like this in registry (and reboot):
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0]
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\Client]
"DisabledByDefault"=dword:00000001
"Enabled"=dword:00000000
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\Server]
"Enabled"=dword:00000000
"DisabledByDefault"=dword:00000001
While there make sure SSLv2 is nuked too.
You might prefer to use IISCrypto -- a nice utility that makes SSL/TLS protocols/ciphers registry config trivial.

Connection String in Textbox

I'am new in programming and my problem is. i have put my ado db connection string into a text box how can i call that text box? i'm creating my program in vb 6 and here's my code.
Private Sub lvButtons_H2_Click()
On Error GoTo errtrap
If Label47.Caption = "True" Then
MsgBox "INITIAL SETTING FOR SHIP ACCOUNT IS BEING PERFORMED", vbOKOnly, "ABORT"
Exit Sub
End If
Dim conas As New ADODB.Connection, rs01 As New ADODB.Recordset, rsx1 As New ADODB.Recordset, RS9 As New ADODB.Recordset
conas.Connectio`enter code here`nString = Text1155.Text
conas.Open
Set RS9 = New ADODB.Recordset
RS9.ActiveConnection = conas
RS9.CursorType = 3
RS9.LockType = 3
RS9.Open ("SELECT * FROM [SHIPACCOUNT].[dbo].[SPARE PART LIST BOND 29 MONTHLY] WHERE NAMECODE = " & Text2.Text & "")
Set DataReport2.DataSource = RS9
DataReport2.Sections("Section2").Controls.item("LABEL12").Caption = Text1.Text
DataReport2.Sections("Section2").Controls.item("LABEL11").Caption = Text3.Text
DataReport2.Sections("Section1").Controls.item("TEXT1").DataField = RS9![PARTSNAME].Name
DataReport2.Sections("Section1").Controls.item("TEXT2").DataField = RS9![Price].Name
DataReport2.Sections("Section1").Controls.item("TEXT3").DataField = RS9![unit].Name
DataReport2.Sections("Section1").Controls.item("TEXT4").DataField = RS9![QTYAPPLY].Name
DataReport2.Sections("Section1").Controls.item("TEXT5").DataField = RS9!QTYAPPROVE.Name
DataReport2.Sections("Section1").Controls.item("TEXT6").DataField = RS9![AMOUNTAPPROVE].Name
DataReport2.Sections("Section1").Controls.item("TEXT7").DataField = RS9![Date].Name
DataReport2.Show 1
Exit Sub
errtrap:
MsgBox Err.Description, vbCritical, "The system encountered an error"
End Sub
You can pass the connection string as parameter to the Connection.Open method
Such as (assuming the name of the textbox is Text1155):
Dim conas As New ADODB.Connection
conas.Open Text1155.Text
(You don't need parenthesis for calling a Sub in vb6)
Your code looks right otherwize...

how to create group email with CDO Using VB6

How can I send an email to a group of recipients with CDO? I'm using VB6.
You can list multiple recipients on the .To line by separating them with ";", for example:
Set m = Server.CreateObject("CDO.Message")
m.Subject="subject..."
m.From="sender#example.com"
m.To="some#email.com;other#email.com;third#email.com"
m.TextBody="Message"
m.Send
This works in Office 97 and whatever Exchange we had back then:
Dim oOApp As Outlook.Application
Dim newMail As Outlook.MailItem
Set oOApp = CreateObject("Outlook.Application")
Set newMail = oOApp.CreateItem(olMailItem)
With newMail
.Display
.Body = whatever
.Subject = whatever
.Attachments.Add whatever
.Recipients.Add (whomever)
.Send
End With
Set newMail = Nothing
Set oOApp = Nothing

Resources