Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Сети > idSMTP Проблема с кодировкой при аттаче


Автор: markesyan 9.3.2007, 00:09
Вполне стандартная ф-ция отправки письма, взята кстати с этого форума.
Когда на форме добавляю отправлять с аттачем уходит в нечитабельной кодировке,
без аттача все нормально. Вот код.
За ранее благодарен, поиском пользовался не нашел smile
ЗЫ: отправляю через MDaemon SMTP/POP/IMAP server: v9.0.7
Код

function SendMail(Host:String; AdressTo:String; AdressFrom:String; Subject:String; txt:String; FileAttach: String):Boolean;
var    
  smtp: TIdSMTP;
  Msg: TIdMessage;
begin
  smtp := TIdSMTP.Create(nil);
  smtp.Host:=Host;
  Msg:=TIdMessage.Create(smtp);
  Msg.Date := now();
  Msg.CharSet := 'windows-1251';
  Msg.Encoding := meMIME;
  Msg.ContentType := 'text/plain';
  Msg.ContentDisposition := 'inline';
  Msg.ReplyTo.EMailAddresses := AdressFrom;
  Msg.Recipients.EMailAddresses := AdressTo;
  Msg.Subject:=Subject;
  Msg.Body.Text:=Txt;
  MSG.Body.AddStrings(textmsg);
  if FileAttach <> '' then
    If FileExists(FileAttach) then
       TIdAttachment.Create(Msg.MessageParts, FileAttach);
  try
    smtp.Connect;
    smtp.Send(Msg);
    smtp.Disconnect;
    Result:=true;
  except
    Result:=False;
  end;
  smtp.Free;
end;

Автор: markesyan 9.3.2007, 00:53
Прошу прощения, плохо искал. Вот здесь предложенный вариант мне помог.

http://forum.vingrad.ru/index.php?showtopic=26195&view=findpost&p=185307

Автор: larinva 27.10.2009, 10:26
можно еще так

Код:
Код



const

Koi: array[0..66] of Char = (
'T', 'Ё', 'ё', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж',
'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р',
'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ',
'Ы', 'Ь', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д',
'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш',
'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я');

Win: array[0..66] of Char = (
'ё', 'Ё', 'T', 'ю', 'а', 'б', 'ц', 'д', 'е', 'ф',
'г', 'х', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
'я', 'р', 'с', 'т', 'у', 'ж', 'в', 'ь', 'ы', 'з',
'ш', 'э', 'щ', 'ч', 'ъ', 'Ю', 'А', 'Б', 'Ц', 'Д',
'Е', 'Ф', 'Г', 'Х', 'И', 'Й', 'К', 'Л', 'М', 'Н',
'О', 'П', 'Я', 'Р', 'С', 'Т', 'У', 'Ж', 'В', 'Ь',
'Ы', 'З', 'Ш', 'Э', 'Щ', 'Ч', 'Ъ');


function WinToKoi(Str: string): string;
var
  i, j, index: Integer;
begin
  Result := '';

  for i := 1 to Length(Str) do
  begin
    index := -1;
    for j := Low(Win) to High(Win) do
      if Win[j] = Str[i] then
      begin
        index := j;
        Break;
      end;

    if index = -1 then
      Result := Result + Str[i]
    else
      Result := Result + Koi[index];
  end;
end;

function KoiToWin(Str: string): string;
var
  i, j, index: Integer;
begin
  Result := '';

  for i := 1 to Length(Str) do
  begin
    index := -1;
    for j := Low(Win) to High(Win) do
      if Koi[j] = Str[i] then
      begin
        index := j;
        Break;
      end;

    if index = -1 then
      Result := Result + Str[i]
    else
      Result := Result + Win[index];
  end;
end;


procedure SendFileOnSMTP(Host: string; Port: Integer;
Subject, FromAddress, ToAddress, Body, FileName: string);
var
  NMSMTP: TNMSMTP;
begin
  if DelSpace(ToAddress) = '' then
    Exit;
  if ToAddress[1] = '' then
    Exit;

  if (DelSpace(FileName) <> '') and not FileExists(FileName) then
    raise Exception.Create('SendFileOnSMTP: file not exist: ' + FileName);

  NMSMTP := TNMSMTP.Create(nil);
  try
    NMSMTP.Host := Host;
    NMSMTP.Port := Port;
    NMSMTP.Charset := 'koi8-r'
    NMSMTP.PostMessage.FromAddress := FromAddress;
    NMSMTP.PostMessage.ToAddress.Text := ToAddress;
    NMSMTP.PostMessage.Attachments.Text := FileName;
    NMSMTP.PostMessage.Subject := Subject;
    NMSMTP.PostMessage.Date := DateTimeToStr(Now);
    NMSMTP.UserID := 'netmaster'
    NMSMTP.PostMessage.Body.Text := WinToKoi(Body);
    NMSMTP.FinalHeader.Clear;
    NMSMTP.TimeOut := 5000;
    NMSMTP.Connect;
    NMSMTP.SendMail;
    NMSMTP.Disconnect;
  finally
    NMSMTP.Free;
  end;
end;



Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)