Monday, August 08, 2005

Sending Read and Delivery Recipients programmatically via Exchange

Someone asked me last week about sending a delivery recipient programmatically via OWA which is a bit of interesting one so I thought I’d share. There is a good KB article that gives a description of how to send read and delivery recipient via CDOSYS (and CDOEX) using the urn:schemas:mailheader:disposition-notification-to (for read recipients) and urn:schemas:mailheader:return-receipt-to (for delivery recipients). If your sending via the OWA send command you need to include two different properties

"http://schemas.microsoft.com/exchange/readreceiptrequested=1"
"http://schemas.microsoft.com/exchange/deliveryreportrequested=1"

If you are sending via WebDAV for example using this code from the SDK . You need to make the following mods

Declare another string eg
string strNotifyaddr = "\"Fred Smith\""<fsmith@domain.com>";

And then modify the header string to include disposition-notification-to and Return-Receipt-To eg

strMailboxURI = "http://" + strServer + "/exchange/" + strAlias;
strSubURI = "http://" + strServer + "/exchange/" + strAlias
+ "/##DavMailSubmissionURI##/";
strTempURI = "http://" + strServer + "/exchange/" + strAlias
+ "/drafts/" + strSubject + ".eml";
strBody = "To: " + strTo + "\n" +
"Subject: " + strSubject + "\n" +
"Date: " + System.DateTime.Now +
"X-Mailer: test mailer" + "\n" +
"MIME-Version: 1.0" + "\n" +
"Content-Type: text/plain;" + "\n" +
"disposition-notification-to: " + strNotifyaddr + "\n" +
"Return-Receipt-To: " + strNotifyaddr + "\n" +
"Charset = \"iso-8859-1\"" + "\n" +
"Content-Transfer-Encoding: 7bit" + "\n" +
"\n" + strText;


I’ve put together two samples for sending a mail with read recipients via OWA and posted the code here. The difference between both samples is the first is for if you aren’t using Forms based authentication on your Exchange box and the second is a sample for if you are using FBA on your Exchange box (this includes code to deal with cookies). The normal OWA send code look like

stmsgbody ="Hello Fred <BR<BR>"
stmsgbody = stmsgbody & "What to get some coffee Mate, Meet me downstairs in
ten<BR><BR>"
stmsgbody = stmsgbody & "Cheers<BR>"
stmsgbody = stmsgbody & "Barney<BR>"
szXml = ""
szXml = szXml & "Cmd=send" & vbLf
szXml = szXml & "MsgTo=address@domain.com" & vbLf
szXml = szXml & "MsgCc=" & vbLf
szXml = szXml & "MsgBcc=" & vbLf
szXml = szXml & "urn:schemas:httpmail:importance=1" & vbLf
szXml = szXml & "http://schemas.microsoft.com/exchange/readreceiptrequested=1" &
vbLf
szXml = szXml &
"http://schemas.microsoft.com/exchange/deliveryreportrequested=1" & vbLf
szXml = szXml & "http://schemas.microsoft.com/exchange/sensitivity-long=" & vbLf
szXml = szXml & "urn:schemas:httpmail:subject=Coffee ???" & vbLf
szXml = szXml & "urn:schemas:httpmail:htmldescription=<!DOCTYPE HTML PUBLIC " _
& """-//W3C//DTD HTML 4.0 Transitional//EN""><HTML DIR=ltr><HEAD><META
HTTP-EQUIV" _
& "=""Content-Type"" CONTENT=""text/html; charset=utf-8""></HEAD><BODY><DIV>" _
& "<FONT face='Arial' color=#000000 size=2>" & stmsgbody & "</font>" _
& "</DIV></BODY></HTML>" & vbLf
Set ObjxmlHttp = CreateObject("Microsoft.XMLHTTP")
ObjxmlHttp.Open "POST", "https://server/exchange/mailbox/Drafts", False, "",""
ObjxmlHttp.setRequestHeader "Accept-Language:", "en-us"
ObjxmlHttp.setRequestHeader "Content-type:", "application/x-www-UTF8-encoded"
ObjxmlHttp.setRequestHeader "Content-Length:", Len(xmlstr)
ObjxmlHttp.Send szXml
Wscript.echo ObjxmlHttp.responseText

8 comments:

Anonymous said...

Hi Glen,

I have tried to use many samples (from MSDN and from other sites as well) in the past and left only with a "Login Timed Out" message.

I even tried with your samples and get an Invalid Procedure Call error.

I believe my exchange server is using FBA and not sending the session ID is the primary cause of the problem.

Even with your sample I get the following headers.

Content-Length
Expires
Date
Content-Type
Cache-Control
Server
X-Powered-By

Can you help me in any other way?

Thanks

Narayanan Sankaranarayanan

Narayanan Sankaranarayanan said...

Hi Glen,

I have tried to use many samples (from MSDN and from other sites as well) in the past and left only with a "Login Timed Out" message.

I even tried with your samples and get an Invalid Procedure Call error.

I believe my exchange server is using FBA and not sending the session ID is the primary cause of the problem.

Even with your sample I get the following headers.

Content-Length
Expires
Date
Content-Type
Cache-Control
Server
X-Powered-By

Can you help me in any other way?

Thanks

Narayanan Sankaranarayanan

Glen said...

You might want to check your Internet explorer configuration. Because this script uses the browser control if you have disable Cookies in IE then these scripts and others will never work because cookies are essential to getting FBA to work. If your doing a getallresponseheaders you should be getting a cookie returned. You may also want to use something like etheral to try and do a capture of the logon process so you can see the exact url's that are being used maybe you not putiing in the correct domain\username and password format in and its timeouting because of a password error. Also check the event logs on you server unsuccessfull logons are usually recorded by IIS in your event logs.

Anonymous said...

Hi Glen,

well I am able to to send the mail with retunr reciept. But after reading mail, which attribute we need to change so that if user reads the mail again and again then Exchange won't send a return reciept for each.

I would like to hear response from your side.

Regards,
Will Turner

Anonymous said...

How do I disable the dilevery receipt from the server?

Thanks
walia
kswalia@eim.ae

Glen said...

There's no method to disable delivery reports at the server that in know of. (What reports are your talking about anyway normal delivery reports or NDR's ?). You may be able to do this by writing a transport event sink that modified the headers in the message. See The Exchange SDK for more details on Transport event sinks. You have to be carefull disabling delivery reports means you server would no longer be RFC compliant.

Cheers
Glen

SamirMarwaha said...

Hey Glen,

I am trying to use the above scripts to send test email from CAS servers (FBA) to test OWA functionality but seems i am not successfully able to run. Do you have any tested scripts that i can use on E2K7 CAS servers?

thanks

Glen said...

This is for Exchange 2003 only because it uses specific OWA 2003 cmds that dont exist on 2007. If you want to send mail programatically on 2007 then i would suggest using EWS see http://msdn.microsoft.com/en-us/library/aa563049.aspx

Cheers
Glen