Exemplo n.º 1
0
        public void Send(MailOptions options)
        {
            _mail.From = new MailAddress(options.From);

            if (options.To != null)
            {
                foreach (string to in options.To)
                {
                    _mail.To.Add(new MailAddress(to));
                }
            }

            if (options.Cc != null)
            {
                foreach (string cc in options.Cc)
                {
                    _mail.CC.Add(new MailAddress(cc));
                }
            }

            _mail.Subject    = options.Subject;
            _mail.IsBodyHtml = options.IsBodyHtml;
            _mail.Body       = options.Body;

            Thread th = new Thread(() =>
            {
                _smtp.Send(_mail);
                Sended?.Invoke();
            });

            th.Start();
        }
Exemplo n.º 2
0
        public void Send(Message message)
        {
            Console.WriteLine("Отправка сообщения");
            Sends.Add(message);

            Sended?.Invoke(this, message);//генерирование события
        }
Exemplo n.º 3
0
 public void Send(byte[] data)
 {
     if (socket.Connected)
     {
         byte[] packet;
         if (enableMultiBytes)
         {
             packet = new byte[4 + data.Length];
             byte[] length = BitConverter.GetBytes(data.Length);
             Buffer.BlockCopy(length, 0, packet, 0, 4);
             Buffer.BlockCopy(data, 0, packet, 4, data.Length);
         }
         else
         {
             packet = new byte[data.Length];
             Buffer.BlockCopy(data, 0, packet, 0, data.Length);
         }
         try
         {
             socket.BeginSend(packet, 0, packet.Length, 0, new AsyncCallback((ar) =>
             {
                 int bytesSent = socket.EndSend(ar);
                 Sended?.Invoke(this, enableMultiBytes ? bytesSent - 4 : bytesSent);
             }), null);
         }
         catch (Exception e) { ErrorOccurred?.Invoke(e); }
     }
     else
     {
         ErrorOccurred?.Invoke(new InvalidOperationException("현재 소켓이 연결되어있지 않습니다."));
     }
 }
Exemplo n.º 4
0
    public void SendMessage(string type, string content)
    {
        var newMessage = new Message(type, content);

        Sended.Add(newMessage);

        NetworkManager.AddNetworkOperation(CreateSendMessageOperation(newMessage));
    }
