CT.Space
Personal Internet Space
28.261
首页(Home)日志(Blog)知识库(KB)关于(About)
Microsoft.Net
ASP.NET
.Net Framework
LINQ
MS.Net Other
知识标签
老子(4) 养生(3) 夏季(3) 股市(3) 血气(2) mail(2) 婚姻(2) SmtpClient(2) 地震(1) 情绪(1) 爱情(1) 股票(1) 集合初始化器(1) 房地产(1) 日本(1) 理财(1) 饮料(1) 经络(1) 管理(1) 对象初始化器(1) 任正非(1) rss(1) 老板(1) 人力资源(1) 五脏六腑(1) 自动属性(1) 台湾(1) 扩展方法(1) 饮食(1) 人体系统(1) 中国(1) C#(1) 投资(1) iframe(1) 智力(1) 女性(1) 福州(1) 职场(1) 进化(1) 离婚(1) MVC(1) 经济增速(1) 家庭(1) 运动(1) 哭泣(1) 人性(1)
最新知识
  • TFS 中删除 workitem
  • ASP.NET 4.0 无法加载 System.ServiceModel.Activation.HttpModule
  • Asp.net关于Header/title/Meta tages/Style操作的小技巧
  • ASP.NET的错误处理机制
  • 配置 URLScan 工具
  • ASP.NET MVC 1.0 - 17. Anti Attack
  • 常用WebServices返回数据的4种方法比较
  • 从Oxite看使用了ADO.NET Entity Framework的应用程序的多层架构
  • asp.net防止页面重复提交
  • 在LINQ中使用NewID()随机排序
Microsoft.Net/MS.Net Other SmtpClient 异步发送例子
Tag SmtpClientmail
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Examples.SmptExamples.Async
{
    public class SimpleAsynchronousExample
    {
        static bool mailSent = false;
        public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
             String token = (string) e.UserState;
           
            if (e.Cancelled)
            {
                 Console.WriteLine("[{0}] Send canceled.", token);
            }
            if (e.Error != null)
            {
                 Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
            } else
            {
                Console.WriteLine("Message sent.");
            }
            mailSent = true;
        }
        public static void Main(string[] args)
        {
            // Command line argument must the the SMTP host.
            SmtpClient client = new SmtpClient(args[0]);
            // Specify the e-mail sender.
            // Create a mailing address that includes a UTF8 character
            // in the display name.
            MailAddress from = new MailAddress("jane@contoso.com", 
               "Jane " + (char)0xD8+ " Clayton", 
            System.Text.Encoding.UTF8);
            // Set destinations for the e-mail message.
            MailAddress to = new MailAddress("ben@contoso.com");
            // Specify the message content.
            MailMessage message = new MailMessage(from, to);
            message.Body = "This is a test e-mail message sent by an application. ";
            // Include some non-ASCII characters in body and subject.
            string someArrows = new string(new char[] {'\u2190', '\u2191', '\u2192', '\u2193'});
            message.Body += Environment.NewLine + someArrows;
            message.BodyEncoding =  System.Text.Encoding.UTF8;
            message.Subject = "test message 1" + someArrows;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            // Set the method that is called back when the send operation ends.
            client.SendCompleted += new 
            SendCompletedEventHandler(SendCompletedCallback);
            // The userState can be any object that allows your callback 
            // method to identify this send operation.
            // For this example, the userToken is a string constant.
            string userState = "test message1";
            client.SendAsync(message, userState);
            Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
            string answer = Console.ReadLine();
            // If the user canceled the send, and mail hasn't been sent yet,
            // then cancel the pending operation.
            if (answer.StartsWith("c") && mailSent == false)
            {
                client.SendAsyncCancel();
            }
            // Clean up.
            message.Dispose();
            Console.WriteLine("Goodbye.");
        }
    }
}
2007-03-14 15:08 | View(585) | Comment(0) | Send a mail
添加评论
 主题   *  
 姓名   *  
 Email   
 主页  
 
Copyright © 2009. All Rights Reserved. 闽ICP备08006344号 master@crazythief.com      [ Base On L.Smart ]