Board index » cppbuilder » Attachment missing from email

Attachment missing from email


2007-09-07 12:53:24 AM
cppbuilder79
I am trying to add the ability to email error logs from the help screen of an application. I have a simple test form with a TIdSMTP component and a TIdMessage component. I can send an email but I can't get the attachment to be included. I'm using the following test code in Borland C++ Builder 6, using Indy 9.0.50.
int TMainForm::TestFunction(void)
{
AnsiString FileToAttach = "C:\\Link\\Logs\\Logs.zip";
if (FileExists(FileToAttach))
{
Log->Lines->Add("Attaching file " + FileToAttach);
}
else
{
Log->Lines->Add("File <" + FileToAttach + ">does not exist.");
}
TDMPCursor MyCursor(crHourGlass);
// MailMessage is a TIdMessage component on the main form.
MailMessage->ContentType = "multipart/related; type=\"text/html\"";
MailMessage->Body->Add("With attachment.");
TIdAttachment* LogFile = new TIdAttachment(MailMessage->MessageParts, FileToAttach);
LogFile->FileName = FileToAttach;
//LogFile->ContentType = GetMIMETypeFromFile(LogFile->FileName);
LogFile->ContentType = "application/zip";
try
{
// SMTPClient is a TIdSMTP component on the main form.
SMTPClient->Connect();
SMTPClient->Send(MailMessage);
}
__finally
{
SMTPClient->Disconnect();
}
Log->Lines->Add("TestFunction Completed: " + AnsiString(""));
return 0;
}
What am I missing?
Royce
 
 

Re:Attachment missing from email

"Royce Fessenden" < XXXX@XXXXX.COM >wrote in message
Quote
I can send an email but I can't get the attachment to be included.
That is because you are putting the text in the wrong place and setting the
wrong ContentType for the message. Try this instead:
int TMainForm::TestFunction(void)
{
AnsiString FileToAttach = "C:\\Link\\Logs\\Logs.zip";
if( !FileExists(FileToAttach) )
{
Log->Lines->Add("File <" + FileToAttach + ">does not exist.");
return -1;
}
Log->Lines->Add("Attaching file " + FileToAttach);
TDMPCursor MyCursor(crHourGlass);
MailMessage->ContentType = "multipart/mixed";
TIdText *txt = new TIdText(MailMessage->MessageParts, NULL);
txt->Body->Add("With attachment.");
txt->ContentType = "text/plain";
TIdAttachment* LogFile = new
TIdAttachment(MailMessage->MessageParts, FileToAttach);
LogFile->FileName = ExtractFileName(FileToAttach);
//LogFile->ContentType = GetMIMETypeFromFile(LogFile->FileName);
LogFile->ContentType = "application/zip";
SMTPClient->Connect();
try
{
SMTPClient->Send(MailMessage);
}
__finally
{
SMTPClient->Disconnect();
}
Log->Lines->Add("TestFunction Completed");
return 0;
}
Gambit
 

Re:Attachment missing from email

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Royce Fessenden" < XXXX@XXXXX.COM >wrote in message
news:46e03084$ XXXX@XXXXX.COM ...

>I can send an email but I can't get the attachment to be included.

That is because you are putting the text in the wrong place and setting the
wrong ContentType for the message. Try this instead:

int TMainForm::TestFunction(void)
{
AnsiString FileToAttach = "C:\\Link\\Logs\\Logs.zip";

if( !FileExists(FileToAttach) )
{
Log->Lines->Add("File <" + FileToAttach + ">does not exist.");
return -1;
}

Log->Lines->Add("Attaching file " + FileToAttach);
TDMPCursor MyCursor(crHourGlass);

MailMessage->ContentType = "multipart/mixed";

TIdText *txt = new TIdText(MailMessage->MessageParts, NULL);
txt->Body->Add("With attachment.");
txt->ContentType = "text/plain";

TIdAttachment* LogFile = new
TIdAttachment(MailMessage->MessageParts, FileToAttach);
LogFile->FileName = ExtractFileName(FileToAttach);
//LogFile->ContentType = GetMIMETypeFromFile(LogFile->FileName);
LogFile->ContentType = "application/zip";

SMTPClient->Connect();
try
{
SMTPClient->Send(MailMessage);
}
__finally
{
SMTPClient->Disconnect();
}

Log->Lines->Add("TestFunction Completed");
return 0;
}


Gambit


Now I'm getting an Access violation at the line: txt->Body->Add("With attachment.");
Body appears to be NULL, though the debug shows a valid address for the ->Add() method. I tried several things, including replacing the NULL (second parameter) in the txt constructor with MailMessage->Body. I can change the place where I get an error, but anything that will run and send an email does not send the attachment. There's got to be something else I'm missing.
Royce
 

{smallsort}

Re:Attachment missing from email

"Royce" < XXXX@XXXXX.COM >wrote in message
Quote
Now I'm getting an Access violation at the line:
txt->Body->Add("With attachment.");

Body appears to be NULL
It cannot be. TIdText's constructor instantiates it.
Quote
though the debug shows a valid address for the ->Add() method.
That doesn't mean anything.
Gambit
 