Exemplo n.º 5
0
Arquivo: Peer.cs Projeto: n-suudai/Lab
        private void SendMain()
        {
            try
            {
                Serializer serializer = new Serializer(client.GetStream());

                while (client.Connected)
                {
                    // キューに追加されるのを待つ
                    enqueuedWaitHandle.WaitOne();
                    enqueuedWaitHandle.Reset();

                    RemoteEntity remoteEntity = null;
                    if (sendQueue.TryDequeue(out remoteEntity))
                    {
                        if (remoteEntity == null)
                        {
                            break;
                        }                                    // null が来た時点で終了

                        serializer.WriteString(remoteEntity.Name);

                        remoteEntity.Serialize(serializer);

                        // post sended
                        if (synchronization != null)
                        {
                            synchronization.Post(_ => Sended?.Invoke(remoteEntity), null);
                        }
                        else
                        {
                            Sended?.Invoke(remoteEntity);
                        }
                    }
                }
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            catch (IOException e)
            {
                Console.WriteLine("IOException: {0}", e);
            }
            finally
            {
            }

            client.Close();
        }
Exemplo n.º 6
0
        public static async void Send(Sended item)
        {
            foreach (SendedReceiver itemSR in item.SendedReceivers)
            {
                await SendMailsAsync(item, itemSR);
            }

            AllSended?.Invoke(_errDic, item);

            ClearErrDic();
        }
Exemplo n.º 7
0
        public sealed override void Update()
        {
            UpdateLock.Reset(); // Signal that the UpdateThread is alive.
            try
            {
                while (!UpdateToken.IsCancellationRequested && Stream.IsConnected)
                {
                    ConnectionLock.Reset(); // Signal that we are handling pending client data.
                    try
                    {
                        while (Stream.TryReadPacket(out var packetToReceive))
                        {
                            HandlePacket(packetToReceive);

#if DEBUG
                            Received.Enqueue(packetToReceive);
                            if (Received.Count >= QueueSize)
                            {
                                Received.Dequeue();
                            }
#endif
                        }
                        while (PacketsToSend.TryDequeue(out var packetToSend))
                        {
                            Stream.SendPacket(packetToSend);

#if DEBUG
                            Sended.Enqueue(packetToSend);
                            if (Sended.Count >= QueueSize)
                            {
                                Sended.Dequeue();
                            }
#endif
                        }
                    }
                    finally
                    {
                        ConnectionLock.Set(); // Signal that we are not handling anymore pending client data.
                    }

                    Thread.Sleep(100); // 100 calls per second should not be too often?
                }
            }
            finally
            {
                UpdateLock.Set();                                                // Signal that the UpdateThread is finished

                if (!UpdateToken.IsCancellationRequested && !Stream.IsConnected) // Leave() if the update cycle stopped unexpectedly
                {
                    LeaveAsync();
                }
            }
        }
Exemplo n.º 8
0
        protected override void Dispose(bool disposing)
        {
            if (!IsDisposing)
            {
                if (disposing)
                {
                    Stream.Dispose();

#if DEBUG
                    Sended.Clear();
                    Received.Clear();
#endif
                }


                IsDisposing = true;
            }
            base.Dispose(disposing);
        }
Exemplo n.º 9
0
        private static async Task SendMailsAsync(Sended item, SendedReceiver itemSR)
        {
            MailMessage mm = new MailMessage(item.Sender.Key, itemSR.Receiver.Key);

            mm.Subject    = item.Mail.Topic;
            mm.Body       = item.Mail.Content;
            mm.IsBodyHtml = item.Mail.IsHTML;
            SmtpClient sc = new SmtpClient(item.SMTP.Key, int.Parse(item.SMTP.Value))
            {
                EnableSsl = true, Credentials = new NetworkCredential(item.Sender.Key, item.Sender.Value)
            };

            try
            {
                await Task.Run(() => sc.Send(mm));
            }
            catch (Exception e)
            {
                _errDic.Add(itemSR, e.Message);
            }
        }
Exemplo n.º 10
0
 private void OnSended(Session session, int bytesSent)
 {
     try { Sended?.Invoke(this, new SendEventArgs(session.IP, bytesSent)); }
     catch (Exception e) { ErrorOccurred?.Invoke(this, new ExceptionEventArgs(e)); }
 }
Exemplo n.º 11
0
        private bool mSend(string Title, string Message, string Recipient, Attachment[] Attachments, bool RaiseEvents, string DisplayName = null, string Replyer = null, Priority?Priority = null)
        {
            mSemaphore.Wait();

            var Success = true;
            var Streams = new List <Stream>();

            try
            {
                using (var Mail = new MailMessage(mLogin, Recipient))
                {
                    Mail.Subject    = Title;
                    Mail.Body       = Message;
                    Mail.IsBodyHtml = true;

                    if (!(Priority is null))
                    {
                        switch (Priority)
                        {
                        case MailSenders.Priority.Low: Mail.Priority = MailPriority.Low; break;

                        case MailSenders.Priority.Normal: Mail.Priority = MailPriority.Normal; break;

                        case MailSenders.Priority.High: Mail.Priority = MailPriority.High; break;
                        }
                    }
                    if (!(Replyer is null))
                    {
                        Mail.ReplyTo = new MailAddress(Replyer);
                    }
                    if (!(DisplayName is null))
                    {
                        Mail.From = new MailAddress(mLogin, DisplayName);
                    }

                    foreach (var Attachment in Attachments)
                    {
                        var Stream = new MemoryStream(Attachment.Data)
                        {
                            Position = 0
                        };

                        Streams.Add(Stream);

                        var NAttachment = new System.Net.Mail.Attachment(Stream, MimeService.IsKnown(Attachment.FileName) ? MimeService.From(Attachment.FileName) : MimeService.From(".bin"));
                        NAttachment.ContentDisposition.FileName = Attachment.FileName;

                        Mail.Attachments.Add(NAttachment);
                    }

                    mClient.Send(Mail);
                }
            }
            catch
            {
                Success = false; // in smtpclient exceptions are useless, "one or more error occurred"
            }
            finally
            {
                Streams.ForEach((Stream) => Stream.Close());

                if (RaiseEvents)
                {
                    if (Success)
                    {
                        Sended?.Invoke();
                    }
                    else
                    {
                        Failed?.Invoke();
                    }
                }

                mSemaphore.Release();
            }

            return(Success);
        }