Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

MailMessage Attachment

$
0
0

I need to save my PDF MailMessage Attachment in a Folder
And after that read the PDF file from the folder.
Do I need any special permissions and How to code should be ? (Look at my code)

private void OnNewMessage(object sender, IdleMessageEventArgs e)
{
try
{
MailMessage msg = e.Client.GetMessage(e.MessageUID, FetchOptions.Normal);

            Device.BeginInvokeOnMainThread(() => {

                this.emailSubject.Text = msg.Subject;

                foreach (Attachment atc in msg.Attachments)
                {
                    System.IO.Stream strm = atc.ContentStream;

                    SaveAttachment(atc.Name, strm);
                }
            });                               

        }
        catch (Exception ex)
        {
            Application.Current.MainPage.DisplayAlert("OnNewMessage Failure", ex.ToString(), "OK");
        }
    }

private void SaveAttachment(string name, Stream data, string location = "temp")
    {
        try
        {      
            string filePath = Path.Combine(App.FolderPath, name);

            byte[] bArray = new byte[data.Length];

            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                using (data)
                {
                    data.Read(bArray, 0, (int)data.Length);
                }
                int length = bArray.Length;
                fs.Write(bArray, 0, length);

                ShowPDF(filePath, true);
                //ShowPDF("content:///" + filePath, true);

                Console.WriteLine(filePath);
            }
        }
        catch (Exception ex)
        {
            Application.Current.MainPage.DisplayAlert("SaveAttachment Failure", ex.ToString(), "OK");
        }

    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>