procedure TForm1.SendMail(Host, Username, Password, Subj, Recip, From, Attach: string);
var
M: TIdMessage;
begin
M:=TIdMessage.Create(Form1);
M.From.Text:=From; // От кого
M.Recipients.Add; // Добавили получателя
M.Recipients.Items[0].Text:=Recip; // Получатель
M.Subject:=subj; // Тема
TIdAttachment.Create(M.MessageParts, Attach); // Аттачмент
// Соединяемся
Memo1.Lines.Add('Соединение с сервером...');
IdSMTP1.AuthenticationType := atLogin;
IdSMTP1.Host:=Host; // Сервер
IdSMTP1.Username:=UserName; // Логин
IdSMTP1.Password:=Password; // Пароль
try
// Пробуем соединиться
IdSMTP1.Connect();
except
Memo1.Lines.Add('Ошибка! Сервер не найден.');
exit;
end;
// Пробуем отправить файл
try
if IdSMTP1.Connected then begin
Memo1.Lines.Add('Отправка файла...');
IdSMTP1.Send(M);
IdSMTP1.Disconnect;
Memo1.Lines.Add('Файл успешно отправлен.');
Button2.Enabled:=false;
end;
except
Memo1.Lines.Add('Ошибка соединения. Данные не отправлены.');
IdSMTP1.Disconnect;
end;
end;