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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook automation using javascript 1

Status
Not open for further replies.

amigo02

Programmer
Feb 19, 2003
109
Hi there

I want to attach a file to outlook email using javascript within a web page. The following code works fine without attachments.

var outlookApp = new ActiveXObject("Outlook.Application");
var nameSpace = outlookApp.getNameSpace("MAPI");
mailFolder = nameSpace.getDefaultFolder(6);
mailItem = mailFolder.Items.add('IPM.Note.FormA');
mailItem.Subject="CUSTOMER SERVICE";
mailItem.To = document.frmM.txtEmail.value;
mailItem.HTMLBody = "SOME HTML CODE";
mailItem.display (0);

What I really want to do is attaching a file to this email message, so when I add the following line into the code above it should work fine but it does not.

mailItem.Attachments.Add = "C:\temp\xxx.txt"

this line creates an error. How can I do this?
 
amigo02,

Add is a method rather than a property. Try this?
[tt]
mailitem.Attachements.Add ("C:\temp\xxx.txt");
[/tt]
regards - tsuji
 
Amendment:

It should be escaped as usual.
mailitem.Attachements.Add ("C:\\temp\\xxx.txt");

- tsuji
 
Thank you tsuji , it worked fine..
 
Also I have another question!

How to embed images into emails instead of sending a link to it. (using outlook active x and javascript)
 
amigo02,

You can embed the image in the htmlbody and have the image file as attachment. By then, the image would keep the reference to source valid.
[tt]
mailItem.HTMLBody = "<html><body><img src='somepic.jpg'></body></html>";
mailItem.Attachments.Add ("c:\\temp\\somepic.jpg"); //your local source path here
[/tt]
- tsuji
 
tsuji thanks for the very valuable information you provided..

How can I copy the image file from web server(intranet) to user's temporary folder so I can attach it to the email.. Or better yet how to attach images from server directly into emails in javascript and outlook active x .
 
amigo02,

You can use xmlhttp component to get it. To demo, do this.
Code:
adTypeBinary=1;
adSaveCreateOverwrite=2;
adModeReadWrite=3;
var sSource = "[URL unfurl="true"]http://tek-tips.com/images/partnerbtn120x60.gif";[/URL]
var sTarget="c:\\temp\\somepic.gif";
try {
    var oHTTP=new ActiveXObject("Microsoft.XMLHTTP");
    oHTTP.open ("GET", sSource, false);
    oHTTP.send();
    var ostream=new ActiveXObject("adodb.stream");
    with (ostream) {
        type=adTypeBinary;
        mode=adModeReadWrite;
        adModeReadWrite=3
        open();
        write(oHTTP.responseBody);
        savetofile(sTarget, adSaveCreateOverwrite);
        close();
} catch(e) {
    //error message, using such as alert() or logfile
    //error may be due to multiple reasons, not scrutinize here
}
ostream=null;
oHTTP=null;
- tsuji
 
Correction:

I missed out a closing bracket here.
[tt]
with (ostream) {
type=adTypeBinary;
mode=adModeReadWrite;
adModeReadWrite=3
open();
write(oHTTP.responseBody);
savetofile(sTarget, adSaveCreateOverwrite);
close();
[red]}[/red]
[/tt]
- tsuji
 
yet another question?

When I follow your instructions the image is displayed in outlook without problem. But if I send the html page as an attachment instead of in the body of the email internet explorer does not display the image when the attached file opened by double clicking the attachment in outlook, even though the image file is there as a seperate attachment as well.
 
amigo02,

The simplest is to furnish the cid (compatibility id) to the source. Something like this, if I use your convention.
[tt]
var outlookApp = new ActiveXObject("Outlook.Application");
var nameSpace = outlookApp.getNameSpace("MAPI");
var mailFolder = nameSpace.getDefaultFolder(6);
var mailItem = mailFolder.Items.add('IPM.Note.FormA');
with (mailItem) {
Subject="CUSTOMER SERVICE";
To = document.frmM.txtEmail.value;
Attachments.Add ("c:\\temp\\somepic.jpg");
HTMLBody = "<html><body><p>some body script</p><img src='cid:'somepic.jpg'></body></html>";
Display (0);
//Send();
}
mailItem=null;
mailFolder=null;
nameSpace=null;
outlookApp=null;
[/tt]
The slight drawback is that the scr file is still appeared as the attachment and is not fully integrated into the mail. To make improvement in this direction involves quite a bit of detours. Also, it seems to me that .net technology provision standard builtin method to simplify the construction-I cannot be 100% certain here.

- tsuji
 
Correction (typo)
[tt]src='cid:somepic.jpg'[/tt]
(one single-quote too many)
- tsuji
 
I tried src='cid:somepic.gif' solution but no go!

May be I was not clear enough on what I meant . I give you the code below;

var outlookApp = new ActiveXObject("Outlook.Application");
var nameSpace = outlookApp.getNameSpace("MAPI");
var mailFolder = nameSpace.getDefaultFolder(6);
var mailItem = mailFolder.Items.add('IPM.Note.FormA');
with (mailItem) {
Subject="CUSTOMER SERVICE";
To = document.frmM.txtEmail.value;
Attachments.Add ("c:\\temp\\somepic.jpg");
Attachments.Add ("C:\\temp\\somehtml.html");
HTMLBody = "Some Text here"
Display (0);
//Send();
}
mailItem=null;
mailFolder=null;
nameSpace=null;
outlookApp=null;


When I open the email there are two attachments. One for the Logo(image file) and one for the html page. When I click to open the html page, internet explorer does not display the Logo but the rest of the page appears normal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top