vs2003 和vs2005下的发送SMTP邮件 (downmoon原创)

vs2003 和vs2005下的发送SMTP邮件 (downmoon原创)
一、vs2003
引用 System.Web.Mail命名空间

 
private   void  SenMail2003()
    
{
        MailMessage mailObj 
=   new  MailMessage();
        mailObj.To 
=   this .txtTo.Text;
        mailObj.From 
=   this .txtFrom.Text;
       
        mailObj.Subject 
=   " 精采笑话 " ;
        mailObj.Body 
=   " 猪!你已中毒! 哈哈  " ;
       
        mailObj.BodyFormat 
=  MailFormat.Html;
        mailObj.BodyEncoding 
=  MailFormat.Base64;
        mailObj.Priority 
=  MailPriority.High;
        mailObj.Attachments.Add(
new  MailAttachment( " c:/swf/000.bmp " ));
        SmtpMail.Send(mailObj);
        Response.Write(
" 发送邮件成功! " );
    }



二、vs2005
引用 System.Net.Mail命名空间,安全性得到了增强

 

public   static   void  SendWebMailAndAttach( string  server)
     {
          string  file  =   " e:/inetpub/wwwroot/Test2005All/TestXML/testXML.xml " ;
         System.Net.Mail.MailMessage message  =   new  System.Net.Mail.MailMessage( " Test@126.com " ,  " Test@126.com " ,  " text message for you. " ,  " Test Title " );
         System.Net.Mail.Attachment data  =   new  System.Net.Mail.Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);
         System.Net.Mime.ContentDisposition disposition  =  data.ContentDisposition;
         disposition.CreationDate  =  System.IO.File.GetCreationTime(file);
         disposition.ModificationDate  =  System.IO.File.GetLastWriteTime(file);
         disposition.ReadDate  =  System.IO.File.GetLastAccessTime(file);
         message.Attachments.Add(data);
        System.Net.Mail.SmtpClient client  =   new  System.Net.Mail.SmtpClient(server);
        client.Credentials  =   new  NetworkCredential( " 用户名 " ,  " 密码 " );
        client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;//这句是关键
        client.Send(message);
        data.Dispose();
    }


引用示例

SendWebMailAndAttach("smtp.126.com");

没有更多推荐了,返回首页