.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Sending Feedback mail from contactus page, asp.net

Posted By: Balwant     Posted Date: February 25, 2011    Points:2   Category :ASP.Net
Hi

can we use gmail smtp in live website to send mail(feedback) from contactus form to the owner of the website, is it possible?



mail is send to the desire email id from local host from feedback form.
but problem occur when the same project uploaded to the live web server.
on clicking the submit button in the feedback form nothing happen.

what and where is the problem i can't get that?
because this application behave normal at local-host.

please guide me regarding this problem.

this line is appended with the url while clicking on submit button.

__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKMTk2Njc5ODE2N2RkEGPdgAflCjv7N%2Fj8z04OHUSWDag%3D&__EVENTVALIDATION=%2FwEWBgLzi4feDwK0w7i7DQK9w4QnAqHo7y4C9oCMmQwCro6e7AMCSNU4FMIyaDwIGsIYTXrxUJrXnQ%3D%3D&ctl00%24ContentPlaceHolder1%24txtName=balwant&ctl00%24ContentPlaceHolder1%24txtMail=balwant.mnd%40gmail.com&ctl00%24ContentPlaceHolder1%24txtFeed=testing&ctl00%24ContentPlaceHolder1%24btsubmit=Submit


my code for sending email is as follow:my code for sending email is as follow:

protected void btSubmit_Click(object sender, EventArgs e)
{
try
{

SmtpClient smtpClient = new SmtpClient();
MailMessage objMail = new MailMessage();
//From Address will be assigned from the e-mail specified in the From TextField You can also give the code given below.
MailAddress objMail_fromaddress = new MailAddress(txtMail.Text);
MailAddress objMail_toaddress = new MailAddress("mymail@gmail.com");
objMail.IsBodyHtml = true;
//Assigning From address to the MailMessage class
objMail.From = objMail_fromaddress;
//Assigning To address to the MailMessage class as a collection
objMail.To.Add(objMail_toaddress);
objMail.Subject = "Feedback from" + txtName.Text + " to us from Contact us Form";
objMail.Body = "Message from" + txtName.Text + "as comments:-" + txtComments.Text + "<br><br><br>" + "Address:" + txtAddress.Text + "<br>Mobile no:" + txtTel.Text;
objMail.Priority = MailPriority.High;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587
smtpClient.Credentials = new System.Net.NetworkCredential("mymail@gmail.com", "1239807643");
smtpClient.EnableSsl = true;
smtpClient.Send(objMail);
Label1.Visible = true;
Label1.Text = "your feedback is submitted successfully.";
}
catch (Exception ex)
{

Label1.Visible = true;
Label1.Text = "something went wrong!, Please Try Again." + ex.Message;
}

Responses
Author: Manas Patnaik             
Posted Date: February 25, 2011     Points: 5   

Its Working at your local machine because you have all the required assemblies such as System.Net.Mail etc.
Make sure all the reference DLLs are in the Delpoyed Bin Folder.



Manas Patnaik
http://manaspatnaik.com/blog
Author: Deep Gautam             
Posted Date: February 25, 2011     Points: 5   

Dear Go through n\below code.
It will definatily help you.

Sending Email using C# and ASP.Net 2.0
Most often, we will have requirements to send email from our asp.net applications. This article will help us understand some of the available techniques we can use to send email from asp.net applications. With the introduction of .Netframework 2.0, the classes for sending email are packed in System.Net.Mail namespace as opposed to System.Web.Mail namespace in 1.x framework. Moving forward we will see,
? Sending a Simple Mail
? Sending Mail with Attachment
? Sending Mail with HTML body
? Sending Email with Embedded Image in the Message Body
? Sending Email with Gmail SMTP Server from ASP.Net

Sending a Simple Mail
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("from@gmail.com");
mail.Subject = "Test Email";
string Body = "Welcome to CodeDigest.Com!!";
mail.Body = Body;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Send(mail);

The above code can be used to send a simple email.

Sending Mail with Attachment
We can also send email with attachments. The below code will help you to achieve it.

MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("From@gmail.com");
mail.Subject = "Test Email";
string Body = "Welcome to CodeDigest.Com!!";
mail.Body = Body;
mail.Attachments.Add(new Attachment(@"F:\Articles\Email in ASP.Net 2.0\SendEmail\mail.png"));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Send(mail);

Sending Mail with HTML Body
Sometimes, we will have requirements to send email as a HTML body. The below code will help you to achieve the same.
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("From@gmail.com");
mail.Subject = "Test Email";
string Body = "Welcome to CodeDigest.Com!!";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Send(mail);

We may also have requirements to send email with an embedded image in the message body. This was made easy with ASP.Net 2.0 with the help 2 new classes that are packed in .Netframework 2.0. Next section will help us understand the same.

Sending Email with Embedded Image in the Message Body
We can embed image in the email body using LinkedResource class and AlternateView class that is packed with System.Net.Mail namespace and by making the message body as HTML.

How to achieve this?
We have to include a <IMG> with a contentid specified in src attribute of <IMG> tag in the message body, i.e. the HTML body should contain <img src="cid:imageId" />.
Example will be,
<b>View my Pic</b><br>
<img src="cid:Anyid" />

We need to give the same contentid to the ContentId attribute of LinkedResource object for the code to work.

Implementation
try
{
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("from@gmail.com");
mail.Subject = "Test with Image";
string Body = <b>Welcome to codedigest.com!!</b><br><BR>Online resource for .net articles.<BR><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >";

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\codedigest.png", "image/png");
imagelink.ContentId = "imageId";
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink);
mail.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.Send(mail);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}





OUTPUT


Sending Email with Gmail SMTP Server from ASP.Net
To send email using gmail SMTP server you will require a valid gmail userid and password. If you didn't specify a valid gmail userid and password then you will get the following error.

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

The below code can be used to send email using gmail SMTP server, smtp.gmail.com.

MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("from@gmail.com");
mail.Subject = "Test Email";
string Body = "<b>Welcome to CodeDigest.Com!!</b>";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
smtp.EnableSsl = true;
smtp.Send(mail);

Web.Config
<appSettings>
<add key="SMTP" value="smtp.gmail.com"/>
<add key="FROMEMAIL" value="mail@gmail.com"/>
<add key="FROMPWD" value="password"/>
</appSettings>



Deep Gautam
Happy Coding

Author: Balwant             
Posted Date: February 28, 2011     Points: 20   

hi,
Dear Deep, code id not the problem because the code i have used is also same but mail is not sent on button click. and the problem is the below text which append with website url on clicking button.
i have used master page so the contentPlaceHolder is shown in the below text.
Please have look at this text.

__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKMTk2Njc5ODE2N2RkEGPdgAflCjv7N%2Fj8z04OHUSWDag%3D&__EVENTVALIDATION=%2FwEWBgLzi4feDwK0w7i7DQK9w4QnAqHo7y4C9oCMmQwCro6e7AMCSNU4FMIyaDwIGsIYTXrxUJrXnQ%3D%3D&ctl00%24ContentPlaceHolder1%24txtName=balwant&ctl00%24ContentPlaceHolder1%24txtMail=balwant.mnd%40gmail.com&ctl00%24ContentPlaceHolder1%24txtFeed=testing&ctl00%24ContentPlaceHolder1%24btsubmit=Submit


Logic Lab technologies
Email:balwant.mnd@gmail.com
Website:www.techlogiclab.com


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend