Monday, November 9, 2009

SMTP Email Application c# ASP.NET





using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;


public partial class _Default : System.Web.UI.Page
{

#region "Private Variables And Constants"
string savePath;
private string[] file;
private string fileName = string.Empty;
private string _fileTypes = string.Empty;
private string _serverpath = string.Empty;
string filePath = string.Empty;
private int _fileSize = 5;
int fileTypeCounter = 0;
SmtpClient smtpClient = new SmtpClient();
MailMessage mMessage = new MailMessage();
ContentType attachType = new ContentType();
#endregion


protected void Page_Load(object sender, EventArgs e)
{
lblErrorMsg.Text = "";

}


protected void btnSend_Click(object sender, EventArgs e)
{

string str;
MailAddress fromAddress = new MailAddress(this.txtFrom.Text);
mMessage.From = fromAddress;
mMessage.To.Add(this.txtTo.Text);
mMessage.Subject = this.txtSubject.Text;
mMessage.Body = this.txtBody.Text;
//Server IP Address
smtpClient.Host = "10.229.86.65";
attachType.Name = UploadFile.PostedFile.FileName;
mMessage.Attachments.Add(new Attachment(savePath + UploadFile.PostedFile.FileName));
smtpClient.Send(mMessage);

}

protected void btnReset_Click(object sender, EventArgs e)
{
this.txtTo.Text = "";
this.txtFrom.Text = "";
this.txtSubject.Text = "";
this.txtBody.Text = "";
}

///


/// Upload files upload to the server
///

///

#region "Upload Files"

public void UploadFiles()
{
try
{
file = _fileTypes.Split(',');
if ((UploadFile.PostedFile != null) && (UploadFile.PostedFile.ContentLength > 0))
{
//Checking for File Size in MegaBytes. 1048576 bytes represents 1 MB.
if (((UploadFile.PostedFile.ContentLength / 1048576) <= this._fileSize)) { for (fileTypeCounter = 0; fileTypeCounter <= file.Length - 1; fileTypeCounter++) { file[fileTypeCounter] = file[fileTypeCounter].ToString(); if (!(UploadFile.PostedFile.ContentType == "application/octet-stream")) { if (!(UploadFile.PostedFile.ContentType == "application/zip")) { if (UploadFile.PostedFile.ContentType == "text/plain" || UploadFile.PostedFile.ContentType == "application/pdf" || UploadFile.PostedFile.ContentType == "application/msword" || UploadFile.PostedFile.ContentType == "application/vnd.ms-excel" || UploadFile.PostedFile.ContentType == "application/vnd.ms-powerpoint" || UploadFile.PostedFile.ContentType == "image/jpg" || UploadFile.PostedFile.ContentType == "image/jpeg" || UploadFile.PostedFile.ContentType == "image/pjpeg") { filePath = Path.GetFullPath(UploadFile.PostedFile.FileName); fileName = Path.GetFileName(UploadFile.PostedFile.FileName); SaveFile(UploadFile.PostedFile); } else { lblErrorMsg.Text = ""; lblErrorMsg.Text = "Only File type text,PDF,JPG,MSWord,Excel,PowerPoint are allowed."; } } else { lblErrorMsg.Text = ""; lblErrorMsg.Text = "You cannot upload file type Zip"; } } else { lblErrorMsg.Text = ""; lblErrorMsg.Text = "You cannot upload file type Zip"; } break; // TODO: might not be correct. Was : Exit For } } else { lblErrorMsg.Text = ""; lblErrorMsg.Text = "The File size cannot be greater than 5MB."; } } else { lblErrorMsg.Text = ""; lblErrorMsg.Text = "There is no file to Upload"; } } catch (Exception ex) { lblErrorMsg.Text = ""; lblErrorMsg.Text = ex.Message; } } #endregion #region "Save File to Server" ///
/// Save File to the Server (upload)
///

///
///

private void SaveFile(HttpPostedFile file1)
{

try
{
// Specify the path to save the uploaded file to.
//Server path
savePath = "\\\\Gvwrsd01\\e$\\UploadFiles\\";
fileName = UploadFile.FileName.ToString();
string pathToCheck = savePath.ToString() + fileName.ToString();
string temperoryfileName = string.Empty;
if ((UploadFile.HasFile))
{
savePath += fileName;
file1.SaveAs(savePath);
lblErrorMsg.Text = "";
lblErrorMsg.Text = "The File has been uploaded successfully.";
}
else
{

}
}
catch (Exception ex)
{
lblErrorMsg.Text = "";
lblErrorMsg.Text = ex.Message;
}
}
#endregion


protected void Button1_Click(object sender, EventArgs e)
{
UploadFiles();
}


}



No comments:

Post a Comment