Represents an email message.
PlatformVersion supported AndroidAndroid 4.4 and later iOSiOS 9.0 and later Windows UWPWindows 10 Windows StoreWindows 8.1 or later Windows Phone StoreWindows Phone 8.1 or later Windows Phone SilverlightWindows Phone 8.0 or later Windows (Desktop Apps)Windows Vista or later
示例#1
0
        private static void ShowComposeNewEmailImpl(EmailMessage message)
        {
            bool useAnsi    = global::System.Environment.OSVersion.Version < new Version(6, 2);
            int  recipCount = message.To.Count + message.CC.Count + message.Bcc.Count;

            NativeMethods.MapiMessage msg = new NativeMethods.MapiMessage();
            if (useAnsi)
            {
                if (!string.IsNullOrEmpty(message.Subject))
                {
                    msg.lpszSubject = Marshal.StringToHGlobalAnsi(message.Subject);
                }

                if (!string.IsNullOrEmpty(message.Body))
                {
                    msg.lpszNoteText = Marshal.StringToHGlobalAnsi(message.Body);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(message.Subject))
                {
                    msg.lpszSubject = Marshal.StringToHGlobalUni(message.Subject);
                }

                if (!string.IsNullOrEmpty(message.Body))
                {
                    msg.lpszNoteText = Marshal.StringToHGlobalUni(message.Body);
                }
            }

            // recipients

            if (recipCount > 0)
            {
                int recipdesclen = Marshal.SizeOf <NativeMethods.MapiRecipDesc>();
                msg.lpRecips    = Marshal.AllocHGlobal(recipdesclen * recipCount);
                msg.nRecipCount = recipCount;
                int currentRecip = 0;

                foreach (EmailRecipient r in message.To)
                {
                    if (useAnsi)
                    {
                        var rd = new NativeMethods.MapiRecipDesc(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_TO);
                        Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false);
                    }
                    else
                    {
                        var rd = new NativeMethods.MapiRecipDescW(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_TO);
                        Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false);
                    }

                    currentRecip++;
                }

                foreach (EmailRecipient r in message.CC)
                {
                    if (useAnsi)
                    {
                        var rd = new NativeMethods.MapiRecipDesc(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_CC);
                        Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false);
                    }
                    else
                    {
                        var rd = new NativeMethods.MapiRecipDescW(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_CC);
                        Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false);
                    }

                    currentRecip++;
                }

                foreach (EmailRecipient r in message.Bcc)
                {
                    if (useAnsi)
                    {
                        var rd = new NativeMethods.MapiRecipDesc(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_BCC);
                        Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false);
                    }
                    else
                    {
                        var rd = new NativeMethods.MapiRecipDescW(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_BCC);
                        Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false);
                    }

                    currentRecip++;
                }
            }

            // attachments
            if (message.Attachments.Count > 0)
            {
                int fileDescLen = Marshal.SizeOf <NativeMethods.MapiFileDesc>();
                msg.lpFiles    = Marshal.AllocHGlobal(fileDescLen * message.Attachments.Count);
                msg.nFileCount = message.Attachments.Count;

                for (int i = 0; i < message.Attachments.Count; i++)
                {
                    if (useAnsi)
                    {
                        NativeMethods.MapiFileDesc f = new Email.EmailManager.NativeMethods.MapiFileDesc(message.Attachments[i].Data.Path, message.Attachments[i].FileName);
                        Marshal.StructureToPtr(f, IntPtr.Add(msg.lpFiles, fileDescLen * i), false);
                    }
                    else
                    {
                        NativeMethods.MapiFileDescW f = new Email.EmailManager.NativeMethods.MapiFileDescW(message.Attachments[i].Data.Path, message.Attachments[i].FileName);
                        Marshal.StructureToPtr(f, IntPtr.Add(msg.lpFiles, fileDescLen * i), false);
                    }
                }
            }

            if (useAnsi)
            {
                uint result = NativeMethods.MAPISendMail(IntPtr.Zero, IntPtr.Zero, ref msg, 0xd, 0);
            }
            else
            {
                uint result = NativeMethods.MAPISendMailW(IntPtr.Zero, IntPtr.Zero, ref msg, 0xd, 0);
            }

            FreeMapiMessage(msg);
        }
