Setting since_tizen 3/4 on Tizen.NET API
[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.Threading.Tasks;
19
20 namespace Tizen.Messaging.Email
21 {
22     /// <summary>
23     /// This class is used to send email messages.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public static class EmailSender
27     {
28         /// <summary>
29         /// Sends the email message.
30         /// </summary>
31         /// <param name="email">The email message.</param>
32         /// <returns> Failure if the email sending activity failed, otherwise Success.</returns>
33         /// <since_tizen> 3 </since_tizen>
34         public static async Task<EmailSendResult> SendAsync(EmailMessage email)
35         {
36             var task = new TaskCompletionSource<EmailSendResult>();
37             int ret = (int)EmailError.None;
38             bool saveToSentBox = false;
39
40             email.FillHandle();
41             email.Save();
42
43             Interop.Email.EmailSentCallback _emailSendingCallback = (IntPtr handle, int result, IntPtr userData) =>
44             {
45                 task.SetResult((EmailSendResult)result);
46             };
47
48             ret = Interop.Email.SetCb(email._emailHandle, _emailSendingCallback, IntPtr.Zero);
49             if (ret != (int)EmailError.None)
50             {
51                 Log.Error(EmailErrorFactory.LogTag, "Failed to set email incoming callback, Error code: " + (EmailError)ret);
52                 throw EmailErrorFactory.GetException(ret);
53             }
54
55             ret = Interop.Email.SendEmail(email._emailHandle, saveToSentBox);
56             if (ret != (int)EmailError.None)
57             {
58                 Log.Error(EmailErrorFactory.LogTag, "Failed to send email, Error code: " + (EmailError)ret);
59                 throw EmailErrorFactory.GetException(ret);
60             }
61
62             var sendResult = await task.Task;
63
64             ret = Interop.Email.UnsetCb(email._emailHandle);
65             if (ret != (int)EmailError.None)
66             {
67                 Log.Error(EmailErrorFactory.LogTag, "Failed to set email incoming callback, Error code: " + (EmailError)ret);
68                 throw EmailErrorFactory.GetException(ret);
69             }
70
71             return sendResult;
72         }
73     }
74 }