Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Emailing attachments with different display name

Status
Not open for further replies.

mkrausnick

Programmer
Apr 2, 2002
766
US
I currently email from VFP8 using Outlook, which allows me to specify a display name for each attachment. For example, the attachment filename might be UTILIZATION_CM200501.PDF, but when the email is received, the user would see it as "January 2005 Current Month Utilization".

Now I want to use CDO in place of Outlook. the ADDATTACHMENT method seems to take only a filename. Is there a way to specify a display name different from the filename using CDO, and if so, what would the syntax be?

Thanks,

Mike Krausnick
Dublin, California
 
Try viewing the source to an email (with a display named attachment) to yourself, then use "view source" to see what you really got. I don't know how outlook does it, but I'd guess that it uses HTML in the email with an A HREF= anchor tag to refer to the attached file. Then, you can do that yourself (but you'll have to figure out how to send HTML email with CDO... )

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
I don't think that's the answer, because I'm sending the emails in text mode, as some of the recipients may have email that doesn't support HTML.

In Outlook, DisplayName is a property of the ATTACHMENT object, as is FileName and PathName. So to add an attachment I just execute:
Code:
oMsg.Attachments.Add(FileName.Ext,1,0,UserFileDescription)
'1' being the attachment type and '0' being the position within the body text where to place the attachment.

The CDO object model talks about 'BODYPARTS', which I am familiar with only in the context of Frankenstein movies. :)

Mike Krausnick
Dublin, California
 
An email sent with SMTP and attachments usually uses MIME to encode both the email and the attachments.

MIME basically is a way of dividing the "body" of the email into pieces, each piece including a short header describing the type of data in that piece, and sometimes a display name for it, and perhaps the length of the part, but most importantly, the string which marks the end of the piece.


- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
OK, but I'm not sure how that helps me.

Mike Krausnick
Dublin, California
 
I don't use CDO (because I can't be guaranteed that it will be available on my clients' machines) but your statement: The CDO object model talks about 'BODYPARTS' indicates that CDO may allow you to address each of these MIME segments of the message.

I thought some explanation might nudge you along in the right direction. The display name seems to be specified in the "Name" attribute of the first line of the MIME header for each Body Part, but I don't know how (or if) CDO exposes this to you.

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Thanks for your help. I reviewed your excellent FAQ on SMTP email in the general coding forum. It's clear that I need to do more research.


Mike Krausnick
Dublin, California
 
Thanks for the compliment!

Thanks for the compliment!

I Looked a little closer at the CDO documents on MSDN (and I couldn't find any reference to the "AddAttachment" that you mention... instead it seems that CDO uses the .Add method of the .Attachments collection on a message object.
It seems that you want to create an attachment object, then change its "name" property.

Basing this on Mike Gagnon's faq1251-4969, and the MSDN articles cited above, you could do something like this following code, however, this code doesn't work with the version of CDO that is on my computer... it seems to be mixing versions of CDO. So, look more at the second code sample below...

Code:
iMsg = Createobject("CDO.Message")
iConf = Createobject("CDO.Configuration")
Flds = iConf.Fields
With Flds
  .Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
  .Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = 'Your SMTP server name here'
  .Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
  .Update
Endwith
With iMsg
  .Configuration = iConf
  .To = "me@hotmail.com"
  .CC = ""
  .BCC = ""
  .From = "me@somewhere.com"
  .Subject = "This is a test"
  .TextBody = "This is the body text"
  
  oAttach = .Attachments.Add()
  oAttach.Source = "C:\local path\UTILIZATION_CM200501.PDF"
  oAttach.Name = "January 2005 Current Month Utilization.pdf"
  
  * or use this:
  * .Attachments.Add( "January 2005 Current Month Utilization.pdf",,,"C:\local path\UTILIZATION_CM200501.PDF")
  
  .Send
ENDWITH
oAttach = .NULL.
iMsg = .Null.
iConf = .Null.

I looked further, and found that instead of using CDO.Message, if I used CDONTS.Session as a starting point, I got objects that more closely matched the MSDN documents.

So, this seems to work:
Code:
  objSession = CreateObject ("CDONTS.Session")
  * I don't know what you need to specify to logon successfully....
  objSession.LogonSMTP('me','me@here.com')
  
  objOutBox = objSession.Outbox()
  oMsg = objOutbox.Messages.add()
  oAttch = oMsg.Attachments.Add
  oAttch.Name = 'whatever'
  oAttach.Source = "C:\local path\UTILIZATION_CM200501.PDF"
  oMsg.Send

I hope this gets you a bit further...

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top