<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <asp:label id="lblUploadStatus" runat="server"></asp:label><br /> <asp:label id="lblEmailStatus" runat="server"></asp:label><br /> <form id="form1" runat="server"> <div> From: <asp:textbox id="txtFromEmail" runat="server" /><br /> <asp:fileupload id="FileUpload1" runat="server" /><br /> <asp:button id="btnUpload" runat="server" text="Submit" onclick="btnUpload_Click" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net.Mail; public partial class _Default : System.Web.UI.Page { // Specify the path on the server to save the uploaded file to. public static string sSavePath = @"c:temp"; public static string sEmailFromAddress = "Website_Online_Form@domain.com"; public static string sEmailToAddress = "email@email.com"; public static string sEmailToName = "John Doe"; public static string sEmailSubject = "Test Email"; public static string sEmailServer = "mail.domain.com"; protected void Page_Load (object sender, EventArgs e) { } protected void btnUpload_Click (object sender, EventArgs e) { #region File Upload // Before attempting to perform operations on the file, verify that the FileUpload control contains a file. if (FileUpload1.HasFile) { // Get the name of the file to upload. String fileName = DateTime.Now.ToString("yyyy-mm-dd_HHmmss") + "_" + FileUpload1.FileName; // Append the name of the file to upload to the path. sSavePath += fileName; // Call the SaveAs method to save the uploaded file to the specified path. // This example does not perform all the necessary error checking. // If a file with the same name already exists in the specified path, the uploaded file overwrites it. FileUpload1.SaveAs(sSavePath); // Notify the user of the name of the file // was saved under. lblUploadStatus.Text = "Your file was saved as " + fileName; } else { // Notify the user that a file was not uploaded. lblUploadStatus.Text = "You did not specify a file to upload."; } #endregion File Upload #region Email File MailMessage mailObj = new MailMessage(); // Set the message sender mailObj.From = new MailAddress(sEmailFromAddress, "Web Form Mailer"); // Set the message recipient(s) mailObj.To.Add(new MailAddress(sEmailToAddress, sEmailToName)); //mailObj.CC.Add(new MailAddress(userName, "Person Name")); //mailObj.Bcc.Add(new MailAddress(userName, "Person Name")); // Set the message subject mailObj.Subject = sEmailSubject; // Set the message body type mailObj.IsBodyHtml = true; // true or false if (txtFromEmail.Text != "") { mailObj.ReplyToList.Add(txtFromEmail.Text); } SmtpClient SMTPServer = new SmtpClient(sEmailServer); SMTPServer.EnableSsl = true; //SMTPServer.Credentials = new System.Net.NetworkCredential(sEmailUsername, sEmailPassword); try { SMTPServer.Send(mailObj); lblEmailStatus.Text = "Sent!"; System.IO.File.Delete(sSavePath); } catch (Exception ex) { lblEmailStatus.Text = ex.ToString(); //System.IO.File.Delete(sSavePath); } #endregion Email File } }
Last Updated on October 26, 2015