http://www.yesky.com/20011209/209018.shtml
分三页自己看!

解决方案 »

  1.   

    Sending Email From .NET Applications
    Introduction
    Sending emails is one of the most common requirements of any web application. In traditional VB and ASP applications CDONT serves this requirement. In .NET the System.Web.Mail namespaces provides classes that form a wrapper over CDONT and can be use to quickly send emails. Even though it is found in System.Web namespace it can be used in windows forms also. This article will tell you how to perform many common email tasks with this namespace. 
    Sending email - Quick and Easy
    This example shows the most basic way to send an email: 
    SmtpMail.SmtpServer="myserver.com"
    SmtpMail.Send("[email protected]", 
    "[email protected]", 
    "This is subject", 
    "This is message")As you can see the SmtpMail class has a static method Send that takes four arguments - sender's email address, receiver's email address, subject of the email and message body. The SmtpServer property specifies the SMTP server to be used for sending email messages. If you do not provide this value the default local server will be used. This is the most obvious usage for simple confirmation kind of messages. 
    Sending CC, BCC and setting priority
    Many times we also need to send CC and BCC to others. In such situation the MailMessage class can be effectively used as shown below: 
    Dim mm As New MailMessage()
    mm.From = "[email protected]"
    mm.To = "[email protected]"
    mm.Cc = "[email protected]"
    mm.Bcc = "[email protected]"
    mm.Priority = MailPriority.High
    mm.Subject = "This is subject"
    mm.Body = "This is message"
    SmtpMail.Send(mm)Here we have created an instance of MailMessage and set individual properties like From, To, CC and BCC. You can also set mail priority using Priority property and MailPriority enum. Then you can use another overloaded form of Send method that takes this instance of MailMessage. 
    Sending HTML messages with images and links
    Now a days HTML messages are also very common. By default the message format is plain text. Following example shows how to send HTML messages: 
    Dim mm As New MailMessage()
    mm.From = "[email protected]"
    mm.To = "[email protected]"
    mm.BodyFormat = MailFormat.Html
    mm.UrlContentBase = "http://www.yourdomain.com"
    mm.UrlContentLocation = "images/"
    mm.Subject = "This is subject"
    mm.Body = "This is message"
    SmtpMail.Send(mm)Here we have set the BodyFormat property to HTML. The UrlContentBase and UrlContentLocation properties are typically used together. The former tells the base address for the referenced images and links where as the later tells the relative location further to the base location. 
    Sending Attachments
    Our final example shows how to send attachments with the email. 
    Dim mm As New MailMessage()
    mm.From = "[email protected]"
    mm.To = "[email protected]"
    mm.Subject = "This is subject"
    mm.Body = "This is message"
    Dim ma as New MailAttachment("c:\attatchments\somefile.zip")
    mm.Attachments.Add(ma)
    SmtpMail.Send(mm)Here, we have created an instance of MailAttachment class and passed the path of some disk file to its constructor. We then add it to the Attachments collection of the MailMessage class. Finally the Send method is called as usual. I hope this covers almost all the commonly used email tasks! 
    About the author