简体   繁体   English

如何使用C ++ Builder发送电子邮件Android?

[英]How send email Android with c++ builder?

I have seen many examples in Delphi and not one a C ++ builder. 我在Delphi中看到了许多示例,而没有一个C ++构建器。 I tried to recreate the code in C ++, but it flies only exception. 我试图在C ++中重新创建代码,但它仅产生异常。 How to use the Intent to С++? 如何将意图用于С++?

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    callEmail("russia@gmail.com", "Application");
}
//---------------------------------------------------------------------------
void TForm1::callEmail(const String address, const String Subject){
  JIntent* intent;
  TJIntent* intentTwo;
  intent = intentTwo->Create();
  intent->setAction(intentTwo->JavaClass->ACTION_SEND);
  intent->setFlags(intentTwo->JavaClass->FLAG_ACTIVITY_NEW_TASK);
  intent->putExtra(intentTwo->JavaClass->EXTRA_EMAIL, StringToJString(address));
  intent->putExtra(intentTwo->JavaClass->EXTRA_SUBJECT, StringToJString(Subject));
  intent->setType(StringToJString('vnd.android.cursor.dir/email'));
  SharedActivity()->startActivity(intent);
}

I thnik, maybe I think maybe something needs to change in androidmanifest or user-permission? 我想,也许我认为也许需要改变androidmanifest或用户权限?

Your code is crashing because you are not constructing the Intent object correctly. 您的代码崩溃是因为您没有正确构造Intent对象。

Create() is a constructor in Delphi. Create()是Delphi中的构造函数。 intent := TJIntent.Create in Delphi would be intent = new TJIntent in C++. intent := TJIntent.Create在Delphi中创建将是intent = new TJIntent在C ++中intent = new TJIntent

Also, Embarcadero uses interfaces for its iOS/Android bridge frameworks, so you should use the provided DelphiInterface<T> typedefs, such as _di_JIntent instead of JIntent* directly. 另外,Embarcadero将接口用于其iOS / Android桥框架,因此您应使用提供的DelphiInterface<T> typedef,例如_di_JIntent而不是JIntent*

Also, JavaClass (and OCClass in iOS) is a static class property. 另外, JavaClass (iOS中为OCClass )是静态类属性。 You do not need an object instance to access it, just the class type. 您不需要对象实例来访问它,只需类类型。

Also, C++ uses single-quotes for character literals and double-quotes for string literals, whereas Delphi uses single-quotes for both. 同样,C ++对字符文字使用单引号,对字符串文字使用双引号,而Delphi两者都使用单引号。 'vnd.android.cursor.dir/email' in C++ is not a string literal, it is a multi-byte character literal instead, which is not what you want here. C ++中的'vnd.android.cursor.dir/email'不是字符串文字,而是多字节字符文字,这不是您想要的。 Use double-quotes instead. 请改用双引号。

Also, EXTRA_EMAIL must be expressed as an array of strings. 另外, EXTRA_EMAIL必须表示为字符串数组。

Try something more like this: 尝试更多类似这样的方法:

void TForm1::callEmail(const String address, const String Subject)
{
    _di_JIntent intent;
    intent = new TJIntent; // or: intent = TJIntent::JavaClass->init();
    intent->setAction(TJIntent::JavaClass->ACTION_SEND);
    // or: intent = TJIntent::JavaClass->init(TJIntent::JavaClass->ACTION_SEND);
    intent->setFlags(TJIntent::JavaClass->FLAG_ACTIVITY_NEW_TASK);
    TJavaObjectArray__1<_di_JString> *Recipients = new TJavaObjectArray__1<_di_JString>(1);
    Recipients->Items[0] = StringToJString(address);
    intent->putExtra(TJIntent::JavaClass->EXTRA_EMAIL, Recipients);
    intent->putExtra(TJIntent::JavaClass->EXTRA_SUBJECT, StringToJString(Subject));
    intent->setType(StringToJString(L"vnd.android.cursor.dir/email"));
    SharedActivity()->startActivity(intent);
}

Now, that said, you really should not be using vnd.android.cursor.dir/email as the intent's MIME type. 就是说,您实际上不应该将vnd.android.cursor.dir/email用作意图的MIME类型。 Use message/rfc822 instead, or even plain/text . 请改用message/rfc822 ,甚至使用plain/text But those do not limit the intent to just email clients, other apps might also support those types. 但是,这些并不仅限于电子邮件客户端,其他应用程序也可能支持这些类型。 To send an email using only a true email client, use ACTION_SENDTO with a mailto: URI instead. 要仅使用真正的电子邮件客户端发送电子邮件,请使用ACTION_SENDTOmailto: URI代替。 For 1 recipient, you can put the address directly in the URI and not use EXTRA_EMAIL at all. 对于1个收件人,您可以将地址直接放在URI中,而根本不使用EXTRA_EMAIL For 2+ recipients, use a mailto: URI with no address in it and use EXTRA_EMAIL for the addresses. 对于2 EXTRA_EMAIL收件人,请使用不带地址的mailto: URI,并使用EXTRA_EMAIL作为地址。 This is mentioned in the Android documentation: Android文档中提到了这一点:

Common Intents | 共同意图 Email 电子邮件

For example: 例如:

void TForm1::callEmail(const String address, const String Subject)
{
    _di_JIntent intent;
    intent = new TJIntent; // or: intent = TJIntent::JavaClass->init();
    intent->setAction(TJIntent::JavaClass->ACTION_SENDTO);
    intent->setData(StrToJURI(L"mailto:" + address));
    // or: intent = TJIntent::JavaClass->init(TJIntent::JavaClass->ACTION_SENDTO, StrToJURI(L"mailto:" + address));
    intent->setFlags(TJIntent::JavaClass->FLAG_ACTIVITY_NEW_TASK);
    intent->putExtra(TJIntent::JavaClass->EXTRA_SUBJECT, StringToJString(Subject));
    intent->setType(StringToJString(L"message/rfc822"));
    SharedActivity()->startActivity(intent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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