Re:Attachment missing from email

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Royce" < XXXX@XXXXX.COM >wrote in message
news:46e06fc0$ XXXX@XXXXX.COM ...

>Now I'm getting an Access violation at the line:
>txt->Body->Add("With attachment.");
>
>Body appears to be NULL

It cannot be. TIdText's constructor instantiates it.

>though the debug shows a valid address for the ->Add() method.

That doesn't mean anything.


Gambit


I didn't think it would be of much use, but then I'm not a Builder or C++ guru by any means. At any rate, the original code compiled and ran, but did not give the desired result. The modified code fails at run time, and anything I try that does not give the runtime error gives the same result as the original code.
Is there any other information I might be able to provide that might help?
 

Re:Attachment missing from email

"Royce" < XXXX@XXXXX.COM >wrote in message
Quote
Is there any other information I might be able to provide that might help?
Please show the exact code you are using now.
Gambit
 

Re:Attachment missing from email

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Royce" < XXXX@XXXXX.COM >wrote in message
news:46e19c52$ XXXX@XXXXX.COM ...

>Is there any other information I might be able to provide that might help?

Please show the exact code you are using now.


Gambit



int TMainForm::TestFunction(void)
{
AnsiString FileToAttach = "C:\\Link\\Logs\\Logs.zip";
if( !FileExists(FileToAttach) )
{
Log->Lines->Add("File <" + FileToAttach + ">does not exist.");
return -1;
}
Log->Lines->Add("Attaching file " + FileToAttach);
TDMPCursor MyCursor(crHourGlass);
MailMessage->ContentType = "multipart/mixed";
TIdText* txt = new TIdText(MailMessage->MessageParts, NULL);
txt->ContentType = "text/plain";
txt->Body->Append("txt With zips attached.");
TIdAttachment* LogFile = new TIdAttachment(MailMessage->MessageParts, FileToAttach);
LogFile->FileName = ExtractFileName(FileToAttach);
//LogFile->ContentType = GetMIMETypeFromFile(LogFile->FileName);
LogFile->ContentType = "application/zip";
SMTPClient->Connect();
try
{
SMTPClient->Send(MailMessage);
}
__finally
{
SMTPClient->Disconnect();
}
Log->Lines->Add("TestFunction Completed");
return 0;
}
The .dfm settings of the components are as follows:
object SMTPClient: TIdSMTP
MaxLineAction = maException
Host = 'mail.dmp.com'
Port = 25
AuthenticationType = atNone
HeloName = 'Link'
Password = 'xxxxxx'
Username = 'engtesting'
Left = 176
Top = 52
end
object MailMessage: TIdMessage
AttachmentEncoding = 'MIME'
BccList = <>
CCList = <>
ContentType = 'Application/Zip'
ContentTransferEncoding = 'Base64'
Encoding = meMIME
From.Address = ' XXXX@XXXXX.COM '
From.Name = 'engtesting'
From.Text = 'engtesting < XXXX@XXXXX.COM >'
Recipients = <
item
Address = ' XXXX@XXXXX.COM '
Name = 'Royce'
Text = 'Royce < XXXX@XXXXX.COM >'
end>
ReplyTo = <>
Subject = 'Email via Link'
Left = 176
Top = 104
end
 

Re:Attachment missing from email

"Royce" < XXXX@XXXXX.COM >wrote in message
Quote
int TMainForm::TestFunction(void)
How are you setting up the TIdMessage prior to calling TestFunction()? Are
you ever clearing the TIdMessage between sendings?
Gambit
 

Re:Attachment missing from email

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Royce" < XXXX@XXXXX.COM >wrote in message
news:46e1c522$ XXXX@XXXXX.COM ...

>int TMainForm::TestFunction(void)

How are you setting up the TIdMessage prior to calling TestFunction()? Are
you ever clearing the TIdMessage between sendings?


Gambit


The only "setup" is setting properties directly on the form. (Sender, Receipients, etc.) I am not calling clear.
The application is a shell with an <Execute>button used for testing code snippets. All applicable code is in TestFunction().
Royce
 

Re:Attachment missing from email

"Royce" < XXXX@XXXXX.COM >wrote in message
Quote
The only "setup" is setting properties directly on the form. (Sender,
Receipients, etc.) I am not calling clear.
You need to clear the TIdMessage between sendings. Otherwise, the data from
a previous message will carry over into the next message (if you are sending
multiple messages to begin with, that is).
Gambit
 

Re:Attachment missing from email

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Royce" < XXXX@XXXXX.COM >wrote in message
news:46e55443$ XXXX@XXXXX.COM ...

>The only "setup" is setting properties directly on the form. (Sender,
Receipients, etc.) I am not calling clear.

You need to clear the TIdMessage between sendings. Otherwise, the data from
a previous message will carry over into the next message (if you are sending
multiple messages to begin with, that is).


Gambit


I finally figured it out. I had to add winsock2.hpp to the files to be included. And I had to reorder the directory list so that the Indy we were using was used before the Indy that ships with Borland. (I had added _WINSOCKAPI_ to the defines but that did not solve the issues.)
Royce