Error sending E-Mail by Indy 10

1

The following error appears when you attempt to send the mail function:

Não há um programa padrão de e-mail associado para realizar a ação solicitada. instale um programa de e-mail ou, caso já exista um instalado, crie uma associação no painel de controle Programas Padrão.

Follow the code:

procedure TfrmEnviaEmail.btnEnviarClick(Sender: TObject);
var
    i: integer;
  IdAttachmentFile1 : TIdAttachmentFile;
  IdUserPassProvider: TIdUserPassProvider;
  idSASLLogin: TIdSASLLogin;
begin
  IdSMTP1.ConnectTimeout := 30000;
  IdSMTP1.ReadTimeout := 30000;
    try
//      cancelar:= false;
        Screen.Cursor:= crHourGlass;
        btnEnviar.Enabled:= false;

    if cbDominio.ItemIndex = 0 then    //anibaltec
    begin
      IdSMTP1.Host := 'anibaltec.com.br';
    end;
    if cbDominio.ItemIndex = 1 then    //gmail
    begin
      IdSMTP1.Host := 'smtp.gmail.com';
    end;
    if cbDominio.ItemIndex = 2 then    //hotmail
    begin
      IdSMTP1.Host := 'smtp.live.com';
    end;
    if cbDominio.ItemIndex = 3 then    //yahoo
    begin
      IdSMTP1.Host := 'smtp.mail.yahoo.com';
    end;
    if cbDominio.ItemIndex = 4 then    //tef.net
    begin
      IdSMTP1.Host := 'tef.net.br';
    end;

    IdMessage1.Clear;
    IdMessage1.MessageParts.Clear;
    IdMessage1.IsEncoded := True;
    IdMessage1.CharSet := 'iso-8859-1';
    IdMessage1.Encoding := MeMIME;
    IdMessage1.ConvertPreamble := True;
    IdMessage1.Priority := mpNormal;
    IdMessage1.Date := Now;
    IdMessage1.From.Address := edtEmailDest.Text;
        IdMessage1.Subject := EdtAssunto.Text;
    IdMessage1.Body.Text := 'Este e-mail e um e-mail automatico, por favor nao responde-lo. Segue em anexo o relatório.';
    IdMessage1.ContentType := 'multipart/mixed';
    IdMessage1.Recipients.EMailAddresses := EdtPara.Text;

    //recebimento de confirmação
    IdMessage1.Headers.Add('Disposition-Notification-To: '+edtEmailDest.Text);
    IdMessage1.ReceiptRecipient.Address := edtEmailDest.Text;
    IdMessage1.ReceiptRecipient.User := edtEmailDest.Text;
    IdMessage1.ReceiptRecipient.Text := IdMessage1.From.Text;

    if ListBox1.Items.Count > 0 then
        begin
            for i:= 0 to ListBox1.Items.Count - 1 do
      begin
        if FileExists(ListBox1.Items[i]) then
        begin
          IdAttachmentFile1 := TIdAttachmentFile.Create(IdMessage1.MessageParts, ListBox1.Items[i]);

          if ExtractFileExt(ListBox1.Items[i]) = 'ini' then
            IdAttachmentFile1.ContentType := 'application/txt;'
          else
            IdAttachmentFile1.ContentType := 'application/'+ExtractFileExt(ListBox1.Items[i])+';';

          IdAttachmentFile1.FileName := ExtractFileName(ListBox1.Items[i]);
        end;
            end;
    end;

    with idSMTP1 do
    begin
      try
        Port := 587;
        IOHandler := IdSSLIOHandlerSocketOpenSSL1;
        UseTLS := utNoTLSSupport;
