Article View: borland.public.cppbuilder.internet.socket
Article #1019Re: Indy TidSMTP component
From: 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
Technical Details
Message-ID:
<46814544@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<46813745$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,69af8efb47c65aa X-Google-Attributes: gid11508e,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <46813745$1@newsgroups.borland.com> Subject: Re: Indy TidSMTP component Date: Tue, 26 Jun 2007 09:45:14 -0700 Lines: 96 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 75.82.252.126 Message-ID: <46814544@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1182876996 75.82.252.126 (26 Jun 2007 09:56:36 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail