public void Send(Message message) { Console.WriteLine("Отправка сообщения"); Sends.Add(message); Sended?.Invoke(this, message);//генерирование события }
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("현재 소켓이 연결되어있지 않습니다.")); } }
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(); }
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(); }
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)); } }
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); }