How to Send Email With Attachment In Asp.Net

Sending email with attachment in ASP.Net with a simple coding. In this article I will show you, how you can do this.

Requirements

 

Index.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Index.aspx.cs” Inherits=”_Default” Debug=”true” %>

Place your Html tags here

<!– Form starts here –>
<label for=”exampleInputName”>Name</label>
<asp:TextBox ID=”txtName” runat=”server” class=”form-control” placeholder=”Name”></asp:TextBox>
<label for=”exampleInputEmail”>Email address</label>

<asp:TextBox ID=”txtEmail” runat=”server” class=”form-control” placeholder=”Enter Email”></asp:TextBox>
<label for=”txtcontact”>Contact no</label>
<asp:TextBox ID=”txtcontact” runat=”server” class=”form-control” placeholder=”Contact no”></asp:TextBox>
<label for=”txtjobTitle”>Job Title</label>
<asp:DropDownList ID=”txtjobTitle” runat=”server” class=”form-control”>
<asp:ListItem Text=”Select” Value=”0″></asp:ListItem>
<asp:ListItem Text=”Social Media Experts” Value=”1″></asp:ListItem>
<asp:ListItem Text=”Business Developement Executives” Value=”2″></asp:ListItem>
<asp:ListItem Text=”Copywriters” Value=”3″></asp:ListItem>
<asp:ListItem Text=”Graphic Designers” Value=”4″></asp:ListItem>
<asp:ListItem Text=”Web Designers” Value=”5″></asp:ListItem>
<asp:ListItem Text=”Animation Designers” Value=”6″></asp:ListItem>
</asp:DropDownList>
<label for=”txtjobExp”>Experience</label>
<asp:DropDownList ID=”txtjobExp” runat=”server” class=”form-control”>
<asp:ListItem Text=”Select” Value=”0″></asp:ListItem>
<asp:ListItem Text=”0-1″ Value=”1″></asp:ListItem>
<asp:ListItem Text=”1-3″ Value=”2″></asp:ListItem>
<asp:ListItem Text=”3-5″ Value=”3″></asp:ListItem>
</asp:DropDownList>
<label for=”exampleInputFile”>Upload Resume</label>
<asp:FileUpload ID=”fileUploader” runat=”server” />
<asp:Button ID=”bttn_Send” Text=”Submit” runat=”server” OnClick=”bttn_Send_Click” class=”btn” />

<!– Form ends here –>

Index.aspx.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void bttn_Send_Click(object sender, EventArgs e)
{
string from = “info@suryarpraveen-wordpress.com”;
string textTo = “careers@suryarpraveen-wordpress.com”;
using (MailMessage mail = new MailMessage(from, textTo))
{

mail.Subject = “Careers – Surya R Praveen WordPress”;

mail.Body = string.Format(@”
Name: {0}
Email: {1}
Contact: {2}
Job: {3}
Experience: {4}
“, txtName.Text, txtEmail.Text, txtcontact.Text, txtjobTitle.SelectedItem.Text, txtjobExp.SelectedItem.Text);

if (fileUploader.HasFile)
{
string fileName = Path.GetFileName(fileUploader.PostedFile.FileName);
mail.Attachments.Add(new Attachment(fileUploader.PostedFile.InputStream, fileName));
}
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = “mail.suryarpraveen-wordpress.com”;
smtp.EnableSsl = false;
NetworkCredential networkCredential = new NetworkCredential(from, “password@007”);
smtp.UseDefaultCredentials = true;
smtp.Credentials = networkCredential;
smtp.Port = 25;
smtp.Send(mail);
ClientScript.RegisterStartupScript(GetType(), “alert”, “alert(‘Message has been sent successfully.’);”, true);
}
}
}

Advertisements