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