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