• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using startAbilityByType to Start an Email Application
2
3<!--Kit: Ability Kit-->
4<!--Subsystem: AGC-->
5<!--Owner: @liusu23-->
6<!--Designer: @xukeke-->
7<!--Tester: @yizhixiaosiqin-->
8<!--Adviser: @huipeizi-->
9
10This topic describes how to open the vertical domain panel of email applications.
11> **NOTE**
12>
13> If the parameter from the initiating application is a mailto string, you are advised to [use mailto to start an email application](start-email-apps-by-mailto.md). Upon receiving the mailto string, the email application parses the string to fill in details like the sender, recipient, and email body.
14
15## Parameters on the Email Application Panel
16
17If the **type** field in **startAbilityByType** is set to **mail**, **wantParam** contains the following properties.
18
19| Name                               | Type                                                        | Mandatory| Description                                                        |
20| ------------------------------------- | ------------------------------------------------------------ | ------ | ------------------------------------------------------------ |
21| email                                 | string[ ]                                                    | No  | Email address of the recipient. Multiple email addresses, separated by commas (,), are supported.                      |
22| cc                                    | string[ ]                                                    | No  | Email address of the CC recipient. Multiple email addresses, separated by commas (,), are supported.                      |
23| bcc                                   | string[ ]                                                    | No  | Email address of the BCC recipient. Multiple email addresses, separated by commas (,), are supported.                      |
24| subject                               | string                                                       | No  | Email subject.                                                    |
25| body                                  | string                                                       | No  | Email body.                                                    |
26| ability.params.stream                 | string[ ]                                                    | No  | Email attachments (URI list of the attachments).                               |
27| ability.want.params.uriPermissionFlag | [wantConstant.Flags](../reference/apis-ability-kit/js-apis-app-ability-wantConstant.md#flags) | No  | At least the read permission must be granted on the email attachments. This parameter is mandatory when **ability.params.stream** is specified.|
28| sceneType                             | number                                                       | No  | Intent scene, which indicates the purpose of the current request. 1: Send an email. The default value is **1**.                             |
29
30> **NOTE**
31>
32> * Parameters of the string type displayed in the vertical domain panel of email applications must be encoded using **encodeURI**.
33>
34> * For parameters of the string[] type displayed in the vertical domain panel of email applications, all elements in the array must be encoded using **encodeURI**.
35
36## Developing a Caller Application
371. Import the module.
38    ```ts
39    import { common, wantConstant } from '@kit.AbilityKit';
40    ```
412. Construct parameters and call the **startAbilityByType** API.
42
43    ```ts
44    @Entry
45    @Component
46    struct Index {
47        @State hideAbility: string = 'hideAbility'
48
49        build() {
50            Row() {
51                Column() {
52                    Text(this.hideAbility)
53                        .fontSize(30)
54                        .fontWeight(FontWeight.Bold)
55                        .onClick(() => {
56                            let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
57                            let wantParam: Record<string, Object> = {
58                                'sceneType': 1,
59                                'email': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // Email address of the recipient. Multiple values are separated by commas (,). The array content is URL encoded using encodeURI().
60                                'cc': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // Email address of the CC recipient. Multiple values are separated by commas (,). The array content is URL encoded using encodeURI().
61                                'bcc': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // Email address of the BCC recipient. Multiple values are separated by commas (,). The array content is URL encoded using encodeURI().
62                                'subject': encodeURI('Email subject'), // Email subject. The content is URL encoded using encodeURI().
63                                'body': encodeURI('Email body'), // Email body. The content is URL encoded using encodeURI().
64                                'ability.params.stream':[encodeURI('attachment uri1'), encodeURI('attachment uri2')], // Attachment URIs. Multiple values are separated by commas (,). The array content is URL encoded using encodeURI().
65                                'ability.want.params.uriPermissionFlag': wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION
66                            };
67                            let abilityStartCallback: common.AbilityStartCallback = {
68                                onError: (code: number, name: string, message: string) => {
69                                    console.log(`onError code ${code} name: ${name} message: ${message}`);
70                                },
71                                onResult: (result) => {
72                                    console.log(`onResult result: ${JSON.stringify(result)}`);
73                                }
74                            }
75
76                            context.startAbilityByType("mail", wantParam, abilityStartCallback,
77                                (err) => {
78                                    if (err) {
79                                        console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`);
80                                    } else {
81                                        console.log(`success`);
82                                    }
83                                });
84                        });
85                }
86                .width('100%')
87            }
88            .height('100%')
89        }
90    }
91    ```
92    Effect
93
94    ![Effect example](./figures/start-mail-panel.png)
95
96## Developing a Target Application
97
981. Add the [linkFeature](../quick-start/module-configuration-file.md#skills) attribute to **module.json5** and declare the features supported. In this way, the system can find the applications that support a specific feature from all the applications installed on the device.
99
100    | Value          | Description                     |
101    | --------------| ------------------------- |
102    | ComposeMail   | The application supports email writing.	|
103
104    ```json
105    {
106      "abilities": [
107          {
108          "skills": [
109              {
110              "uris": [
111                  {
112                  "scheme": "mailto", // It is for reference only. Ensure that the declared URI can be started by external systems.
113                  "host": "",
114                  "path": "",
115                  "linkFeature": "ComposeMail" // Declare that the application supports email writing.
116                  }
117                ]
118              }
119          ]
120          }
121      ]
122    }
123    ```
124
1252. Parse and process the parameters transferred from the panel.
126
127    ```ts
128    UIAbility.onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void
129    ```
130
131    The **want.parameters** parameter contains the following parameters, which may be slightly different from the ones passed in by the caller.
132
133    | Name | Type     | Mandatory| Description                                  |
134    | ------- | --------- | ---- | -------------------------------------- |
135    | email   | string[ ] | No  | Email address of the recipient. Multiple email addresses, separated by commas (,), are supported.|
136    | cc      | string[ ] | No  | Email address of the CC recipient. Multiple email addresses, separated by commas (,), are supported.|
137    | bcc     | string[ ] | No  | Email address of the BCC recipient. Multiple email addresses, separated by commas (,), are supported.|
138    | subject | string    | No  | Email subject.                              |
139    | body    | string    | No  | Email body.                              |
140    | stream  | string[ ] | No  | Email attachments (URI list of the attachments).     |
141
142    > **NOTE**
143    >
144    > * Parameters of the string type received by the target application must be decoded using **decodeURI**.
145    >
146    > * For parameters of the string[] type received by the target application, all elements in the array must be decoded using **decodeURI**.
147
148**Sample Code**
149
150```ts
151import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
152import { hilog } from '@kit.PerformanceAnalysisKit';
153import { window } from '@kit.ArkUI';
154
155const TAG = 'MailTarget1.EntryAbility'
156
157export default class EntryAbility extends UIAbility {
158    windowStage: window.WindowStage | null = null;
159
160    email: string[] | undefined;
161    cc: string[] | undefined;
162    bcc: string[] | undefined;
163    subject: string | undefined;
164    body: string | undefined;
165    stream: string[] | undefined;
166
167    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
168        hilog.info(0x0000, TAG, `onCreate, want=${JSON.stringify(want)}`);
169        super.onCreate(want, launchParam);
170        this.parseWant(want);
171    }
172
173    onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
174        hilog.info(0x0000, TAG, `onNewWant, want=${JSON.stringify(want)}`);
175        super.onNewWant(want, launchParam);
176        this.parseWant(want);
177        if (!this.windowStage) {
178            hilog.error(0x0000, TAG, 'windowStage is null');
179            this.context.terminateSelf();
180            return;
181        }
182        this.loadPage(this.windowStage);
183    }
184
185    private parseWant(want: Want): void {
186        this.email = this.decodeStringArr(want.parameters?.email as string[]);
187        this.cc = this.decodeStringArr(want.parameters?.cc as string[]);
188        this.bcc = this.decodeStringArr(want.parameters?.bcc as string[]);
189        this.subject = decodeURI(want.parameters?.subject as string); // Use decodeURI() to decode the URL of the email subject. Other fields are processed in the same way.
190        this.body = decodeURI(want.parameters?.body as string); // Use decodeURI() to decode the URL of the email body. Other fields are processed in the same way.
191        this.stream = this.decodeStringArr(want.parameters?.stream as string[]);
192    }
193
194    // Use decodeURI() to decode the content in the string array.
195    private decodeStringArr(source: string[] | undefined): string[] {
196        let target: string[] = [];
197        source?.forEach(e => {
198            target.push(decodeURI(e));
199        })
200        return target;
201    }
202
203    private loadPage(windowStage: window.WindowStage): void {
204        const storage: LocalStorage = new LocalStorage({
205            "email": this.email,
206            "cc": this.cc,
207            "bcc": this.bcc,
208            "subject": this.subject,
209            "body": this.body,
210            "stream": this.stream
211        } as Record<string, Object>);
212
213        windowStage.loadContent('pages/ComposeMailPage', storage);
214
215    }
216
217    onDestroy(): void {
218        hilog.info(0x0000, TAG, `onDestroy`);
219    }
220
221    onWindowStageCreate(windowStage: window.WindowStage): void {
222        hilog.info(0x0000, TAG, `onWindowStageCreate`);
223        this.windowStage = windowStage;
224        this.loadPage(this.windowStage);
225    }
226
227    onWindowStageDestroy(): void {
228        hilog.info(0x0000, TAG, `onWindowStageDestroy`);
229    }
230
231    onForeground(): void {
232        hilog.info(0x0000, TAG, `onForeground`);
233    }
234
235    onBackground(): void {
236        hilog.info(0x0000, TAG, `onBackground`);
237    }
238}
239```
240