第13章 FTP檔案上傳、Email自動傳送 報告人 M9153314 李育旻
FTP檔案上傳 檔案上傳的發動在瀏覽器這一端,在設計程式時,要注意 表單Enctype(編碼型態)屬性須設定成”multipart/form-data” 佈置File型態的HTML輸入欄位,供使用者選取所要上傳的檔案
Msg <Html> <Body BgColor=White> <H3>檔案上傳 -- 上傳一個檔案<Hr></H3> <Form Enctype="multipart/form-data" runat="server"> 請輸入完整檔案路徑: <Input Type="File" id="FileUp" runat="server"><P> <asp:Button runat="server" Text="Upload" OnClick="UploadFile" /> <Hr> <asp:Label runat="server" id="Msg" /> </Form> </Body> </Html> FileUp Msg
<script language="VB" runat="server"> Sub UploadFile(sender As Object, e As EventArgs) Dim file As HttpPostedFile = FileUp.PostedFile If file.ContentLength <> 0 Then Msg.Text = "大小: " & file.ContentLength Msg.Text &= "<br>類型: " & file.ContentType Msg.Text &= "<br>名稱: " & file.FileName Dim FileSplit() As String = Split( file.FileName, "\" ) Dim FileName As String = FileSplit(FileSplit.Length-1) file.SaveAs( Server.MapPath(FileName) ) Else Msg.Text = "" End If End Sub </script> FileUp Msg
FTP上傳多個檔案 若想要同時上傳多個檔案,要在表單中佈置多個<Input type=“FILE”>欄位 <Html> <Body BgColor=White> <H3>檔案上傳 -- 上傳多個檔案<Hr></H3> <Form Enctype="multipart/form-data" runat="server"> 請輸入完整路徑檔案名稱:<br> <Input Type="File" id="FileUp1" runat="server"><br> <Input Type="File" id="FileUp2" runat="server"><br> <Input Type="File" id="FileUp3" runat="server"><br> <asp:button runat="server" Text="Upload" OnClick="UploadFile" /> <Hr> <asp:Label runat="server" id="Msg" /> </Form></Body></Html>
<script language="VB" runat="server"> Sub UploadFile(sender As Object, e As EventArgs) Dim I As Integer Msg.Text = "" For I = 0 To Request.Files.Count - 1 Dim File As HttpPostedFile = Request.Files(I) If File.ContentLength > 0 Then ' 顯示上傳檔案的資訊 Msg.Text &= "檔案: " & File.FileName Msg.Text &= ", " & File.ContentLength Msg.Text &= ", " & File.ContentType & "<br>" ' 儲存檔案 Dim FileSplit() As String = Split( File.FileName, "\" ) Dim FileName As String = FileSplit(FileSplit.Length-1) File.SaveAs( Server.MapPath(FileName) ) End If Next End Sub </script>
Email自動傳送 網頁製做中,需要自動傳送Email給上網者的情況很多,例如: 電子購物網頁:上網者購買物品後,傳送物品清單供上網者備查 電子賀卡:上網者可選擇卡片寄給某人,我們要實做寄信給受卡者的動作
MailMessages及SmtpMail物件 MailMessages用來設定Email的內容 SmtpMail負責將Email傳送出去 <script Language="VB" runat="server"> Sub Button_Click(sender As Object, e As EventArgs) Dim mail As MailMessage = New MailMessage mail.To = 收件人e-mail mail.From = 送件人e-mail mail.Subject = 主旨 mail.BodyFormat = 郵件格式(純文字或HTML) mail.Body = 郵件內容 On Error Resume Next SmtpMail.SmtpServer = “smtp伺服器的位址" SmtpMail.Send(mail) If Err.Number <> 0 Then Msg.Text = Err.Description Else Msg.Text = "郵件已經送出!" End If End Sub </script>
mailTo mailFrom mailSubject mailBody
<%@ Import Namespace="System.Web.Mail" %> <Html> <Body BgColor="White"> <H2>ASP.NET Email 發送程式!<Hr></H2> <Form runat="server"> <Table Border=1> <Tr><Td>收件者:</Td> <Td><asp:TextBox id="mailTo" Size=40 runat="server"/></Td></Tr> <Tr><Td>寄件者:</Td> <Td><asp:TextBox id="mailFrom" Size=40 runat="server"/></Td></Tr> <Tr><Td>主旨:</Td> <Td><asp:TextBox id="mailSubject" Size=40 runat="server"/></Td></Tr> <Tr><Td>內文:</Td> <Td><asp:TextBox runat="server" id="mailBody" TextMode="MultiLine" Rows=8 Cols=60 /> </Td></Tr> </Table> <asp:Button runat="server" Text="送 出" OnClick="Button_Click" /> </Form> <Hr><asp:Label id="Msg" runat="server" ForeColor="Red" /><p> <Font Size=-1 Color=Blue>使用本範例之前,請先參閱書本「SMTP Server 與郵件的傳送」段落中的說明,設定好 SmtpMail.SmtpServer 屬性。</Font> </Body> </Html>
<script Language="VB" runat="server"> Sub Button_Click(sender As Object, e As EventArgs) Dim mail As MailMessage = New MailMessage mail.To = mailTo.Text mail.From = mailFrom.Text mail.Subject = mailSubject.Text mail.BodyFormat = MailFormat.Text mail.Body = mailBody.Text On Error Resume Next SmtpMail.SmtpServer = "msa.hinet.net" SmtpMail.Send(mail) If Err.Number <> 0 Then Msg.Text = Err.Description Else Msg.Text = "郵件已經送出!" End If End Sub </script>
<script Language="VB" runat="server"> Sub Button_Click(sender As Object, e As EventArgs) Dim mail As MailMessage = New MailMessage mail.To = mailTo.Text mail.From = mailFrom.Text mail.Subject = mailSubject.Text mail.BodyFormat = MailFormat.Html mail.Body = mailBody.Text On Error Resume Next SmtpMail.SmtpServer = "msa.hinet.net" SmtpMail.Send(mail) If Err.Number <> 0 Then Msg.Text = Err.Description Else Msg.Text = "郵件已經送出!" End If End Sub
MailMessage的細部設定 原始收件人格式mail.To=“kjwang@gcn.net.tw” 設定收件人姓名 多位收件人 mail.From = “阿毛<amou@kjwang.com>” 多位收件人 mail.To = “王國榮<kjwang@gcn.net.tw>, 阿毛<amou@kjwang.com>” 副本收件人 mail.Cc = “阿毛<amou@kjwang.com>” 密件副本收件人 mail.Bcc = “阿毛<amou@kjwang.com>”
Email增加附件 若要將檔案附加到Email中一起傳送,所需撰寫的程式如下: Dim attach as New MailAttachment(完整路徑檔案名稱) mail.Attachments.Add(attach) ……. SmtpMail.Send(mail) 程式碼解說 利用New MailAttachment()建立一個附件,建立時須傳入完整路徑檔案名稱。例如目前目錄有F8315.gif 檔案要設定成附件,則敘述如下 Dim attach As New MailAttachment(Server.MapPath(“F8315.gif”)) 2. 建立附件後,將其加入Email中須呼叫Attachments.Add方法
範例 新增<Input type=“File” , id=“FileUp” run=“server”> 表單屬性增加Enctype設定,如下 <Form enctype=“multipart/form-data” run=“server”>
程式碼解說 Sub Button_Click(sender As Object, e As EventArgs) Dim mail As MailMessage = New MailMessage mail.To = mailTo.Text mail.From = mailFrom.Text mail.Subject = mailSubject.Text mail.Body = mailBody.Text If Format.SelectedItem.Text = "純文字格式" Then mail.BodyFormat = MailFormat.Text Else mail.BodyFormat = MailFormat.Html End If Dim file As HttpPostedFile = FileUp.PostedFile If file.ContentLength <> 0 Then Dim FileSplit() As String = Split( File.FileName, "\" ) Dim FileName As String = FileSplit(FileSplit.Length-1) File.SaveAs( Server.MapPath(FileName) ) Dim attach As New MailAttachment(Server.MapPath(FileName)) mail.Attachments.Add( attach ) On Error Resume Next SmtpMail.SmtpServer = "msa.hinet.net" SmtpMail.Send(mail)
結束