示例#2
0
        /// <summary>
        /// Launches the email application with a new message displayed.
        /// </summary>
        /// <param name="message">The email message that is displayed when the email application is launched.</param>
        /// <returns>An asynchronous action used to indicate when the operation has completed.</returns>
        public static Task ShowComposeNewEmailAsync(EmailMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException();
            }

            return Task.Run(() =>
            {
#if __ANDROID__
                Intent emailIntent = new Intent(Intent.ActionSendto);
                //emailIntent.SetType("message/rfc822");
                emailIntent.SetData(Android.Net.Uri.Parse("mailto:"));
                emailIntent.PutExtra(Intent.ExtraEmail, RecipientsToStringArray(message.To));
                if (message.CC.Count > 0)
                {
                    emailIntent.PutExtra(Intent.ExtraCc, RecipientsToStringArray(message.CC));
                }

                if (message.Bcc.Count > 0)
                {
                    emailIntent.PutExtra(Intent.ExtraBcc, RecipientsToStringArray(message.Bcc));
                }
                emailIntent.PutExtra(Intent.ExtraSubject, message.Subject);
                emailIntent.PutExtra(Intent.ExtraText, message.Body);
                emailIntent.AddFlags(ActivityFlags.ClearWhenTaskReset);
                Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity.StartActivity(emailIntent);
                //Platform.Android.ContextManager.Context.StartActivity(emailIntent);           
#elif __IOS__
                try
                {
                    UIApplication.SharedApplication.InvokeOnMainThread(() =>
                    {
                        MFMailComposeViewController mcontroller = new MFMailComposeViewController();
                        mcontroller.Finished += mcontroller_Finished;

                        mcontroller.SetToRecipients(BuildRecipientArray(message.To));
                        mcontroller.SetCcRecipients(BuildRecipientArray(message.CC));
                        mcontroller.SetBccRecipients(BuildRecipientArray(message.Bcc));
                        mcontroller.SetSubject(message.Subject);
                        mcontroller.SetMessageBody(message.Body, false);
                        foreach (EmailAttachment a in message.Attachments)
                        {
                            NSData dataBuffer = NSData.FromFile(a.Data.Path);
                            mcontroller.AddAttachmentData(dataBuffer, a.MimeType, a.FileName);
                        }

                        UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
                        while (currentController.PresentedViewController != null)
                            currentController = currentController.PresentedViewController;

                        currentController.PresentViewController(mcontroller, true, null);
                    });
                }
                catch
                {
                    throw new PlatformNotSupportedException();
                }
#elif WINDOWS_UWP || WINDOWS_PHONE_APP
                Windows.ApplicationModel.Email.EmailMessage em = new Windows.ApplicationModel.Email.EmailMessage() { Subject = message.Subject, Body = message.Body };
                foreach(EmailRecipient r in message.To)
                {
                    em.To.Add(new Windows.ApplicationModel.Email.EmailRecipient(r.Address, r.Name));
                }
                foreach(EmailRecipient r in message.CC)
                {
                    em.CC.Add(new Windows.ApplicationModel.Email.EmailRecipient(r.Address, r.Name));
                }
                foreach (EmailRecipient r in message.Bcc)
                {
                    em.Bcc.Add(new Windows.ApplicationModel.Email.EmailRecipient(r.Address, r.Name));
                }
                foreach(EmailAttachment a in message.Attachments)
                {
                    em.Attachments.Add(new Windows.ApplicationModel.Email.EmailAttachment(a.FileName, (Windows.Storage.StorageFile)a.Data));
                }

                return Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(em).AsTask();
#elif WINDOWS_APP
                /*if (_type10 != null)
                {

                }
                else
                {*/
                    // build URI for Windows Store platform

                    // build a uri
                    StringBuilder sb = new StringBuilder();
                    bool firstParameter = true;

                    if (message.To.Count == 0)
                    {
                        throw new InvalidOperationException();
                    }
                    else
                    {
                        sb.Append("mailto:" + FormatRecipientCollection(message.To));
                    }

                    if (message.CC.Count > 0)
                    {
                        if (firstParameter)
                        {
                            sb.Append("?");
                            firstParameter = false;
                        }
                        else
                        {
                            sb.Append("&");
                        }

                        sb.Append("cc=" + FormatRecipientCollection(message.CC));
                    }

                    if (message.Bcc.Count > 0)
                    {
                        if (firstParameter)
                        {
                            sb.Append("?");
                            firstParameter = false;
                        }
                        else
                        {
                            sb.Append("&");
                        }

                        sb.Append("bbc=" + FormatRecipientCollection(message.Bcc));
                    }

                    if (!string.IsNullOrEmpty(message.Subject))
                    {
                        if (firstParameter)
                        {
                            sb.Append("?");
                            firstParameter = false;
                        }
                        else
                        {
                            sb.Append("&");
                        }

                        sb.Append("subject=" + Uri.EscapeDataString(message.Subject));
                    }

                    if (!string.IsNullOrEmpty(message.Body))
                    {
                        if (firstParameter)
                        {
                            sb.Append("?");
                            firstParameter = false;
                        }
                        else
                        {
                            sb.Append("&");
                        }

                        sb.Append("body=" + Uri.EscapeDataString(message.Body));
                    }

                    Windows.System.Launcher.LaunchUriAsync(new Uri(sb.ToString()));
                //}
#elif WINDOWS_PHONE
                // On Phone 8.0 use the email compose dialog
                EmailComposeTask task = new EmailComposeTask();

                if (!string.IsNullOrEmpty(message.Subject))
                {
                    task.Subject = message.Subject;
                }

                if (!string.IsNullOrEmpty(message.Body))
                {
                    task.Body = message.Body;
                }

                if (message.To.Count == 0)
                {
                    throw new InvalidOperationException();
                }
                else
                {
                    task.To = FormatRecipientCollection(message.To);
                }

                if (message.CC.Count > 0)
                {
                    task.Cc = FormatRecipientCollection(message.CC);
                }

                if (message.Bcc.Count > 0)
                {
                    task.Bcc = FormatRecipientCollection(message.Bcc);
                }

                task.Show();
#else
                throw new PlatformNotSupportedException();
#endif
            });
        }
