跳到主要內容

Send Mail And Attach File

  using System.Net;
using System.Net.Mail;
using System.Net.Mime;


string file = "C:\\test\\newFile.txt";
        MailMessage mail = new MailMessage();
        //set the addresses
        mail.From = new MailAddress("1111");
        mail.To.Add("2222");
        mail.To.Add("333");
        //set the content
        mail.Subject = "This is mail by net2005";
        mail.Body = "ASP.NET MAIL 發信測試";
         

        Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
      
        // Add time stamp information for the file.
        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);
      
        // Add the file attachment to this e-mail message.
        mail.Attachments.Add(data);
      
        //Send the message.
        SmtpClient client = new SmtpClient("msa.hinet.net");
      
        // Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials;
        client.Send(mail);
      
        data.Dispose();