Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Mar : Re: Socket Error # 0
| Subject: | Re: Socket Error # 0 |
| Posted by: | "Remy Lebeau (TeamB)" (no.spam@no.spam.com) |
| Date: | Thu, 22 Mar 2007 12:36:22 |
"Stephan Jaschke" <news@stj-software.de> wrote in message
news:4602c873$1@newsgroups.borland.com...
> function ContentType(const Ext: string): string;
Indy has its own GetMimeTypeFromFile() function for that same purpose.
> if Assigned(ABody) then
> Msg.Body.Assign(ABody)
> else
> Msg.ClearBody;
If you are adding attachments to the message, then you need to place
the text into a TIdText part instead.
> Msg.ContentType := 'text/plain';
That is the wrong ContentType to use when attachments are present.
Try this code instead:
procedure SendEMail(const ARecipients, ACC, ABCC, ASubject:
String; ABody, AAttachments: TStrings; AConfig: TTjEMailConfig);
var
SMTP: TIdSMTP;
Msg: TIdMessage;
i: Integer;
s: String;
begin
SMTP := TIdSMTP.Create(nil);
try
SMTP.MaxLineAction := maException;
SMTP.ReadTimeout := 0;
SMTP.RecvBufferSize := 8192;
SMTP.Host := AConfig.SMTPHost;
SMTP.Port := AConfig.SMTPPort;
SMTP.AuthenticationType := atLogin;
SMTP.Username := AConfig.Account;
SMTP.Password := AConfig.Pass;
Msg := TIdMessage.Create(nil);
try
Msg.Encoding := meMIME;
Msg.AttachmentEncoding := 'MIME';
Msg.From.Name := AConfig.SenderName;
Msg.From.Address := AConfig.SenderEMail;
Msg.Organization := AConfig.SenderOrganization;
Msg.ReplyTo.EMailAddresses := AConfig.SenderReplyTo;
Msg.Recipients.EMailAddresses := ARecipients;
Msg.CCList.EMailAddresses := ACC;
Msg.BccList.EMailAddresses := ABCC;
Msg.Subject := ASubject;
Msg.Priority := mpNormal;
if Assigned(AAttachments) and (AAttachments.Count > 0)
then
begin
TIdText.Create(Msg.MessageParts,
ABody).ContentType := 'text/plain';
for i := 0 to AAttachments.Count-1 do
begin
s := Trim(AAttachments.Strings[i]);
if not FileExists(s) then raise
ETjCheck.CreateResFmt(@SErrAttachNotFound, [s]);
TIdAttachment.Create(Msg.MessageParts,
s).ContentType := GetMimeTypeFromFile(s);
end;
Msg.ContentType := 'multipart/mixed';
end else
begin
if Assigned(ABody) then Msg.Body.Assign(ABody);
Msg.ContentType := 'text/plain';
end;
try
SMTP.Connect;
try
SMTP.Send(Msg);
finally
SMTP.Disconnect;
end;
except
on E: EIdSocketError do
case E.LastError of
WSAETIMEDOUT: raise
ETjCheck.CreateResFmt(@SErrConnectFailed, [AConfig.SMTPHost]);
else
raise;
end;
on E: EIdConnectTimeout do
raise
ETjCheck.CreateResFmt(@SErrConnectFailed, [AConfig.SMTPHost]);
on E: EIdProtocolReplyError do
raise ETjCheck.CreateFmt('Fehler beim
E-Mail-Versand.' + sLineBreak + 'Antwort des Servers: %s' + sLineBreak
+ E.Message, [AConfig.SMTPHost]);
end;
finally
Msg.Free;
end;
finally
SMTP.Free;
end;
end;
Gambit