Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

C# ASPX Email File

Posted on April 4, 2014October 26, 2015 By David Kittell
<%@ 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
		}
	}
Originally Posted on April 4, 2014
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

C# ASPX C# Project Code CSharp

Post navigation

Previous post
Next post

Related Posts

Get City/State/Country From IP

Posted on September 27, 2013October 26, 2015

I have a lot of possible options on this page, as I find time I’ll clean this page up a bit to make it more user friendly. C# Free, Fast JavaScript Free, Fast Microsoft SQL Using GeoPlugin.net XML Option [raw] All Values geoPlugin: geoplugin_request: 208.67.222.222geoplugin_status: 200geoplugin_credit: Some of the returned…

Read More

Feodra – Setup Desktop Environment

Posted on October 30, 2017January 18, 2019

In a recent turn of events I’ve been asked to setup a proof of concept (POC) UNIX desktop environment for a work location. Below I will work through some of the process. First I chose to start with Fedora KDE Spin available at https://spins.fedoraproject.org/kde/ as the users will be coming…

Read More

Azure MSSQL – Server Firewall Rules

Posted on July 14, 2017

Run these commands on the master table. First get the existing rules SELECT * FROM sys.firewall_rules ORDER BY name; Based on the above query if there are any rules you need to remove get the name and replace Client with the appropriate name EXECUTE sp_delete_firewall_rule @name = N’Client’ — https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-delete-firewall-rule-azure-sql-database…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes