Send mail and attach file with a web service


What I want is to be able to send an email with data that is digitized from my Windows CE project, the problem is that I create the website and put my two codes both the email and export data to excel but then two problems arise.

  • With the subject of excel, how do I tell my program to choose the 14 Labels and the 7 TextBox that are found in my Windows CE project ?, Because if I make the call it tells me that there are no such Labels and TextBoxes.

  • How can the mail code which is also in the web service send me the information of that Excel since it sends me the file but sends it to me with some words that I gave as an example? , if I call the method which contains the excel export within that I get an error.

  • How do I call that service from my Windows CE project?

  • I enclose the codes I am handling


    public void exporta_a_excel()
        Microsoft.Office.Interop.Excel.ApplicationClass excel = new ApplicationClass();
        int ColumnIndex = 0;
        foreach (DataGridViewColumn col in dataGridView1.Columns)
            excel.Cells[1, ColumnIndex] = col.Name;
        int rowIndex = 0;
        foreach (DataGridViewRow row in dataGridView1.Rows)
            ColumnIndex = 0;
            foreach (DataGridViewColumn col in dataGridView1.Columns)
                excel.Cells[rowIndex + 1, ColumnIndex] = row.Cells[col.Name].Value;
        excel.Visible = true;
        Worksheet worksheet = (Worksheet)excel.ActiveSheet;


    public void Correo()
        MailMessage objeto_mail = new MailMessage();
        SmtpClient client = new SmtpClient();
        client.Port = 587;
        client.Host = "";
        client.Timeout = 10000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("[email protected]", "xxxx");
        objeto_mail.From = new MailAddress("[email protected]");
        objeto_mail.To.Add(new MailAddress("[email protected]"));
        objeto_mail.To.Add(new MailAddress("[email protected]"));
        objeto_mail.To.Add(new MailAddress("[email protected]"));
        objeto_mail.Subject = "Pedidos";
        //List<string> Archivo = new List<string>();
        objeto_mail.IsBodyHtml = false;
        objeto_mail.Body = "se hizo el pedido y la orden esta adjuntada en el correo con un excell";
        client.EnableSsl = true;
        using (var stream = new MemoryStream())
        using (var writer = new StreamWriter(stream))    // using UTF-8 encoding by default
        // using (var mailClient = new SmtpClient("localhost", 25))
        // using (var message = new MailMessage("[email protected]", "[email protected]", "Just testing", "See attachment..."))
            writer.WriteLine("Comma,Seperated,Values,..."); //ESTO ME SALED EN EL ARCHIVO EXCEL
            stream.Position = 0;     // read from the start of what was written
            objeto_mail.Attachments.Add(new Attachment(stream, "Pedido.csv", "text/csv"));

    All this is in a web service, and make the call from the form of Windows CE in which is the database, the Labels and the TextBox and which until now makes the operation to update the data, the which must be inserted in the Excel which is sent by email.

    The liberias that I am using for the mail are:

    using OpenNETCF.Net.Mail;
    using OpenNETCF.Net;

    And the data that should go in the Excel are these:

    asked by Cristian R.M 23.08.2016 в 18:06

    1 answer


    I do not understand very well what your problem is, however I could suggest some things to you. Instead of directly using Microsoft.Office.Interop.Excel uses EPPlus , since you are prepared to perform excel books quite advanced in a web server. You can read there is a lot of documentation on the web. Here there is an example.

    As for how to pass the data (or parameters, I do not understand very well) from a client to the server you could do it by sending the data in JSON format. Of that you would have to explain a little better what is happening and where are your doubts since I do not see the problem clear.

    answered by 01.12.2016 в 17:23