C#发送电子邮件 (异步)

[csharp]
///验证电子邮件的正则表达式 
[csharp] view plaincopyprint?
string emailStr = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; 
[csharp] 
/// <summary> 
        /// 异步发送电子邮件 
        /// </summary> 
        /// <param name="fromEmail">发送方电子邮件</param> 
        /// <param name="fromPwd">发送方电子邮件密码</param> 
        /// <param name="toEmail">接收方电子邮件</param> 
        /// <param name="subject">邮件标题</param> 
        /// <param name="body">邮件内空</param> 
        /// <param name="emailType">邮件类型</param> smtp.163.com.cn; smtp.qq.com.cn; smtp.126.com.cn;  smtp.sina.com.cn 
        private void SendEmailAsync(string fromEmail, string fromPwd, string toEmail, string subject, string body, string emailType) 
        { 
            MailAddress addrFrom = new MailAddress(fromEmail, fromEmail); 
            MailAddress addrTo = new MailAddress(toEmail, toEmail); 
            MailMessage mm = new MailMessage(addrFrom, addrTo); 
            mm.BodyEncoding = Encoding.UTF8; 
            mm.IsBodyHtml = true; 
            mm.Subject = subject; 
            mm.Body = body; 
 
            if (!string.IsNullOrEmpty(attFile)) 
            { 
                Attachment att = new Attachment(attFile, MediaTypeNames.Application.Octet); 
                ContentDisposition cd = att.ContentDisposition; 
                cd.CreationDate = File.GetCreationTime(attFile); 
                cd.ModificationDate = File.GetLastWriteTime(attFile); 
                cd.ReadDate = File.GetLastAccessTime(attFile); 
                mm.Attachments.Add(att);//添加附件 
            } 
 
            NetworkCredential nc = new NetworkCredential(fromEmail, fromPwd); 
            SmtpClient smtp = new SmtpClient(emailType); 
            smtp.UseDefaultCredentials = false; 
            smtp.Credentials = nc; 
            smtp.EnableSsl = false; 
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
            smtp.SendCompleted+=new SendCompletedEventHandler(smtp_SendCompleted); 
            smtp.SendAsync(mm, "OK"); 
        } 
 
        void smtp_SendCompleted(object sender, AsyncCompletedEventArgs e) 
        { 
            if (e.UserState.ToString() == "OK") 
                MessageBox.Show("发送成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用异步方法来处理键值对。具体实现可以使用异步的Dictionary类,它提供了一些异步的方法来访问和操作键值对。例如,可以使用异步的AddAsync方法来向字典中添加键值对,使用异步的ContainsKeyAsync方法来检查是否存在某个键等等。以下是一个使用异步方法处理键值对的示例代码: ```csharp using System.Collections.Generic; using System.Threading.Tasks; class Program { static Dictionary<string, string> dictionary = new Dictionary<string, string>(); static async Task Main(string[] args) { await dictionary.AddAsync("key1", "value1"); bool containsKey = await dictionary.ContainsKeyAsync("key1"); string value = await dictionary.GetValueAsync("key1"); await dictionary.RemoveAsync("key1"); } } public static class DictionaryExtensions { public static async Task AddAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) { await Task.Run(() => dictionary.Add(key, value)); } public static async Task<bool> ContainsKeyAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) { return await Task.Run(() => dictionary.ContainsKey(key)); } public static async Task<TValue> GetValueAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) { return await Task.Run(() => dictionary[key]); } public static async Task RemoveAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) { await Task.Run(() => dictionary.Remove(key)); } } ``` 在这个示例中,我们使用了异步方法AddAsync、ContainsKeyAsync、GetValueAsync和RemoveAsync来向字典中添加、检查、获取和删除键值对。这些方法都是异步的,可以在异步环境下使用,比如在异步方法或线程池任务中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值