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 /// The class contains Messaging API to support sending email messages.
26 public class EmailMessage : IDisposable
28 internal IntPtr _emailHandle = IntPtr.Zero;
29 private bool _disposed = false;
30 private String _subject;
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>();
42 int ret = Interop.Email.CreateEmail(out _emailHandle);
43 if (ret != (int)EmailError.None)
45 Log.Error(EmailErrorFactory.LogTag, "Failed to create message handle, Error code: " + (EmailError)ret);
46 throw EmailErrorFactory.GetException(ret);
51 /// Subject of the email message
58 int ret = Interop.Email.SetSubject(_emailHandle, _subject);
59 if (ret != (int)EmailError.None)
61 Log.Error(EmailErrorFactory.LogTag, "Failed to set subject, Error code: " + (EmailError)ret);
62 throw EmailErrorFactory.GetException(ret);
73 /// Body of the email message
80 int ret = Interop.Email.SetBody(_emailHandle, _body);
81 if (ret != (int)EmailError.None)
83 Log.Error(EmailErrorFactory.LogTag, "Failed to set body, Error code: " + (EmailError)ret);
84 throw EmailErrorFactory.GetException(ret);
94 /// List of file attachments
97 /// The maximum attachment file size is 10 MB.
99 public IList<EmailAttachment> Attachments
108 /// Collection of normal email recipients
111 /// Email address should be in standard format (as described in Internet standards RFC 5321 and RFC 5322).
113 public ICollection<EmailRecipient> To
122 /// Collection of CC(carbon copy) email recipients
125 /// Email address should be in standard format (as described in Internet standards RFC 5321 and RFC 5322).
127 public ICollection<EmailRecipient> Cc
136 /// Collection of BCC(blind carbon copy) email recipients
139 /// Email address should be in standard format (as described in Internet standards RFC 5321 and RFC 5322).
141 public ICollection<EmailRecipient> Bcc
150 /// Saves the email message
157 ret = Interop.Email.SaveEmail(_emailHandle);
158 if (ret != (int)EmailError.None)
160 Log.Error(EmailErrorFactory.LogTag, "Failed to save email, Error code: " + (EmailError)ret);
161 throw EmailErrorFactory.GetException(ret);
165 public void Dispose()
168 GC.SuppressFinalize(this);
171 protected virtual void Dispose(bool disposing)
181 if (_emailHandle != IntPtr.Zero)
183 Interop.Email.DestroyEmail(_emailHandle);
184 _emailHandle = IntPtr.Zero;
189 internal void FillHandle()
191 int ret = (int)EmailError.None;
192 foreach (EmailAttachment it in Attachments)
194 Console.WriteLine(it.FilePath);
195 ret = Interop.Email.AddAttachment(_emailHandle, it.FilePath);
196 if (ret != (int)EmailError.None)
198 Log.Error(EmailErrorFactory.LogTag, "Failed to add attachment, Error code: " + (EmailError)ret);
199 throw EmailErrorFactory.GetException(ret);
203 foreach (EmailRecipient it in To)
205 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.To, it.Address);
206 if (ret != (int)EmailError.None)
208 Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
209 throw EmailErrorFactory.GetException(ret);
213 foreach (EmailRecipient it in Cc)
215 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.Cc, it.Address);
216 if (ret != (int)EmailError.None)
218 Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
219 throw EmailErrorFactory.GetException(ret);
223 foreach (EmailRecipient it in Bcc)
225 ret = Interop.Email.AddRecipient(_emailHandle, (int)Interop.EmailRecipientType.Bcc, it.Address);
226 if (ret != (int)EmailError.None)
228 Log.Error(EmailErrorFactory.LogTag, "Failed to add recipients, Error code: " + (EmailError)ret);
229 throw EmailErrorFactory.GetException(ret);