Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

In a background worker that will be running on our Server, I am listening for data in the form of a System.Net.Mail.MailMessage object (including attachments) to come from a client on our LAN. (The client PCs and operators on our manufacturing floor do not have an email program).

 

This is a work in progress, so the Client Side hasn't been written yet. The plan is to save the MailMessage object to a MemoryStream, and send the MemoryStream over the network using TCP.

 

My current obstical comes from finding a way to convert the received MemoryStream back into a MailMessage on the Server (the receiving end) so that the Server can send the message.

 

How do I convert a MemoryStream of data into a MailMessage object? I tried the code below, but the compile time error is "Cannot convert type 'byte[]' to 'System.Net.Mail.MailMessage'".

 

Server Side:

try {
 SvrForm.Server.Start();
 TcpClient client = SvrForm.Server.AcceptTcpClient(); // waits for data
 if (worker.CancellationPending == true) return;
 NetworkStream stream = client.GetStream();
 try {
   byte[] buf = new byte[client.ReceiveBufferSize];
   using (MemoryStream ms = new MemoryStream(buf)) {
     int len;
     do {
       len = stream.Read(buf, 0, client.ReceiveBufferSize);
       ms.Write(buf, 0, len);
     } while (len == client.ReceiveBufferSize);
     byte[] byteFile = ms.GetBuffer();
     try {
       MailMessage email = (MailMessage)byteFile; <= ERROR HERE!!!
       if (email.From.Address != string.Empty) {
         SmtpClient client = new SmtpClient("172.16.8.200");
         client.Send(email);
       }
     } catch (Exception er1) {
       Console.WriteLine(er1.Message);
     }
     ms.Close();
   }
 } finally {
   stream.Close();
   client.Close();
   SvrForm.Server.Stop();
 }
} catch (SocketException) { // See MSDN:
 // Windows Sockets V2 API Error Code Documentation
 // for detailed description of error code
 e.Cancel = true;
} catch (ThreadAbortException err) { // If I have to call Abort on this thread
 eMsg = "Worker Thread Abort Exception:\r\n" + err.Message;
 e.Cancel = true;
} catch (Exception err) {
 eMsg = "General Error:\r\n" + err.Message;
 e.Cancel = true;
} finally {
 SvrForm.Server.Stop();
}

Any helpful tips on writing the Client Side portion are appreciated as well.

 

Regards,

~Joe

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...