Solved

How can I create a contact form with attachment in ASP.Net & C#?

Posted on 2008-10-14
4
524 Views
Last Modified: 2013-12-17
Experts,

I need to create a form to send a few text details and a text/word file to a set email address. I have the form designed in the front page. Where I'm having a problem is in getting working C# code. I have tried a few different examples I've found with no luck.

Below is my front page code and what I have in the.cs which won't compile at the moment.

Can anyone help?
.aspx:
 

<%@ Page Title="Ambassador Recruitment - Jobseekers - Submit CV" Language="C#" MasterPageFile="~/Right.master" AutoEventWireup="true" CodeFile="submitcv.aspx.cs" Inherits="jobseekers_submitcv" %>
 

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_Text" Runat="Server">

	<h1>Submit CV</h1>

	<div>

		<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">

			<tr>

				<td align="right" bgcolor="#eeeeee" style="width: 150px">&nbsp;Name</td>

				<td bgcolor="#FFFFFF"><asp:TextBox ID="TextBox1" runat="server" Columns="50"></asp:TextBox>

				</td>

			</tr>

			<tr>

				<td align="right" bgcolor="#eeeeee" style="width: 150px">&nbsp;Email</td>

				<td bgcolor="#FFFFFF"><asp:TextBox ID="txtFrom" runat="server" Columns="50"></asp:TextBox>

				</td>

			</tr>

			<tr>

				<td align="right" bgcolor="#eeeeee" style="width: 150px">&nbsp;Attach CV</td>

				<td bgcolor="#FFFFFF">

					<asp:FileUpload ID="txtAttachmentPath" runat="server" />

				</td>

			</tr>

			<tr>

				<td align="right" bgcolor="#eeeeee" style="width: 150px">&nbsp;Employment Division</td>

				<td bgcolor="#FFFFFF">

					<asp:dropdownlist ID="DropdownSubitDivision" runat="server" 

						DataSourceID="XMLSourceJobCategory" DataTextField="display" 

						DataValueField="value" Width="150px"></asp:dropdownlist>

				</td>

			</tr>

			<tr>

			<td align="right" bgcolor="#eeeeee" style="width: 150px">&nbsp;Optional Comment</td>

			<td bgcolor="#FFFFFF"><asp:TextBox ID="txtBody" runat="server" Columns="40" TextMode="MultiLine"></asp:TextBox></td>

			</tr>

			<tr>

			<td align="right" bgcolor="#eeeeee" style="width: 150px">&nbsp;</td>

			<td bgcolor="#FFFFFF"><asp:Button ID="btnSubmit" runat="server" Text="Send Email" /></td>

			</tr>

		</table>

	</div>

</asp:Content>
 

.aspx.cs:
 

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 jobseekers_submitcv : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        MailMessage objMM = new MailMessage();

        objMM.To = "somebody@gmail.com";
 

        objMM.From = txtemail.Text;

        objMM.BodyFormat = MailFormat.Text;
 

        objMM.Priority = MailPriority.High;
 

        ///////////////file attachment//////////////
 

        objMM.Attachments.Add(new System.Web.Mail.MailAttachment(FileUpload1.PostedFile.FileName));

        objMM.Body = DropdownSubitDivision txtBody.text;
 

        SmtpMail.SmtpServer = "smtp.gmail.com";
 

        SmtpMail.Send(objMM);

    }

}

Open in new window

0
Comment
Question by:Denisdh
  • 2
  • 2
4 Comments
 
LVL 16

Assisted Solution

by:Gyanendra Singh
Gyanendra Singh earned 500 total points
ID: 22713115
0
 

Author Comment

by:Denisdh
ID: 22715135
Thanks for that. I've found this link before but got stuck trying to find a SMTP that would work and setting up a attachement browse button.

I get:

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. w5sm31230747mue.10 at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at jobseekers_submitcv.btnSubmit_Click(Object sender, EventArgs e) in c:\Users\Denis\Desktop\Ambassador\Ambassador-Recruit.com\jobseekers\submitcv.aspx.cs:line 35

when I use smtp.gmail.com

0
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 500 total points
ID: 22718669
hmm you need to add this line in your config file

 <mailSettings>

<smtp from="myEmailFrom">

<network host="smtp.mydomain.com" password="mypwd" userName="myUserName" defaultCredentials="true"/>

</smtp>

</mailSettings>
0
 

Author Comment

by:Denisdh
ID: 22719290
I also had to add emailClient.EnableSsl = true; to get gmail to work.



            MailAddress SendFrom = new MailAddress("blah@gmail.com");

            MailAddress SendTo = new MailAddress("blahb;ah@gmail.com");
 

            MailMessage MyMessage = new MailMessage(SendFrom, SendTo);
 

            MyMessage.Subject = "Website CV Submission from " + txtName.Text + " " + txtPosition.Text + " " + txtDivision.Text;

            MyMessage.Body =    "Name: " + txtName.Text + "\n \n" +

                                "Email: " + txtEmail.Text + "\n \n" +

                                "Address: " + txtAddress.Text + "\n \n" +

                                "Contact No: " + txtContact.Text + "\n \n" +

                                "Position: " + txtPosition.Text + "\n \n" +

                                "Division: " + txtDivision.SelectedItem.Text + "\n \n" +

                                "Salary: " + txtSalary.Text + "\n \n" +

                                "Comment: " + txtComment.Text;
 

            Attachment attachFile = new Attachment(txtCV.Text);

            MyMessage.Attachments.Add(attachFile);
 

            SmtpClient emailClient = new SmtpClient("smtp.gmail.com");

            emailClient.EnableSsl = true;

            emailClient.Send(MyMessage);
 

 
 

 and to my web.config:
 

    <system.net>

    <mailSettings>

      <smtp from="blahblah@gmail.com">

        <network host="smtp.gmail.com" port="587"

         userName="blahblah@gmail.com" password="blahblah"

         defaultCredentials="false" />

      </smtp>

    </mailSettings>

  </system.net>

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ASP.NET e-commerce website 4 29
Hidden Field Value 10 34
Closing all open child forms. 4 23
ASP.NET Web API or ASP.NET Core MVC? 3 25
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now