Wiki Home

Send Hotmail Message From FoxPro


Namespace: Wiki
Hotmail protocol is undocumented, at let I could not find anything like a white paper on Microsoft sites describing this protocol. Fortunately enough information exist on the Net showing how to use this protocol -- search Http Mail on

I'd guess that the protocol should be pretty stable, though, since OutlookExpress can use this protocol to send/receive emails. -- ?wgcs

An article written by Kais Dukes Hotmail Using C# A HTTPMail Client Under .NET helped me a lot. A few other sources were useful -- this problem has been already solved in VB and Delphi communities. So why don't we? :)

Also a tremendous help was the EtherDetect packet sniffer -- a kind of software I have never used before -- you can easily see and understand how HTTP requests are processed.

The following is a sample code allowing you to send an email message from either Hotmail or MSN account. It worked properly with software installed:
- Visual FoxPro 06.00.8961.00 for Windows
- Internet Explorer 6.0.2800.1106.xpsp2.030422-1633
- WinXP, both Home and Pro versions.

Unfortunately I was not successfull trying to run this code on Windows Me. Whoever can do that please share your experience.

If line oHttp = CreateObject('MSXML2.ServerXMLHTTP') creates an error you might try to install MSXML 4.0 Service Pack 2.

* provide valid Hotmail or MSN account
DO HotmailSend WITH;
	'[email protected]', 'mypassword',;
	'[email protected]',;
	'Check this email', 'This is a test message sent from Hotmail account.'

And here is the procedure:
PROCEDURE HotmailSend(cEmail, cPwd, cRecipient, cSubject, cMsgBody)
#DEFINE cret Chr(13)+Chr(10)

* MSN and Hotmail server URLs
#DEFINE urlMsn     "http://oe.msn.msnmail.hotmail.com/cgi-bin/hmdata"
#DEFINE urlHotmail "http://services.msn.com/svcs/hotmail/httpmail.asp"

#DEFINE ccXmlFolders '<'+'?xml version="1.0"?'+'>' + cret +;
'<'+'D:propfind xmlns:D="DAV:" xmlns:h="http://schemas.microsoft.com/hotmail/" ' +;
'xmlns:hm="urn:schemas:httpmail:">' + cret +;
'	<'+'D:prop'+'>' + cret +;
'		<'+'h:adbar/'+'>' + cret +;
'		<'+'hm:contacts/'+'>' + cret +;
'		<'+'hm:inbox/'+'>' + cret +;
'		<'+'hm:outbox/'+'>' + cret +;
'		<'+'hm:sendmsg/'+'>' + cret +;
'		<'+'hm:sentitems/'+'>' + cret +;
'		<'+'hm:deleteditems/'+'>' + cret +;
'		<'+'hm:drafts/'+'>' + cret +;
'		<'+'hm:msgfolderroot/'+'>' + cret +;
'		<'+'h:maxpoll/'+'>' + cret +;
'		<'+'h:sig/'+'>' + cret +;
'	<'+'/D:prop'+'>' + cret +;
'<'+'/D:propfind'+'>'

#DEFINE ccSendmsgNode '/D:multistatus/D:response/D:propstat/D:prop/hm:sendmsg'

	LOCAL cServer, oHttp, nRespCode

	DO CASE
	CASE '@hotmail.com' $ LOWER(cEmail)
		cServer = urlHotmail
	CASE '@msn.com' $ LOWER(cEmail)
		cServer = urlMsn
	OTHER
		? 'Invalid Hotmail account.'
		RETURN
	ENDCASE

	oHttp = CreateObject('MSXML2.ServerXMLHTTP')
	WITH oHttp
		.Open("PROPFIND", cServer, 0, cEmail, cPwd)
		.Send(ccXmlFolders)
		nRespCode = VAL(SUBSTR(.GetResponseHeader('X-Dav-Error'), 1,3))
	ENDWITH

	IF nRespCode <> 200
		? 'Could not connect to Hotmail account.'
		? 'Request status: ' + LTRIM(STR(oHttp.Status)) + ', ' +;
			oHttp.StatusText
		RETURN
	ENDIF

	LOCAL oXml, cSendmsgHref, cMessage
	oXml = CreateObject('Microsoft.XMLDOM')
	oXml.Load(oHttp.ResponseXml)
	cSendmsgHref = oXml.SelectSingleNode(ccSendmsgNode).Text

	IF EMPTY(cSendmsgHref)
		? 'Could not obtain SendMsg url.'
		RETURN
	ENDIF
	
	* formatting message
	LOCAL cMessage
	cMessage = 'MAIL FROM:<' + cEmail + '>' + cret +;
		'RCPT TO:<' + cRecipient + '>' + cret +;
		'' + cret +;
		'From: <' + cEmail + '>' + cret +;
		'To: <' + cRecipient + '>' + cret +;
		'Subject: ' + cSubject + cret +;
		'Date: ' + TTOC(datetime()) + cret +;
		'MIME-Version: 1.0' + cret +;
		'Content-Type: text/plain;' + cret +;
		'X-Mailer: Visual FoxPro applicaiton' + cret +;
		'' + cret +;
		cMsgBody

	WITH oHttp
		.Open("POST", cSendmsgHref, 0)
		.SetRequestHeader('Content-Type', 'message/rfc821')
		.SetRequestHeader('SAVEINSENT', 'f')  && do not save in Sent Messages folder
		.Send(cMessage)
		nRespCode = VAL(SUBSTR(.GetResponseHeader('X-Dav-Error'), 1,3))
	ENDWITH
	
	IF nRespCode <> 200
		? 'Message has not been sent.'
		? 'Request status: ' + LTRIM(STR(oHttp.Status)) + ', ' +;
			oHttp.StatusText
		? oHttp.GetAllResponseHeaders()
	ELSE
		? 'Message has been sent successfully.'
	ENDIF

-- Anatoliy Mogylevets
It seems great, just one question, must we trust in this service?, we are going to send hotmail password to someone who I didn't know.... Esparta Palma
You do not send your password to anyone else except to the Microsoft Hotmail server, and their Xml Http object stands behind all of this. When you access a Hotmail account using Outlook Express, it does exactly the same -- same HTTP requests in same order, a password is encrypted every time.
-- Anatoliy Mogylevets
Just Great again!, now the 1 Million Question, HOWTO Attach files? -- Esparta Palma
I'll add my 2cents: if someone figures out how to attach files, I'll figure out how to remove the need for the MS XML parser.... - wgcs
i tried using the above but no success.
error returned was

HTTP/1.1 503 Fatal Error Occurred
Server: Microsoft-IIS/5.0
Date: Sun, 28 Dec 2003 15:21:35 GMT
P3P:CP="BUS CUR CONo FIN IVDo ONL OUR PHY SAMo TELo"
Cache-Control: private
X-Dav-Error: 901 Fatal Error
HMServer: H: sea2-dav34.sea2.hotmail.com V: WIN2K 09.08.01.0004 i D: Dec 17 2003 12:19:22 S: 0
--suhashegde
503. Service Unavailable. The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
--AnatoliyMogylevets
I also noticed some other things.
i was able to send mail from another hotmail account.
My original account had two custom folders and may be that was the real problem. even though i have deleted the custom folders i am not been able to send the mails.
--suhashegde
See also: Automated Email
Category Code Samples

When I tried I got the following message:

Could not connect to Hotmail account.
Request status: 402, Payment Required

Can anyone figure out what is this?
Unfortunately 2-3 years ago MS closed that door for free Hotmail/MSN email accounts -- Anatoliy Mogylevets
( Topic last updated: 2007.10.08 11:29:11 PM )