8755f16a274c19acaeff35bb9097b6aebcc47078
[platform/core/csapi/messaging.git] / 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     /// The class contains 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         /// 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         /// 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         /// List of file attachments
95         /// </summary>
96         /// <remarks>
97         /// The maximum attachment file size is 10 MB.
98         /// </remarks>
99         public IList<EmailAttachment> Attachments
100         {
101             get
102             {
103                 return _attachments;
104             }
105         }
106
107         /// <summary>
108         /// Collection of normal email recipients
109         /// </summary>
110         /// <remarks>
111         /// Email address should be in standard format (as described in Internet standards RFC 5321 and RFC 5322).
112         /// </remarks>
113         public ICollection<EmailRecipient> To
114         {
115             get
116             {
117                 return _to;
118             }
119         }
120
121         /// <summary>
122         /// Collection of CC(carbon copy) email recipients
123         /// </summary>
124         /// <remarks>
125         /// Email address should be in standard format (as described in Internet standards RFC 5321 and RFC 5322).
126         /// </remarks>
127         public ICollection<EmailRecipient> Cc
128         {
129             get
130             {
131                 return _cc;
132             }
133         }
134
135         /// <summary>
136         /// Collection of BCC(blind carbon copy) email recipients
137         /// </summary>
138         /// <remarks>
139         /// Email address should be in standard format (as described in Internet standards RFC 5321 and RFC 5322).
140         /// </remarks>
141         public ICollection<EmailRecipient> Bcc
142         {
143             get
144             {
145                 return _bcc;
146             }
147         }
148
149         /// <summary>
150         /// Saves the email message
151         /// </summary>
152         public void Save()
153         {
154             int ret;
155             FillHandle();
156
157             ret = Interop.Email.SaveEmail(_emailHandle);
158             if (ret != (int)EmailError.None)
159             {
160                 Log.Error(EmailErrorFactory.LogTag, "Failed to save email, Error code: " + (EmailError)ret);
161                 throw EmailErrorFactory.GetException(ret);
162             }
163         }
164
165         public void Dispose()
166         {
167             Dispose(true);
168             GC.SuppressFinalize(this);
169         }
170
171         protected virtual void Dispose(bool disposing)
172         {
173             if (_disposed)
174                 return;
175
176             if (disposing)
177             {
178
179             }
180
181             if (_emailHandle != IntPtr.Zero)
182             {
183                 Interop.Email.DestroyEmail(_emailHandle);
184                 _emailHandle = IntPtr.Zero;
185             }
186             _disposed = true;
187         }
188
189         internal void FillHandle()
190         {
191             int ret = (int)EmailError.None;
192             foreach (EmailAttachment it in Attachments)
193             {
194                 Console.WriteLine(it.FilePath);
195                 ret = Interop.Email.AddAttachment(_emailHandle, it.FilePath);
196                 if (ret != (int)EmailError.None)
197                 {
198                     Log.Error(EmailErrorFactory.LogTag, "Failed to add attachment, Error code: " + (EmailError)ret);
199                     throw EmailErrorFactory.GetException(ret);
200                 }
201             }
202
203             foreach (EmailRecipient it in To)
204             {
205                 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.To, it.Address);
206                 if (ret != (int)EmailError.None)
207                 {
208                     Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
209                     throw EmailErrorFactory.GetException(ret);
210                 }
211             }
212
213             foreach (EmailRecipient it in Cc)
214             {
215                 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.Cc, it.Address);
216                 if (ret != (int)EmailError.None)
217                 {
218                     Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
219                     throw EmailErrorFactory.GetException(ret);
220                 }
221             }
222
223             foreach (EmailRecipient it in Bcc)
224             {
225                 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.Bcc, it.Address);
226                 if (ret != (int)EmailError.None)
227                 {
228                     Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
229                     throw EmailErrorFactory.GetException(ret);
230                 }
231             }
232         }
233     }
234 }