2011-05-25 79 views
0

我有以下代碼,但它不起作用 - 當我使用message.To時遇到錯誤,然後我將其更改爲message.To.Add,但沒有任何成功。帶有文件附件的Asp電子郵件提交

我不知道ASP.net,我只是想讓這件事情起作用。任何幫助表示讚賞。

using System.Net.Mail; 


protected void btnsubmit_Click(object sender, EventArgs e) 
{ 
    string body = ""; 
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); 
    body = "<table border='0' align='center' cellpadding='2' style='border-collapse: collapse' bordercolor=''#111111' width='100%' id='AutoNumber1'>"; 
    body = body + "<tr><td width='100%' align='center' colspan='6'><b>Photo Submission Form</b></td></tr>"; 
    body = body + "<tr><td width='100%' colspan='6'>&nbsp;</td></tr>"; 
    body = body + "<tr><td width='50%' colspan='2'>Name</td><td width='50%' colspan='4'><b>" + name.Text + "</b></td></tr>"; 
    body = body + "<tr><td width='50%' colspan='2'>E-Mail</td><td width='50%' colspan='4'><b>" + email.Text + "</b></td></tr>"; 
    body = body + "<tr><td width='50%' colspan='2'>Caption</td><td width='50%' colspan='4'><b>" + caption.Text + "</b></td></tr>"; 
    body = body + "<tr><td width='50%' colspan='2'>Phone</td><td width='50%' colspan='4'><b>" + phone.Text + "</b></td></tr>"; 
    MailMessage message = new MailMessage(); 
    Attachment myAttachment = new Attachment(FileUpload1.FileContent, fileName); 
    message.To.Add(new MailAddress("[email protected]")); 
    message.From = New MailAddress(email.Text); 
    message.Subject = "Photo Submission Form"; 
    message.BodyFormat = MailFormat.Html; 
    message.Body = body; 
    message.Attachments.Add(myAttachment); 
    SmtpMail.SmtpServer.Insert(0, ""); 
    SmtpMail.Send(message); 
    RegisterStartupScript("startupScript", "<script language=JavaScript>alert('Message sent successfully.');</script>"); 
+2

您需要學習C#和ASP ** .Net **。 – SLaks 2011-05-25 19:42:32

回答

0
The Attachment class is used with the MailMessage class. All messages 

包括主體,其包含 內容的消息的。除了 正文之外,您可能還想發送 附加文件。這些發送爲 附件,並表示爲 附件實例。要將 附件添加到郵件中,請將其添加到MailMessage.Attachments 集合中。

Attachment content can be a String, Stream, or file name. You can 

通過使用任何附件 構造的指定的附件 的內容。

The MIME Content-Type header information for the attachment is 

由ContentType 屬性表示。內容類型報頭 指定媒體類型和子類型 和任何相關參數。使用 ContentType獲取與附件關聯的實例 。

The MIME Content-Disposition header is represented by the 

ContentDisposition屬性。 Content-Disposition標題指定 附件的演示文稿和文件時間戳 。 A 僅當附件是文件時,纔會發送Content-Disposition標頭 。使用 ContentDisposition屬性獲取 與 附件關聯的實例。

The MIME Content-Transfer-Encoding header is represented by the 

TransferEncoding屬性。

 public static void CreateMessageWithAttachment(string server) 
     { 
      // Specify the file to be attached and sent. 
      // This example assumes that a file named Data.xls exists in the 
      // current working directory. 
      string file = "data.xls"; 
      // Create a message and set up the recipients. 
      MailMessage message = new MailMessage(
       "[email protected]", 
       "[email protected]", 
       "Quarterly data report.", 
       "See the attached spreadsheet."); 

      // Create the file attachment for this e-mail message. 
      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. 
      message.Attachments.Add(data); 

      //Send the message. 
      SmtpClient client = new SmtpClient(server); 
      // Add credentials if the SMTP server requires them. 
      client.Credentials = CredentialCache.DefaultNetworkCredentials; 

    try { 
       client.Send(message); 
      } 
      catch (Exception ex) { 
         //Handle... 

      } 

      data.Dispose(); 
     }