2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
21 namespace Tizen.Messaging.Email
24 /// This class contains the Messaging API to support sending email messages.
26 /// <since_tizen> 3 </since_tizen>
27 public class EmailMessage : IDisposable
29 internal IntPtr _emailHandle = IntPtr.Zero;
30 private bool _disposed = false;
31 private String _subject;
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>();
41 /// <since_tizen> 3 </since_tizen>
44 int ret = Interop.Email.CreateEmail(out _emailHandle);
45 if (ret != (int)EmailError.None)
47 Log.Error(EmailErrorFactory.LogTag, "Failed to create message handle, Error code: " + (EmailError)ret);
48 throw EmailErrorFactory.GetException(ret);
53 /// The subject of the email message.
55 /// <since_tizen> 3 </since_tizen>
61 int ret = Interop.Email.SetSubject(_emailHandle, _subject);
62 if (ret != (int)EmailError.None)
64 Log.Error(EmailErrorFactory.LogTag, "Failed to set subject, Error code: " + (EmailError)ret);
65 throw EmailErrorFactory.GetException(ret);
76 /// The body of the email message.
78 /// <since_tizen> 3 </since_tizen>
84 int ret = Interop.Email.SetBody(_emailHandle, _body);
85 if (ret != (int)EmailError.None)
87 Log.Error(EmailErrorFactory.LogTag, "Failed to set body, Error code: " + (EmailError)ret);
88 throw EmailErrorFactory.GetException(ret);
98 /// The list of file attachments.
100 /// <since_tizen> 3 </since_tizen>
101 public IList<EmailAttachment> Attachments
110 /// The collection of normal email recipients.
113 /// The email address should be in the standard format (as described in the Internet standards RFC 5321 and RFC 5322).
115 /// <since_tizen> 3 </since_tizen>
116 public ICollection<EmailRecipient> To
125 /// The collection of CC (carbon copy) email recipients.
128 /// The email address should be in the standard format (as described in the Internet standards RFC 5321 and RFC 5322).
130 /// <since_tizen> 3 </since_tizen>
131 public ICollection<EmailRecipient> Cc
140 /// The collection of BCC (blind carbon copy) email recipients.
143 /// The email address should be in the standard format (as described in the Internet standards RFC 5321 and RFC 5322).
145 /// <since_tizen> 3 </since_tizen>
146 public ICollection<EmailRecipient> Bcc
160 ret = Interop.Email.SaveEmail(_emailHandle);
161 if (ret != (int)EmailError.None)
163 Log.Error(EmailErrorFactory.LogTag, "Failed to save email, Error code: " + (EmailError)ret);
164 throw EmailErrorFactory.GetException(ret);
169 /// Releases all resources used by the EmailMessage.
171 /// <since_tizen> 3 </since_tizen>
172 public void Dispose()
175 GC.SuppressFinalize(this);
179 /// Releases all resources used by the EmailMessage.
181 /// <param name="disposing">Disposing by User</param>
182 /// <since_tizen> 3 </since_tizen>
183 protected virtual void Dispose(bool disposing)
193 if (_emailHandle != IntPtr.Zero)
195 Interop.Email.DestroyEmail(_emailHandle);
196 _emailHandle = IntPtr.Zero;
201 internal void FillHandle()
203 int ret = (int)EmailError.None;
204 foreach (EmailAttachment it in Attachments)
206 Console.WriteLine(it.FilePath);
207 ret = Interop.Email.AddAttachment(_emailHandle, it.FilePath);
208 if (ret != (int)EmailError.None)
210 Log.Error(EmailErrorFactory.LogTag, "Failed to add attachment, Error code: " + (EmailError)ret);
211 throw EmailErrorFactory.GetException(ret);
215 foreach (EmailRecipient it in To)
217 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.To, it.Address);
218 if (ret != (int)EmailError.None)
220 Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
221 throw EmailErrorFactory.GetException(ret);
225 foreach (EmailRecipient it in Cc)
227 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.Cc, it.Address);
228 if (ret != (int)EmailError.None)
230 Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
231 throw EmailErrorFactory.GetException(ret);
235 foreach (EmailRecipient it in Bcc)
237 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.Bcc, it.Address);
238 if (ret != (int)EmailError.None)
240 Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
241 throw EmailErrorFactory.GetException(ret);