示例#3
0
        /// <summary>
        /// Launches the email application with a new message displayed.
        /// </summary>
        /// <param name="message">The email message that is displayed when the email application is launched.</param>
        /// <returns>An asynchronous action used to indicate when the operation has completed.</returns>
        public static Task ShowComposeNewEmailAsync(EmailMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException();
            }

            return(Task.Run(() =>
            {
#if __ANDROID__
                Intent emailIntent = new Intent(Intent.ActionSendto);
                //emailIntent.SetType("message/rfc822");
                emailIntent.SetData(Android.Net.Uri.Parse("mailto:"));
                emailIntent.PutExtra(Intent.ExtraEmail, RecipientsToStringArray(message.To));
                if (message.CC.Count > 0)
                {
                    emailIntent.PutExtra(Intent.ExtraCc, RecipientsToStringArray(message.CC));
                }

                if (message.Bcc.Count > 0)
                {
                    emailIntent.PutExtra(Intent.ExtraBcc, RecipientsToStringArray(message.Bcc));
                }
                emailIntent.PutExtra(Intent.ExtraSubject, message.Subject);
                emailIntent.PutExtra(Intent.ExtraText, message.Body);
                emailIntent.AddFlags(ActivityFlags.ClearWhenTaskReset);
                Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity.StartActivity(emailIntent);
                //Platform.Android.ContextManager.Context.StartActivity(emailIntent);
#elif __IOS__
                try
                {
                    UIApplication.SharedApplication.InvokeOnMainThread(() =>
                    {
                        MFMailComposeViewController mcontroller = new MFMailComposeViewController();
                        mcontroller.Finished += mcontroller_Finished;

                        mcontroller.SetToRecipients(BuildRecipientArray(message.To));
                        mcontroller.SetCcRecipients(BuildRecipientArray(message.CC));
                        mcontroller.SetBccRecipients(BuildRecipientArray(message.Bcc));
                        mcontroller.SetSubject(message.Subject);
                        mcontroller.SetMessageBody(message.Body, false);
                        foreach (EmailAttachment a in message.Attachments)
                        {
                            NSData dataBuffer = NSData.FromFile(a.Data.Path);
                            mcontroller.AddAttachmentData(dataBuffer, a.MimeType, a.FileName);
                        }

                        UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
                        while (currentController.PresentedViewController != null)
                        {
                            currentController = currentController.PresentedViewController;
                        }

                        currentController.PresentViewController(mcontroller, true, null);
                    });
                }
                catch
                {
                    throw new PlatformNotSupportedException();
                }
#elif WINDOWS_UWP || WINDOWS_PHONE_APP
                Windows.ApplicationModel.Email.EmailMessage em = new Windows.ApplicationModel.Email.EmailMessage()
                {
                    Subject = message.Subject, Body = message.Body
                };
                foreach (EmailRecipient r in message.To)
                {
                    em.To.Add(new Windows.ApplicationModel.Email.EmailRecipient(r.Address, r.Name));
                }
                foreach (EmailRecipient r in message.CC)
                {
                    em.CC.Add(new Windows.ApplicationModel.Email.EmailRecipient(r.Address, r.Name));
                }
                foreach (EmailRecipient r in message.Bcc)
                {
                    em.Bcc.Add(new Windows.ApplicationModel.Email.EmailRecipient(r.Address, r.Name));
                }
                foreach (EmailAttachment a in message.Attachments)
                {
                    em.Attachments.Add(new Windows.ApplicationModel.Email.EmailAttachment(a.FileName, (Windows.Storage.StorageFile)a.Data));
                }

                return Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(em).AsTask();
#elif WINDOWS_APP || __MAC__
                /*if (_type10 != null)
                 * {
                 *
                 * }
                 * else
                 * {*/
                // build URI

                // build a uri
                StringBuilder sb = new StringBuilder();
                bool firstParameter = true;

                if (message.To.Count == 0)
                {
                    throw new InvalidOperationException();
                }
                else
                {
                    sb.Append("mailto:" + FormatRecipientCollection(message.To));
                }

                if (message.CC.Count > 0)
                {
                    if (firstParameter)
                    {
                        sb.Append("?");
                        firstParameter = false;
                    }
                    else
                    {
                        sb.Append("&");
                    }

                    sb.Append("cc=" + FormatRecipientCollection(message.CC));
                }

                if (message.Bcc.Count > 0)
                {
                    if (firstParameter)
                    {
                        sb.Append("?");
                        firstParameter = false;
                    }
                    else
                    {
                        sb.Append("&");
                    }

                    sb.Append("bbc=" + FormatRecipientCollection(message.Bcc));
                }

                if (!string.IsNullOrEmpty(message.Subject))
                {
                    if (firstParameter)
                    {
                        sb.Append("?");
                        firstParameter = false;
                    }
                    else
                    {
                        sb.Append("&");
                    }

                    sb.Append("subject=" + Uri.EscapeDataString(message.Subject));
                }

                if (!string.IsNullOrEmpty(message.Body))
                {
                    if (firstParameter)
                    {
                        sb.Append("?");
                        firstParameter = false;
                    }
                    else
                    {
                        sb.Append("&");
                    }

                    sb.Append("body=" + Uri.EscapeDataString(message.Body));
                }

                InTheHand.System.Launcher.LaunchUriAsync(new Uri(sb.ToString()));
                //}
#elif WINDOWS_PHONE
                // On Phone 8.0 use the email compose dialog
                EmailComposeTask task = new EmailComposeTask();

                if (!string.IsNullOrEmpty(message.Subject))
                {
                    task.Subject = message.Subject;
                }

                if (!string.IsNullOrEmpty(message.Body))
                {
                    task.Body = message.Body;
                }

                if (message.To.Count == 0)
                {
                    throw new InvalidOperationException();
                }
                else
                {
                    task.To = FormatRecipientCollection(message.To);
                }

                if (message.CC.Count > 0)
                {
                    task.Cc = FormatRecipientCollection(message.CC);
                }

                if (message.Bcc.Count > 0)
                {
                    task.Bcc = FormatRecipientCollection(message.Bcc);
                }

                task.Show();
#elif WIN32
                ShowComposeNewEmailImpl(message);
#else
                throw new PlatformNotSupportedException();
#endif
            }));
        }