65306b00a6aa8060ce4806e0616477e54fe3f2d1
[platform/core/csapi/tizenfx.git] / src / Tizen.Messaging / Tizen.Messaging.Email / EmailMessage.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.Collections.ObjectModel;
20
21 namespace Tizen.Messaging.Email
22 {
23     /// <summary>
24     /// This class contains the Messaging API to support sending email messages.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class EmailMessage : IDisposable
28     {
29         internal IntPtr _emailHandle = IntPtr.Zero;
30         private bool _disposed = false;
31         private String _subject;
32         private String _body;
33         private IList<EmailAttachment> _attachments = new List<EmailAttachment>();
34         private ICollection<EmailRecipient> _to = new Collection<EmailRecipient>();
35         private ICollection<EmailRecipient> _cc = new Collection<EmailRecipient>();
36         private ICollection<EmailRecipient> _bcc = new Collection<EmailRecipient>();
37
38         /// <summary>
39         /// The constructor.
40         /// </summary>
41         /// <since_tizen> 3 </since_tizen>
42         public EmailMessage()
43         {
44             int ret = Interop.Email.CreateEmail(out _emailHandle);
45             if (ret != (int)EmailError.None)
46             {
47                 Log.Error(EmailErrorFactory.LogTag, "Failed to create message handle, Error code: " + (EmailError)ret);
48                 throw EmailErrorFactory.GetException(ret);
49             }
50         }
51
52         /// <summary>
53         /// The subject of the email message.
54         /// </summary>
55         /// <since_tizen> 3 </since_tizen>
56         public string Subject
57         {
58             set
59             {
60                 _subject = value;
61                 int ret = Interop.Email.SetSubject(_emailHandle, _subject);
62                 if (ret != (int)EmailError.None)
63                 {
64                     Log.Error(EmailErrorFactory.LogTag, "Failed to set subject, Error code: " + (EmailError)ret);
65                     throw EmailErrorFactory.GetException(ret);
66                 }
67             }
68             get
69             {
70                 return _subject;
71             }
72
73         }
74
75         /// <summary>
76         /// The body of the email message.
77         /// </summary>
78         /// <since_tizen> 3 </since_tizen>
79         public string Body
80         {
81             set
82             {
83                 _body = value;
84                 int ret = Interop.Email.SetBody(_emailHandle, _body);
85                 if (ret != (int)EmailError.None)
86                 {
87                     Log.Error(EmailErrorFactory.LogTag, "Failed to set body, Error code: " + (EmailError)ret);
88                     throw EmailErrorFactory.GetException(ret);
89                 }
90             }
91             get
92             {
93                 return _body;
94             }
95         }
96
97         /// <summary>
98         /// The list of file attachments.
99         /// </summary>
100         /// <since_tizen> 3 </since_tizen>
101         public IList<EmailAttachment> Attachments
102         {
103             get
104             {
105                 return _attachments;
106             }
107         }
108
109         /// <summary>
110         /// The collection of normal email recipients.
111         /// </summary>
112         /// <remarks>
113         /// The email address should be in the standard format (as described in the Internet standards RFC 5321 and RFC 5322).
114         /// </remarks>
115         /// <since_tizen> 3 </since_tizen>
116         public ICollection<EmailRecipient> To
117         {
118             get
119             {
120                 return _to;
121             }
122         }
123
124         /// <summary>
125         /// The collection of CC (carbon copy) email recipients.
126         /// </summary>
127         /// <remarks>
128         /// The email address should be in the standard format (as described in the Internet standards RFC 5321 and RFC 5322).
129         /// </remarks>
130         /// <since_tizen> 3 </since_tizen>
131         public ICollection<EmailRecipient> Cc
132         {
133             get
134             {
135                 return _cc;
136             }
137         }
138
139         /// <summary>
140         /// The collection of BCC (blind carbon copy) email recipients.
141         /// </summary>
142         /// <remarks>
143         /// The email address should be in the standard format (as described in the Internet standards RFC 5321 and RFC 5322).
144         /// </remarks>
145         /// <since_tizen> 3 </since_tizen>
146         public ICollection<EmailRecipient> Bcc
147         {
148             get
149             {
150                 return _bcc;
151             }
152         }
153
154
155         internal void Save()
156         {
157             int ret;
158             FillHandle();
159
160             ret = Interop.Email.SaveEmail(_emailHandle);
161             if (ret != (int)EmailError.None)
162             {
163                 Log.Error(EmailErrorFactory.LogTag, "Failed to save email, Error code: " + (EmailError)ret);
164                 throw EmailErrorFactory.GetException(ret);
165             }
166         }
167
168         /// <summary>
169         /// Releases all resources used by the EmailMessage.
170         /// </summary>
171         /// <since_tizen> 3 </since_tizen>
172         public void Dispose()
173         {
174             Dispose(true);
175             GC.SuppressFinalize(this);
176         }
177
178         /// <summary>
179         /// Releases all resources used by the EmailMessage.
180         /// </summary>
181         /// <param name="disposing">Disposing by User</param>
182         /// <since_tizen> 3 </since_tizen>
183         protected virtual void Dispose(bool disposing)
184         {
185             if (_disposed)
186                 return;
187
188             if (disposing)
189             {
190
191             }
192
193             if (_emailHandle != IntPtr.Zero)
194             {
195                 Interop.Email.DestroyEmail(_emailHandle);
196                 _emailHandle = IntPtr.Zero;
197             }
198             _disposed = true;
199         }
200
201         internal void FillHandle()
202         {
203             int ret = (int)EmailError.None;
204             foreach (EmailAttachment it in Attachments)
205             {
206                 Console.WriteLine(it.FilePath);
207                 ret = Interop.Email.AddAttachment(_emailHandle, it.FilePath);
208                 if (ret != (int)EmailError.None)
209                 {
210                     Log.Error(EmailErrorFactory.LogTag, "Failed to add attachment, Error code: " + (EmailError)ret);
211                     throw EmailErrorFactory.GetException(ret);
212                 }
213             }
214
215             foreach (EmailRecipient it in To)
216             {
217                 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.To, it.Address);
218                 if (ret != (int)EmailError.None)
219                 {
220                     Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
221                     throw EmailErrorFactory.GetException(ret);
222                 }
223             }
224
225             foreach (EmailRecipient it in Cc)
226             {
227                 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.Cc, it.Address);
228                 if (ret != (int)EmailError.None)
229                 {
230                     Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
231                     throw EmailErrorFactory.GetException(ret);
232                 }
233             }
234
235             foreach (EmailRecipient it in Bcc)
236             {
237                 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.Bcc, it.Address);
238                 if (ret != (int)EmailError.None)
239                 {
240                     Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
241                     throw EmailErrorFactory.GetException(ret);
242                 }
243             }
244         }
245     }
246 }