Answered Create an email with an attachment.

  • Tuesday, January 08, 2013 8:31 PM
     
     

    Hello,

    I have an windows store app (C#, XAML). I have implemented Sharing content, and got my data to show up in the content flyout using the default mail client that comes with Windows.  How the heck can I attaché a file to this email.  Does anyone have an example of attaching a file?

All Replies

  • Thursday, January 10, 2013 7:27 AM
    Moderator
     
     Answered

    Hi,

    We can share the files
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh871371(v=win.10).aspx

    But there is the problem, it seems that we cannot sharing the text and files at the same time.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Thursday, January 10, 2013 5:56 PM
     
     
    To send emails with any kind of attachment from a WP7 or WP8 app (soon from WinRT too) directly to Microsoft Hotmail SMTP server , you can use this component: http://www.windowsphonegeek.com/marketplace/components/livemailmessage

    Venetasoft

  • Monday, January 21, 2013 2:55 PM
     
     

    Hi,

    Very cool component, although, you can create a Outlook.com account with a user name of test@abcd.com

    Outlook.com will create an account like this, and your user name will be test@abcd.com

    I would suggest 2 things:

     1. Ommit the test for the suffix of the email (i.e. @Hotmail.com, @Outlook.com)
     2. Have it work for any smtp server.  I know you will have to then store the server settings, but this way it will work with any server.

    Thanks

  • Monday, January 21, 2013 3:24 PM
    Moderator
     
     
    Jesse's answer is correct. Please don't unmark it as there is no other correct answer for this scenario.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

  • Monday, January 21, 2013 7:10 PM
     
     

    Hi Matt,

    So how then can I send an email from my app with an attachment?  I don't want to vent but this seems like a trivial thing to ask.

  • Monday, January 21, 2013 7:39 PM
    Moderator
     
     

    Jesse's post answers the question.  It seems like there should be a way to do this but there is not, at this time.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

  • Monday, January 21, 2013 11:27 PM
    Moderator
     
     

    To clarify, the sharing contract allows your app to share data with the user's choice of apps. It is not an email specific API, but in many cases it is more appropriate than directly using email. The share source does not have any control over the share target: the user chooses the target, and the target choose how to interpret the data. The Sharing content source app sample  demonstrates how to share different types of data, including files.

    One possible target is the Mail application. If the user shares a file via Mail then the file will be attached to the mail. The user can provide a subject or other body text. The user could also choose to share the file with other apps (e.g. SkyDrive).

    Windows Store apps do not have an explicit email API, but they can use standard web service or socket calls to talk to services provided by email systems (Outlook.com, Exchange Web Services, Facebook, etc.). There are also 3rd party components which can simplify this further.

    Which approach is most appropriate depends on the app and the goal of the sharing. In many cases, allowing the user to share the data as they will is more appropriate than specifically targeting email. If the goal is to collect feedback from the customer then your app may be better off connecting to your dedicated feedback service rather than going through an email server. If some cases (especially if you are writing a share target) talking directly to the email server will be necessary.

    --Rob

  • Thursday, January 24, 2013 12:20 AM
     
      Has Code

    Hi Matt,

    I have an issue.  I found a 3rd party component for mail.  Here is what I am doing.  I have modified Sara Silva's code that exports data to a .csv file. (See https://github.com/saramgsilva/MyMSDNSamples)  Works great.  Then I started this thread of creating an email.....with this csv attachment.  I got them both to work.....separately.  So on my window I placed a button on it that would converge the two.  Meaning:

    1. Create the .csv file.
    2. Create an email and attach the above .csv file.

    private void cmdSendReport_Click(object sender, RoutedEventArgs e) {

    CreateEmailBody(); var csv = new CsvExport<MyEventViewModel>(eventitems.ToList(), txtEventName.Text, txtStartDate.Text); csv.ExportToFile(ApplicationData.Current.LocalSettings.Values["lastName"].ToString() + "_" + ApplicationData.Current.LocalSettings.Values["firstName"].ToString() + " - (" + txtEventName.Text + ").csv"); SendEmail(); }

    The result is, that in my inbox, I do get the email with my .csv, but it is empty. Almost like this one button event does not have enough time to do both?

    So then I created 2 buttons as an experiment. 

    1. First button, creates the .csv file.
    2. Second button creates the email, attaches above .csv file, then sends the email.

    When I separate them like this it works fine.  But when I try to converge them.  The .csv file is attached, but no data.  I have no clue where to start on this one.  Any ideas?

  • Thursday, January 24, 2013 12:29 AM
    Moderator
     
     

    I suspect that your ExportToFile call is asynchronous and hasn't finished creating its data.

    If you have it return a Task then you can await it before calling SendEmail. This will prevent SendEmail from being called until after the ExportToFile Task has completed. See Asynchronous programming (Windows Store apps) for more information on asynchronous programming.

    If you need more details on this I'd recommend creating a new thread since this is a different problem from initial one discussed on this one.

    --Rob