//        UseTLS := utUseExplicitTLS;
//        IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvTLSv1;
        IdSSLIOHandlerSocketOpenSSL1.PassThrough := true;
      except
        on E: Exception do
        begin
          IOHandler := TIdIOHandler.MakeDefaultIOHandler( nil );
          UseTLS := utNoTLSSupport;
        end;
      end;
    end;
    if not IdSMTP1.Connected then
    begin
      try
        IdSMTP1.AuthType := satDefault;
        IdSMTP1.Username := edtEmailDest.Text;
        IdSMTP1.Password := edtSenhaDest.Text;
        IdSMTP1.ReadTimeout := 10000;

        IdSMTP1.Connect;
        IdSMTP1.Authenticate;
      except
        on e: Exception do frmprincipal.EscreveLog(caminholog, 'Aplicativo não conseguiu se comunicar com o servidor de e-mail! Erro: '+e.Message);
      end;
    end;

    IdSMTP1.Send(IdMessage1);

    if ListBox1.Items.Count > 0 then
        begin
            for i:= 0 to ListBox1.Items.Count - 1 do
      begin
        if FileExists(ListBox1.Items[i]) then
        begin
          try
            if IdSMTP1.Connected then
            begin
              frmPrincipal.EscreveLog(caminholog, 'Email enviado com sucesso. ' +
                'Relatório '+ListBox1.Items[i]+' em anexo');
            end;
          except
             on e: Exception do frmPrincipal.EscreveLog(caminholog,
                'Servidor de Email ('+IdSMTP1.Host+') não foi conectado');
          end;
        end;
      end;
    end;
  finally
    if IdSMTP1.Connected then
    begin
      frmPrincipal.StatusBar1.Panels[0].Text := 'Email enviado com sucesso.';
      frmprincipal.EscreveLog(caminholog, 'Email enviado com sucesso.');
    end
    else
    begin
      frmprincipal.EscreveLog(caminholog, 'Aplicativo não conseguiu se comunicar com o servidor de e-mail!');
    end;
    Screen.Cursor:= crDefault;
    IdSMTP1.Disconnect;
    btnEnviar.Enabled:= true;
    ListBox1.Clear;
    Close;
  end;
end;
    
asked by anonymous 20.01.2015 / 14:19

1 answer

1

According to this article from Microsoft, follow the steps to continue to resolve this impasse.

  • Open the Control Panel → Programs → Standard Programs .
  • Click Set access to computer programs and defaults .
  • In the Define access to Programs and Computer Defaults window, click the Custom configuration arrow to open the menu for that category.
  • In the Choose a default email program category, select the e-mail program you want to use, for example, Microsoft Outlook , and then click OK .
  • This configuration is saved in the following key in the registry:

    HKEY_CLASSES_ROOT → mailto\shell\open\command
    

    To change this value programmatically, you can use Winapi RegSetValueEx() " or use the class functions Registry .

    Example 1 - RegSetValueEx ()

    function RegSetValue(Key: HKEY; SubKey, NomeDoValor, Valor: String): Boolean;
    Var
      phk: HKEY;
    begin
    Result := False;
    if RegOpenKeyEx(Key, pchar(SubKey), 0, KEY_SET_VALUE, phk) = 0 then begin
      Result := RegSetValueEx(phk, PChar(NomeDoValor), 0, REG_SZ, PChar(Valor), (Length(Valor) + 1) * SizeOf(Char)) = 0;
      RegCloseKey(phk);
    end;
    end;
    

    Usage:

    procedure TForm1.Button2Click(Sender: TObject);
    const
     EMAIL_PROGRAM_SYNTAX = 'C:\Path\Do\Programa\De\Email -c /e "%1"';
    begin
    if RegSetValue(HKEY_CLASSES_ROOT, 'mailto\shell\open\command', '', EMAIL_PROGRAM_SYNTAX) then
      ShowMessage('Operação Feita com sucesso!')
    else
      ShowMessage('Erro na operação! Você tem privilégios para fazer isso?');
    end;
    

    Example 2 - Registry

    // Inclua em Uses: Registry;
    function RegSetValue2(Key: HKEY; SubKey, NomeDoValor, Valor: string): Boolean;
    Var
     Reg: TRegistry;
    begin
    Result := false;
    Try
      Reg := TRegistry.Create;
      Reg.RootKey := Key;
    
      if (Reg.KeyExists(SubKey) and (Reg.OpenKey(SubKey, True))) then begin
        Reg.WriteString(NomeDoValor, Valor);
        result := True;
      end;
      Reg.CloseKey;
    
    Finally
      Reg.Free;
    End;
    end;
    

    Usage:

    procedure TForm1.Button1Click(Sender: TObject);
    const
     EMAIL_PROGRAM_SYNTAX = 'C:\Path\Do\Programa\De\Email -c /e "%1"';
    begin
    if RegSetValue2(HKEY_CLASSES_ROOT, 'mailto\shell\open\command', '', EMAIL_PROGRAM_SYNTAX) then
      ShowMessage('Operação Feita com sucesso!')
    else
      ShowMessage('Erro na operação! Você tem privilégios para fazer isso?');
    end;
    

    To work as expected, you probably have to have privileges to do this operation. Also, be sure to pass the argument line correctly to the email program of your choice.

        
    20.01.2015 / 14:41