none
ENVIAR IMAGEN POR CORREO HECHO EN C# RRS feed

  • Pregunta

  •  

       Hola a todos.  Espero que me puedan ayudar.   Estoy haciendo un correo en c# el problema surge cuando quiero enviar imagenes.  Antes de enviar la imagen la serializo pero la persona que recibe no puede deserializarla.  Alguien sabe que puedo hacer.

     

    GRACIAS

    miércoles, 4 de julio de 2007 17:26

Respuestas

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Windows.Forms;

    namespace Chat
    {
        public partial class Form1 : Form
        {
          
            public Form1()
            {            
                InitializeComponent();
            }

           
           
            private void btnSend_Click(object sender, EventArgs e)
            {
                try
                {
                    //escribe la cadena al puerto serial
                    serialPort.Write(txtDataToSend.Text +
                    Environment.NewLine);

                    //Añadir la cadena envía al control TextBox
                    txtDataReceived.AppendText(">" + txtDataToSend.Text +
                    Environment.NewLine);
                    txtDataReceived.ScrollToCaret();

                    //enviar mensahe
                    serialPort.Write("AT+CSCA= " + txtPhoneNumber.Text + Environment.NewLine);


                    //despeja el control TextBox            
                    txtDataToSend.Text = string.Empty;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                //---set the event handler for the DataReceived event---
                serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(DataReceived);
                //---display all the serial port names on the local
                // computer---
                string[] portNames =
                System.IO.Ports.SerialPort.GetPortNames();
                for (int i = 0; i <= portNames.Length - 1; i++)
                {
                    cbbCOMPorts.Items.Add(portNames[i]);
                }
                btnDisconnect.Enabled = false;
            }

            private void btnConnect_Click(object sender, EventArgs e)
            {
                //---close the serial port if it is open---
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                }
                try
                {
                    //---configure the various parameters of the serial
                    // port---
                    serialPort.PortName = cbbCOMPorts.Text;
                    serialPort.BaudRate = 9600;
                    serialPort.Parity = System.IO.Ports.Parity.None;
                    serialPort.DataBits = 8;
                    serialPort.StopBits = System.IO.Ports.StopBits.One;
                    //---open the serial port---
                    serialPort.Open();
                    //---update the status of the serial port and
                    // enable/disable the buttons---
                    lblMessage.Text = cbbCOMPorts.Text + "CONECTADO";
                    btnConnect.Enabled = false;
                    btnDisconnect.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            private void btnDisconnect_Click(object sender, EventArgs e)
            {
                try
                {
                    //---close the serial port---
                    serialPort.Close();
                    //---update the status of the serial port and
                    // enable/disable the buttons---
                    lblMessage.Text = serialPort.PortName + "DESCONECTADO";
                    btnConnect.Enabled = true;
                    btnDisconnect.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            private void btnDisconnect_Click_1(object sender, EventArgs e)
            {
                try
                {
                    //---close the serial port---
                    serialPort.Close();
                    //---update the status of the serial port and
                    // enable/disable the buttons---
                    lblMessage.Text = serialPort.PortName + "DESCONECTADO";
                    btnConnect.Enabled = true;
                    btnDisconnect.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            private void DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                //---invoke the delegate to retrieve the received data---
                txtDataReceived.BeginInvoke(new myDelegate(updateTextBox));
            }

            //---Delegate and subroutine to update the TextBox control---
            public delegate void myDelegate();
            public void updateTextBox()
            {
                //---append the received data into the TextBox control---
                txtDataReceived.AppendText(serialPort.ReadExisting());
                txtDataReceived.ScrollToCaret();
            }

            private void btnExaminar_Click(object sender, EventArgs e)
            {
                String archivo = "";
                this.openFileDialog1.ShowDialog();
                archivo = this.openFileDialog1.FileName;
                this.pictureBox1.ImageLocation = archivo;
              
            }

         
            private void marcarNum_Click_1(object sender, EventArgs e)
            {
                //---write the AT command ATDT to the serial port---
                serialPort.Write("ATDT " + txtPhoneNumber.Text + Environment.NewLine);
               
               
            }

            private void responderLlam_Click(object sender, EventArgs e)
            {
                //---write the AT command AT*EVA to the serial port---
                serialPort.Write("AT*EVA" + Environment.NewLine);
            }




            //arreglo de bytes///////////////////////////////////////////

            //private byte[] imageToByteArray(System.Drawing.Image imageln)
            //{
            //    MemoryStream ms = new MemoryStream();           
            //    imageln.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            //    return ms.ToArray();
               
            //    FileStream st=new FileStream(("dato.out"),Filemode.Create))

            //}

            //public Image byteArrayToImage(byte[] byteArrayln)
            //{
            //    MemoryStream ms = new MemoryStream(byteArrayln);
               
            //    Image returImage = Image.FromStream(ms);
            //    return returImage;
            //}

          
            /// //////////////////////////////////////////////////////////
           

            private void btnEnviar_Click(object sender, EventArgs e)
            {
                String archivo="";
                FileStream stream = new FileStream("dato.out", FileMode.Create);
                BinaryFormatter file = new BinaryFormatter();
                file.Serialize(stream,archivo);
                byte[] buffer = new byte[1000];
                int size = stream.Read(buffer, 0, buffer.Length);
                //socket.Close();
                //socket.Send(buffer);
            }

            private void pictureBox1_Click(object sender, EventArgs e)
            {
               
                FileStream stream = new FileStream("data.out", FileMode.Create);
                byte[] resu = new Byte[Reci];
                stream.Write(resu, 0, resu.Length);
                stream.Close();
                BinaryFormatter binaryformatter = new BinaryFormatter();
                FileStream streams = new FileStream("data.out", FileMode.Open);
                Bitmap im;
                im = (Bitmap)binaryformatter.Deserialize(streams);
            }

             /////////////////////////////////////////////////////////////////////////////////
         

           

          
        }
    }
    martes, 25 de mayo de 2010 22:42

Todas las respuestas

  • ¿A que te refieres con serializar? Mira éste artículo sobre como enviar correo y archivos adjuntos

    http://www.malagadnug.org/recursos/articulos/correoelectronico/index.ES.aspx


    miércoles, 4 de julio de 2007 20:08
  • Hola:

      

       Bueno por serializar me refiero al proceso de codificación de un objeto en un medio de almacenamiento con el fin de tranmitirlo a trevés de la red.

      En este caso yo estoy serializando una imagen bmp, la estoy almacenando en un buffer y la estoy enviando como una serie de bytes,  el problema es que no puedo deserializarlo cuando llega a su destino.  Para enviarlo estoy utilizando socket.

      El código para enviarlo es:

                          FileStream stream = new FileStream("Dato.out", FileMode.Create);

                         BinaryFormatter file = new BinaryFormatter();

                         file.Serialize(stream,bmp);

                         byte[] buffer = new byte[1000];

                         int size = stream.Read(buffer,0,buffer.Length);

                         stream.Close();

                          ocket.Send (buffer);

    Y para recibirlo:

                         FileStream stream = new FileStream("Data.out",FileMode.Create);

                        byte[] resu = new Byte[Reci];

                        stream.Write(resu,0,resu.Length);

                        stream.Close();

                        BinaryFormatter binaryformatter = new BinaryFormatter();

                        FileStream streams = new FileStream("Data.out", FileMode.Open);

                       Bitmap im;

                       im = (Bitmap)binaryformatter.Deserialize(streams);

     

      ¿Alguien tiene alguna idea de que puedo hacer?

    jueves, 5 de julio de 2007 16:31
  • Pero para que estás haciendo eso? Si quieres mandarlo por correo, al adjuntarlo como archivo se codifica en formato MIME para su envío. Quieres utilizar correo o no?
    jueves, 5 de julio de 2007 16:48
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Windows.Forms;

    namespace Chat
    {
        public partial class Form1 : Form
        {
          
            public Form1()
            {            
                InitializeComponent();
            }

           
           
            private void btnSend_Click(object sender, EventArgs e)
            {
                try
                {
                    //escribe la cadena al puerto serial
                    serialPort.Write(txtDataToSend.Text +
                    Environment.NewLine);

                    //Añadir la cadena envía al control TextBox
                    txtDataReceived.AppendText(">" + txtDataToSend.Text +
                    Environment.NewLine);
                    txtDataReceived.ScrollToCaret();

                    //enviar mensahe
                    serialPort.Write("AT+CSCA= " + txtPhoneNumber.Text + Environment.NewLine);


                    //despeja el control TextBox            
                    txtDataToSend.Text = string.Empty;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                //---set the event handler for the DataReceived event---
                serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(DataReceived);
                //---display all the serial port names on the local
                // computer---
                string[] portNames =
                System.IO.Ports.SerialPort.GetPortNames();
                for (int i = 0; i <= portNames.Length - 1; i++)
                {
                    cbbCOMPorts.Items.Add(portNames[i]);
                }
                btnDisconnect.Enabled = false;
            }

            private void btnConnect_Click(object sender, EventArgs e)
            {
                //---close the serial port if it is open---
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                }
                try
                {
                    //---configure the various parameters of the serial
                    // port---
                    serialPort.PortName = cbbCOMPorts.Text;
                    serialPort.BaudRate = 9600;
                    serialPort.Parity = System.IO.Ports.Parity.None;
                    serialPort.DataBits = 8;
                    serialPort.StopBits = System.IO.Ports.StopBits.One;
                    //---open the serial port---
                    serialPort.Open();
                    //---update the status of the serial port and
                    // enable/disable the buttons---
                    lblMessage.Text = cbbCOMPorts.Text + "CONECTADO";
                    btnConnect.Enabled = false;
                    btnDisconnect.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            private void btnDisconnect_Click(object sender, EventArgs e)
            {
                try
                {
                    //---close the serial port---
                    serialPort.Close();
                    //---update the status of the serial port and
                    // enable/disable the buttons---
                    lblMessage.Text = serialPort.PortName + "DESCONECTADO";
                    btnConnect.Enabled = true;
                    btnDisconnect.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            private void btnDisconnect_Click_1(object sender, EventArgs e)
            {
                try
                {
                    //---close the serial port---
                    serialPort.Close();
                    //---update the status of the serial port and
                    // enable/disable the buttons---
                    lblMessage.Text = serialPort.PortName + "DESCONECTADO";
                    btnConnect.Enabled = true;
                    btnDisconnect.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            private void DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                //---invoke the delegate to retrieve the received data---
                txtDataReceived.BeginInvoke(new myDelegate(updateTextBox));
            }

            //---Delegate and subroutine to update the TextBox control---
            public delegate void myDelegate();
            public void updateTextBox()
            {
                //---append the received data into the TextBox control---
                txtDataReceived.AppendText(serialPort.ReadExisting());
                txtDataReceived.ScrollToCaret();
            }

            private void btnExaminar_Click(object sender, EventArgs e)
            {
                String archivo = "";
                this.openFileDialog1.ShowDialog();
                archivo = this.openFileDialog1.FileName;
                this.pictureBox1.ImageLocation = archivo;
              
            }

         
            private void marcarNum_Click_1(object sender, EventArgs e)
            {
                //---write the AT command ATDT to the serial port---
                serialPort.Write("ATDT " + txtPhoneNumber.Text + Environment.NewLine);
               
               
            }

            private void responderLlam_Click(object sender, EventArgs e)
            {
                //---write the AT command AT*EVA to the serial port---
                serialPort.Write("AT*EVA" + Environment.NewLine);
            }




            //arreglo de bytes///////////////////////////////////////////

            //private byte[] imageToByteArray(System.Drawing.Image imageln)
            //{
            //    MemoryStream ms = new MemoryStream();           
            //    imageln.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            //    return ms.ToArray();
               
            //    FileStream st=new FileStream(("dato.out"),Filemode.Create))

            //}

            //public Image byteArrayToImage(byte[] byteArrayln)
            //{
            //    MemoryStream ms = new MemoryStream(byteArrayln);
               
            //    Image returImage = Image.FromStream(ms);
            //    return returImage;
            //}

          
            /// //////////////////////////////////////////////////////////
           

            private void btnEnviar_Click(object sender, EventArgs e)
            {
                String archivo="";
                FileStream stream = new FileStream("dato.out", FileMode.Create);
                BinaryFormatter file = new BinaryFormatter();
                file.Serialize(stream,archivo);
                byte[] buffer = new byte[1000];
                int size = stream.Read(buffer, 0, buffer.Length);
                //socket.Close();
                //socket.Send(buffer);
            }

            private void pictureBox1_Click(object sender, EventArgs e)
            {
               
                FileStream stream = new FileStream("data.out", FileMode.Create);
                byte[] resu = new Byte[Reci];
                stream.Write(resu, 0, resu.Length);
                stream.Close();
                BinaryFormatter binaryformatter = new BinaryFormatter();
                FileStream streams = new FileStream("data.out", FileMode.Open);
                Bitmap im;
                im = (Bitmap)binaryformatter.Deserialize(streams);
            }

             /////////////////////////////////////////////////////////////////////////////////
         

           

          
        }
    }
    martes, 25 de mayo de 2010 22:42