Release 8.0.0.15812
[platform/core/csapi/tizenfx.git] / src / Tizen.Messaging / Tizen.Messaging.Email / EmailSender.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Threading.Tasks;
20
21 namespace Tizen.Messaging.Email
22 {
23     /// <summary>
24     /// This class is used to send email messages.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public static class EmailSender
28     {
29         static private Dictionary<int, Interop.Email.EmailSentCallback> _sendCbMap = new Dictionary<int, Interop.Email.EmailSentCallback>();
30         static private int _callbackId = 0;
31
32         /// <summary>
33         /// Sends the email message.
34         /// </summary>
35         /// <param name="email">The email message.</param>
36         /// <returns> Failure if the email sending activity failed, otherwise Success.</returns>
37         /// <since_tizen> 3 </since_tizen>
38         public static async Task<EmailSendResult> SendAsync(EmailMessage email)
39         {
40             var task = new TaskCompletionSource<EmailSendResult>();
41             int ret = (int)EmailError.None;
42             bool saveToSentBox = false;
43
44             email.FillHandle();
45             email.Save();
46
47             int id = _callbackId++;
48             _sendCbMap[id] = (IntPtr handle, int result, IntPtr userData) =>
49             {
50                 task?.SetResult((EmailSendResult)result);
51                 _sendCbMap.Remove((int)userData);
52             };
53
54             ret = Interop.Email.SetCb(email._emailHandle, _sendCbMap[id], (IntPtr)id);
55             if (ret != (int)EmailError.None)
56             {
57                 Log.Error(EmailErrorFactory.LogTag, "Failed to set email incoming callback, Error code: " + (EmailError)ret);
58                 _sendCbMap.Remove(id);
59                 throw EmailErrorFactory.GetException(ret);
60             }
61
62             ret = Interop.Email.SendEmail(email._emailHandle, saveToSentBox);
63             if (ret != (int)EmailError.None)
64             {
65                 Log.Error(EmailErrorFactory.LogTag, "Failed to send email, Error code: " + (EmailError)ret);
66                 _sendCbMap.Remove(id);
67                 throw EmailErrorFactory.GetException(ret);
68             }
69
70             var sendResult = await task.Task.ConfigureAwait(false);
71             ret = Interop.Email.UnsetCb(email._emailHandle);
72
73             if (ret != (int)EmailError.None)
74             {
75                 Log.Error(EmailErrorFactory.LogTag, "Failed to set email incoming callback, Error code: " + (EmailError)ret);
76                 throw EmailErrorFactory.GetException(ret);
77             }
78             return sendResult;
79         }
80
81     }
82 }