private void Smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { SendCompleted(e.Cancelled, e.Error); foreach (var item in MMsg.Attachments) { item.Dispose(); } Smtp.Dispose(); MMsg.Dispose(); }
/// <summary> /// 异步发送邮件 /// </summary> public void SendMailSync() { string result = string.Empty; try { Smtp.SendAsync(MMsg, null); }catch (Exception e) { foreach (var item in MMsg.Attachments) { item.Dispose(); } Smtp.Dispose(); MMsg.Dispose(); throw e; } }
/// <summary> /// 同步发送邮件,同时返回错误字符串;为空时则正常发送成功 /// </summary> /// <returns></returns> public string SendMail() { string result = string.Empty; try { Smtp.Send(MMsg); } catch (Exception e) { result = e.Message; } foreach (var item in MMsg.Attachments) { item.Dispose(); } Smtp.Dispose(); MMsg.Dispose(); return(result); }