简体   繁体   中英

Delphi XE5 and email

I found this code for send an email from Delphi application using default email engine

Procedure SendEmail(Const eAddress, eObject, eText, eAttach : String);
var
  Intent : JIntent;
  D, S   : JString;
Begin
  Intent := TJIntent.Create;

  Intent.setAction(TJIntent.JavaClass.ACTION_Send);
  Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
  Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, StringToJString(eAddress));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString(eObject));
  intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(eText));

  Intent.setType(StringToJString('vnd.android.cursor.dir/email'));

  SharedActivity.startActivity(Intent);
End;

The problem is with the attachment. In the original sample code there are a construct like this

Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM,
                TJnet_Uri.JavaClass.fromFile(FileName));

but the FileName must be a JFile.

How can I transform a String into a JFile? Or how can I send an email with attachment passing the file name to the function?

You can use the parse method of the URI class. like so

Uri:=TJnet_Uri.JavaClass.parse(StringToJString(FileName));
Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM, Uri);

and your code will look like this

var
  Intent : JIntent;
  D, S   : JString;
  Uri    : TJnet_Uri;
Begin
  Intent := TJIntent.Create;

  Intent.setAction(TJIntent.JavaClass.ACTION_Send);
  Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
  Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, StringToJString(eAddress));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString(eObject));
  intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(eText));
  Uri:=TJnet_Uri.JavaClass.parse(StringToJString(FileName));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM, Uri);
  Intent.setType(StringToJString('vnd.android.cursor.dir/email'));
  SharedActivity.startActivity(Intent);
End;

I use the following procedure to send e-mail with attachment and SSL encryption. It works:

procedure TForm1.ButtoMailClick(Sender: TObject);
var
  IdSSLIOHandlerOpenSSL1 : TIdSSLIOHandlerSocketOpenSSL;
begin
  IdSMTP1.Host:='smtp.gmail.com';
  IdSMTP1.Username:='username@gmail.com';
  //IdSMTP1.Port:=465;
  IdSMTP1.Password:='password';
  IdSMTP1.ReadTimeout := 9900;

  if (IdSMTP1.Port = 465) then
     IdSMTP1.UseTLS := utUseImplicitTLS
  else
     IdSMTP1.UseTLS := utUseExplicitTLS;

  //IdSMTP1.IOHandler := IdSSLIOHandlerOpenSSL1.Create(IdSMTP1);

  if IdSMTP1.Connected = True then
     IdSMTP1.Disconnect;

  IdMessage1.Clear;
  IdMessage1.From.Text :='Testing';
  IdMessage1.BccList.EMailAddresses :='sebastiankozub@gmail.com';
  IdMessage1.Priority := mpHigh;
  IdMessage1.Sender.Text:='Umail';
  IdMessage1.Subject := 'Subject';
  IdMessage1.Body.Add('test');

  if FileExists('Attach.txt') then
     TIdAttachmentFile.Create(IdMessage1.MessageParts, 'Attach.txt');

  try
    IdSMTP1.Connect;
    IdSMTP1.Authenticate;
    IdSMTP1.Send(IdMessage1);
  except
    on E : Exception do
       ShowMessage('little problem');
  end;
end;

Commented lines are also important if you do not do it in design time. I filled port and create SSL handler in designer so they are commented now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM