Title: Send mime multipart e-Mail with indy 10
Question: How to use TidAttachment with indy 10 and delphi2005
Answer:
//This is a simple function for send e-Mail via SMTP using indy10. //Jpeg file in attach is displayed into the body of the e-Mail.
//SendSMTPMail('recipient@mail.it',
// 'cc@mail.it',
// 'BodyPlainText',
// 'BodyHtmlText',
// 'ObjectText',
// 'FileNameJpgWithDir',
// 'FileNameForHeatherWithoutDir')
function TWM1.SendSMTPMail(V_To,V_CC,V_Text,V_html,V_Subject,V_attac,V_file: String):boolean ;
var
idAttach: TIdAttachmentFile;
htmpart, txtpart: TIdText;
begin
//SetUp generale
try
Result:=True ;
try
//SetUp messaging
MyMessage.IsEncoded:=True ;
//SetUp from variables
MyMessage.CharSet:='iso-8859-1';
MyMessage.Encoding:=meMIME;
MyMessage.Subject:=V_Subject ;
MyMessage.From.Text:='MyName@MyHost.it' ;
MyMessage.From.Name:='NameDescriptionMail';
MyMessage.Recipients.Clear ;
MyMessage.Recipients.add.Text:=V_To ;
MyMessage.CCList.Clear ;
MyMessage.CCList.Add.Text:=V_CC ;
MyMessage.ContentType:='multipart/related' ;
MyMessage.Body.Clear ;
try
txtpart := TIdText.Create(MyMessage.MessageParts);
txtpart.Body.Text :=V_Text;
txtpart.ContentType := 'text/plain';
txtpart.ContentTransfer := '7bit';
htmpart := TIdText.Create(MyMessage.MessageParts);
htmpart.Body.Text :=V_Html;
htmpart.ContentType := 'text/html';
except
result:=False ;
exit ;
end;
if (trim(V_attac)'') then
begin
try
idAttach:=TIdAttachmentFile.Create(MyMessage.MessageParts,V_attac);
idAttach.ContentType := 'image/jpeg';
idAttach.ExtraHeaders.Add('Content-ID: '+V_file+'');
idAttach.ExtraHeaders.Values['content-id'] := V_file ;
idAttach.DisplayName:= V_file ;
idAttach.ContentDisposition := 'inline';
except
idAttach.CleanupInstance ;
end;
end
except
result:=False ;
exit ;
end;
if result then
try
idSMTP1.Host:='smtp.MyHost.it' ;
idSMTP1.Username:='MyUser' ;
idSMTP1.Password:='MyPW' ;
try
idSMTP1.Connect ;
idSMTP1.Send(MyMessage);
Result:= True
except
Result:= False ;
exit ;
end;
finally
idSMTP1.Disconnect ;
idSMTP1.Free ;
end;
finally
MyMessage.Free ;
end;
end;