public virtual Result SendEmail(SmtpClient cl, MailMessage mail) { try { if (Config.SendMails) { if (mail.IsBodyHtml && mail.Attachments.Any()) { for (var i = 0; i < mail.Attachments.Count; i++) { mail.Body = mail.Body.Replace($"%A{i}%", $"cid:{mail.Attachments[i].ContentId}"); } } BeforeSend?.Invoke(this, mail); cl.Send(mail); } return(new Result(0)); } catch (Exception ex) { var res = new Result(1); res.SetException(ex); return(res); } }
/// <summary> /// 将Http请求作为异步请求发送 /// </summary> /// <param name="request">要发送的HTTP请求消息。</param> /// <param name="cancellationToken">取消操作的取消令牌。</param> /// <returns>表示异步操作的任务对象。</returns> protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { try { BeforeSend?.Invoke(this, EventArgs.Empty); return(await(SendAsyncMethod.Invoke(Handler, new object[] { request, cancellationToken }) as Task <HttpResponseMessage>)); // 调用父类的SendAsync方法 } finally { AfterSend?.Invoke(this, EventArgs.Empty); } }
public void Build() { BeforeSend?.Invoke(); ResponserIniter.Build(); BuildCallbacks(); if (!ResponserIniter.IgnoreParamCreate) { BuildParam(); } else { } }
public void Send(EmailMessage email) { try { using (var client = new SmtpClient(Host, Port)) { client.Credentials = new System.Net.NetworkCredential(UserName, Password); client.EnableSsl = EnableSsl; var emailTo = email.To.ToMailAddress(); var emailFrom = email.From.ToMailAddress(); var msg = new MailMessage(emailFrom, emailTo) { Subject = email.Subject, Body = email.Body ?? email.BodyHtml, IsBodyHtml = email.Body == null, }; if (!msg.IsBodyHtml && email.BodyHtml != null) { var mimeType = new ContentType(MimeTypes.Html); var alternate = AlternateView.CreateAlternateViewFromString(email.BodyHtml, mimeType); msg.AlternateViews.Add(alternate); } if (email.Cc != null) { msg.CC.Add(email.Cc.ToMailAddress()); } if (!string.IsNullOrEmpty(Bcc)) { msg.Bcc.Add(new MailAddress(Bcc)); } BeforeSend?.Invoke(email, msg); client.Send(msg); } } catch (Exception e) { Console.WriteLine(e); throw; } }
public void Send(MessageType type, string content = null) { BeforeSend?.Invoke(this, new LinkEventArgs(type, content)); var bytes = GetBytes(type, content); int count = bytes.Length; if (count > _maxlengthofdata) { int rest = 0; int n = Math.DivRem(count, _maxlengthofdata, out rest); int num = rest > 0 ? n + 1 : n; var nbytes = GetBytes(MessageType.BigData, $"{num};{count}"); _buffer = new byte[_maxlengthofdata]; nbytes.CopyTo(_buffer, 0); _socket.Send(_buffer); int i = 0; for (; i < n; i++) { Array.Copy(bytes, i * _maxlengthofdata, _buffer, 0, _maxlengthofdata); _socket.Send(_buffer); } if (rest > 0) { _buffer = new byte[_maxlengthofdata]; Array.Copy(bytes, i * _maxlengthofdata, _buffer, 0, rest); _socket.Send(_buffer); } } else { _buffer = new byte[_maxlengthofdata]; bytes.CopyTo(_buffer, 0); _socket.Send(_buffer); } }
internal void OnBeforeSend(object sender, IRequestBuilder builder) => BeforeSend?.Invoke(sender, builder);
protected virtual void OnBeforeSend(HttpRequest httpRequest) { BeforeSend?.Invoke(httpRequest); }