Thread View: borland.public.cppbuilder.internet.socket
3 messages
3 total messages
Started by Terry Neckar
Tue, 26 Jun 2007 15:56
Indy TidSMTP component
Author: Terry Neckar
Date: Tue, 26 Jun 2007 15:56
Date: Tue, 26 Jun 2007 15:56
62 lines
2615 bytes
2615 bytes
I'm running Borland Developer Studio 2006. I have a program that looks at
error data from a hardware device. It is always running and checking for
data. Upon finding it, it sends an email with an attachment. First time
around, it sends the attachment fine. Any subsequent cycles, it gets an
EAccess Violation when preparing the attachment. Here is a piece of my code.
-------------------------------------------------------
TIdAttachment *Attachment = new
TIdAttachment(MainForm->MailMessage->MessageParts, "Media.pdf");
MailMessage->Recipients->Add()->Address = joe@mail.com;
MailMessage->Subject = "My media report";
MailMessage->From->Name = "Johns Fish House";
MailMessage->From->Address = "admin@fishhouse.com";
MailMessage->Body->Clear();
MailMessage->Body->Add("This is an automatically generated email.");
SMTP->Host = "mail.fishhouse.com";
SMTP->Username = "mike";
try
{
SMTP->Connect();
}
catch (Exception& E)
{
LogErrorMessage(E.Message.c_str());
remove("Media.pdf");
free(Attachment);
LogErrorMessage("Connect email to specialist and CHE mail failed.");
return;
}
try
{
SMTP->Send(MailMessage);
}
catch (Exception& E)
{
LogErrorMessage(E.Message.c_str());
remove("Media.pdf");
free(Attachment);
LogErrorMessage("Send email to specialist and CHE mail failed.");
return;
}
-----------------------------------------------------------------
Here is the status messages:
Resolving Hostname mail.fishhouse.com.
Connecting to 45.222.47.123.
Connected.
EAccess Violation.
Disconnecting.
Disconnected.
-----------------------------------------------------------------
Thanks,
Terry
Re: Indy TidSMTP component
Author: Remy Lebeau (TeamB)
Date: Tue, 26 Jun 2007 16:45
Date: Tue, 26 Jun 2007 16:45
94 lines
3616 bytes
3616 bytes
"Terry Neckar" <tneckar@cheservice.com> wrote in message
news:46813745$1@newsgroups.borland.com...
> Any subsequent cycles, it gets an EAccess Violation
> when preparing the attachment.
You did not say which version of Indy you are using.
> TIdAttachment *Attachment = new
> TIdAttachment(MainForm->MailMessage->MessageParts, "Media.pdf");
You are using a relative file path. If the calling process's working
directory changes (which is likely to happen often), then
TIdAttachment won't be able to find the file anymore. You should
always use full paths whenever dealing with files.
> MailMessage->Body->Clear();
> MailMessage->Body->Add("This is an automatically generated email.");
Do not use the TIdMessage::Body property when attachments are
involved. The text must be in a TIdText part of its own instead.
> catch (Exception& E)
Always catch exception by 'const' reference.
> remove("Media.pdf");
You are using a relative path again. That is not good.
> free(Attachment);
DO NOT use free() on memory that was allocated with 'new'!!! You must
use 'delete' instead, or else the memory will not be freed properly.
Alternatively, just Clear() the TIdMessage. It will free the
attachment for you.
Try this code instead:
AnsiString FileName = "C:\\The Folder To\\Media.pdf";
MailMessage->Clear();
MailMessage->Recipients->Add()->Address = "joe@mail.com";
MailMessage->Subject = "My media report";
MailMessage->From->Name = "Johns Fish House";
MailMessage->From->Address = "admin@fishhouse.com";
TIdText *Text = new TIdText(MailMessage->MessageParts, NULL);
Text->ContentType = "text/plain";
Text->Body->Add("This is an automatically generated email.");
TIdAttachment *Attachment = new
TIdAttachment(MailMessage->MessageParts, FileName);
Attachment->ContentType = GetMIMETypeFromFile(FileName);
SMTP->Host = "mail.fishhouse.com";
SMTP->Username = "mike";
try
{
try
{
SMTP->Connect();
}
catch (const Exception& E)
{
throw Exception("Connect email to specialist and CHE mail
failed. " + E.Message);
}
try
{
SMTP->Send(MailMessage);
}
catch (const Exception& E)
{
SMTP->Disconnect();
throw Exception("Send email to specialist and CHE mail
failed. " + E.Message);
}
SMTP->Disconnect();
}
catch (const Exception& E)
{
MailMessage->Clear();
DeleteFile(FileName);
LogErrorMessage(E.Message.c_str());
return;
}
Gambit
Re: Indy TidSMTP component
Author: Terry Neckar
Date: Tue, 26 Jun 2007 18:40
Date: Tue, 26 Jun 2007 18:40
4 lines
1003 bytes
1003 bytes
Thanks for your help. I'll implement your suggestions. Terry
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads