Email in .net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.ComponentModel;
using System.Net.Mime;

namespace Email
{
    
class Program
    {
        
static void Main(string[] args)
        {
            MailMessage mm 
= new MailMessage();
            mm.From 
= new MailAddress("email@email.com""Joey");
            mm.To.Add(
new MailAddress("email@email.com""Joey"));
            mm.Subject 
= "Email sending test";
            
string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:Pic\"></body></html>";
            AlternateView avHtml 
= AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
            LinkedResource pic 
= new LinkedResource("pic.jpg", MediaTypeNames.Image.Jpeg);
            pic.ContentId 
= "Pic";
            avHtml.LinkedResources.Add(pic);
            mm.AlternateViews.Add(avHtml);
            
string textBody = "You must use an e-mail client that supports HTML messages";
            AlternateView avText 
= AlternateView.CreateAlternateViewFromString(textBody, null, MediaTypeNames.Text.Plain);
            mm.AlternateViews.Add(avText);
            SmtpClient sc 
= new SmtpClient("host");
            sc.Credentials 
= new NetworkCredential("username""password");
            sc.SendCompleted 
+= new SendCompletedEventHandler(sc_SendCompleted);
            sc.SendAsync(mm, 
null);
            sc.SendAsyncCancel();
        }

        
static void sc_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            
if (e.Cancelled)
                Console.WriteLine(
"Message cancelled");
            
else if (e.Error != null)
                Console.WriteLine(
"Error: " + e.Error.ToString());
            
else
                Console.WriteLine(
"Message sent");
        }
    }
}

posted @ 2009-05-04 06:37  N/A2011  阅读(305)  评论(0编辑  收藏  举报