473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Need help with sending a form data to email.

Hai there,

Im newbie here. Im creating a form that can be send it data to email when click submit. Im using C# as programming language.

Check wheter the coding that I keyin. Here is the coding.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. MailMessage mail = new MailMessage(txtEmail.Text , "someone@gmail.com");
  4.  
  5.  
  6.         mail.Subject = ddSubject.Text;
  7.         mail.Body = "Name : " + txtName.Text +
  8.             "<br /><br />Designation : " + txtDesignation.Text +
  9.             "<br /><br />Organisation : " + txtOrganisation.Text +
  10.             "<br /><br />Contact Number : " + txtDesignation.Text +
  11.             "<br /><br />Facsimile: " + txtFax.Text +
  12.             "<br /><br />Email : " + txtEmail.Text +
  13.             "<br /><br />Subject : " + ddSubject.Text +
  14.             "<br />Message : " + txtMsg.Text;
  15.  
  16.         SmtpMail.SmtpServer = "localhost";
  17.  
  18.  
  19.             try
  20.             {
  21.                 SmtpMail.Send(mail);
  22.                 lblConfirm.Text = "We thank you for your feedback/suggestion. Have a nice day!";
  23.             }
  24.             catch (Exception exc)
  25.             {
  26.                 lblConfirm.Text = "Sorry your message not send due some technical problem. Please try again";
  27.             }
Please help me. Urgent.

Thanks,
Meshack
Oct 12 '09 #1
5 2884
tlhintoq
3,525 Expert 2GB
What's the <br /> stuff? This isn't HTML, its a simple string. If you want a new line replace that stuff with "\n"

LIne 16 looks like you are running an SMTP server on this very machine. Is that the case? You are building this program on a machine running an SMTP server?

Here is a basic send message block that works for me.
The properties of "toaddress" and so on are pretty self explanitory. You can substitute your own property names for these.
TempFilePath is the path to an attachment. If you aren't using an attachment then you can kill this 'if' check.

You can see here that we actually make a new SmtpClient and give it credentials. This makes it a bit more scaleable in case you later want to move this code to a machine not directly running the SMTP server.

Expand|Select|Wrap|Line Numbers
  1.                     MailMessage myMsg = new MailMessage();
  2.                     if (!string.IsNullOrEmpty(toaddress))
  3.                     {
  4.                         myMsg.To.Add(toaddress);
  5.                         if (!string.IsNullOrEmpty(ccaddress)) myMsg.CC.Add(ccaddress);
  6.                         if (!string.IsNullOrEmpty(bccaddress)) myMsg.Bcc.Add(bccaddress);
  7.                         //TODO: Armour plate this method
  8.                         myMsg.From = new MailAddress(fromaddress);
  9.                         if (!string.IsNullOrEmpty(replyaddress)) myMsg.ReplyTo = new MailAddress(replyaddress);
  10.                         myMsg.Subject = subject;
  11.                         myMsg.Body = bodytext;
  12.                         if (System.IO.File.Exists(TempFilePath))
  13.                         {
  14.                             myMsg.Attachments.Add(new Attachment(TempFilePath));
  15.  
  16.                             SmtpClient myMailClient = new SmtpClient(mailserver, smtpport);
  17.                             myMailClient.Credentials = new System.Net.NetworkCredential(fromaddress, frompassword);
  18.                             myMailClient.EnableSsl = false;
  19.                             if (!String.IsNullOrEmpty(toaddress)) myMailClient.Send(myMsg);
  20.                             Console.WriteLine("Mail sent");
  21.                             RaiseLog("Send Email: " + toaddress);
  22.                         }
  23.                         else Console.WriteLine("No attach");
  24.                     }
  25.  
Oct 12 '09 #2
Hai there,

Thanks for reply.
It is actually <br>... When I keyin to .net, it become like that.
Thanks for the \n...

line 16 is just for a testing purpose. if i use gmail as smtp server, is it alrright???

here is the edited code using your example....

Expand|Select|Wrap|Line Numbers
  1. System.Net.Mail.MailMessage myMsg = new System.Net.Mail.MailMessage();
  2.  
  3.             myMsg.To("someemail@gmail.com");
  4.             //TODO: Armour plate this method 
  5.             myMsg.From = txtEmail.Text;
  6.             myMsg.Subject = ddSubject.Text;
  7.             myMsg.Body = "Name : " + txtName.Text +
  8.             "\n Designation : " + txtDesignation.Text +
  9.             "\n Organisation : " + txtOrganisation.Text +
  10.             "\n Contact Number : " + txtDesignation.Text +
  11.             "\n Facsimile: " + txtFax.Text +
  12.             "\n Email : " + txtEmail.Text +
  13.             "\n Subject : " + ddSubject.Text +
  14.             "\n Message : " + txtMsg.Text; ;
  15.  
  16.                 SmtpClient myMailClient = new SmtpClient("smtp.gmail.com", "587");
  17.                 myMailClient.Credentials = new System.Net.NetworkCredential("someemail@gmail.com", "somepassword");
  18.                 myMailClient.EnableSsl = false;
  19.                 myMailClient.Send(myMsg);
  20.                 Console.WriteLine("Mail sent");
  21.  
Im insert few codes like...
Expand|Select|Wrap|Line Numbers
  1. using System.Web.Mail;
  2. using System.Net;
  3. using System.Net.Mail;
  4.  
Here it is appears few errors...
line 3 - Non-invocable member 'system.net..mail.mailmessage.to' cannot be used like method.

line 5 - connot implicity convert type 'string' to 'system.net.mail.mailaddress'


How can remove this errors?

thanks,
meshack
Oct 12 '09 #3
hai there,

another question.
im using

Expand|Select|Wrap|Line Numbers
  1. using System.Web.Mail;
  2. using System.Net.Mail;
  3.  
which one should i use???

when I use .net.mail , it give error to codes...
1. cannot be assigned - it is read only

Expand|Select|Wrap|Line Numbers
  1. myMsg.To = "someemail@gmail.com";
2. cannot convert txtEmail to string

Expand|Select|Wrap|Line Numbers
  1. myMsg.From = txtEmail.Text;
thanks,
meshack
Oct 12 '09 #4
tlhintoq
3,525 Expert 2GB
here is the edited code using your example....
Did it not work the way I gave it to you?

line 3 - Non-invocable member 'system.net..mail.mailmessage.to' cannot be used like method.
Expand|Select|Wrap|Line Numbers
  1.             myMsg.To("someemail@gmail.com");
  2.  
The line originally was
Expand|Select|Wrap|Line Numbers
  1. myMsg.To.Add(toaddress);
General coding tip: If something worked one way, and you change it, and it breaks... put it back to the way it was when it worked. As we commonly say here "If it ain't broke, don't fix it"

line 5 - connot implicity convert type 'string' to 'system.net.mail.mailaddress'
Expand|Select|Wrap|Line Numbers
  1. myMsg.From = txtEmail.Text;
The line originally read
Expand|Select|Wrap|Line Numbers
  1. myMsg.From = new MailAddress(fromaddress);
Now look at the two lines and look at the error it gave you. It makes sense. The original line originally make a new mailaddress taking a string as a parameter. You changed it so it no longer made a new mail address. You got an error message that says it can't implicitly figure out how to change a string into a mailaddress.

Again, the original line of code worked. Why did you feel a need to change it? When the lines you changed broke why did you not put them back to known working code?

Understand, I'm not upset about you changing 'my' code or anything like that. It just seems that when handed working code one should recognize how to undo their own changes when it breaks.
Oct 12 '09 #5
Hai tlhintoq,

Thanks for the advice. I change it as you said. It is not giving that errors any more.
Sorry that I edited too much. Im just exploring, Im new to c# and learning.

Sorry again to bother you. Now I msg can't send be.

It giving this error...

Expand|Select|Wrap|Line Numbers
  1. SmtpException was unhandled by user code
for the line 24.

Expand|Select|Wrap|Line Numbers
  1. MailMessage myMsg = new MailMessage();
  2.             myMsg.To.Add("admin@email.com");
  3.                 //TODO: Armour plate this method 
  4.             myMsg.From = new MailAddress("admin@email.com");
  5.             myMsg.Subject = ddSubject.Text;
  6.             myMsg.Body = "Name : " + txtName.Text +
  7.             "\n Designation : " + txtDesignation.Text +
  8.             "\n Organisation : " + txtOrganisation.Text +
  9.             "\n Contact Number : " + txtDesignation.Text +
  10.             "\n Facsimile: " + txtFax.Text +
  11.             "\n Email : " + txtEmail.Text +
  12.             "\n Subject : " + ddSubject.Text +
  13.             "\n Message : " + txtMsg.Text; ;
  14.  
  15.             SmtpClient smtp = new SmtpClient();
  16.             smtp.Host = "mail.email.com";
  17.             smtp.EnableSsl = false;
  18.             System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
  19.             NetworkCred.UserName = "admin@email.com";
  20.             NetworkCred.Password = "somepassword";
  21.             smtp.UseDefaultCredentials = true;
  22.             smtp.Credentials = NetworkCred;
  23.             //smtp.Port = 587;
  24.             smtp.Send(myMsg);
  25.             lblConfirm.Text = "Thank you for your feedback/suggestion. We will get back to you as soon as possible. Have a nice day."; 
  26.  
  27.  
One more question..

Can this email address, be the same???
The email in this mail server name.

Expand|Select|Wrap|Line Numbers
  1. myMsg.To.Add("admin@email.com");
  2. myMsg.From = new MailAddress("admin@email.com");
  3. NetworkCred.UserName = "admin@email.com";

Thanks,
Meshack
Oct 13 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Roy G. Vervoort | last post by:
With them help of newsgroups I created a form that makes it possible to sent an email with an attachement thats on the internet.. How can i attache a file thats on the users harddrive? this...
3
by: NotGiven | last post by:
The code below is designed to loop through rows of a database query obtaining email addresses and send an email to each. It is modified form fomr some code I found on the net. With each while...
3
by: George Self | last post by:
I'm creating a PHP form handler that will send an email to a user based on form entries (a rather standard procedure). I've never had any problems with this type of programming challenge before;...
9
by: Jofio | last post by:
I am just learning PHP. I just tried coding a php script which I saved as mail.php ---------------------------- <? $name=$_POST; $email=$_POST; $comments=$_POST;
11
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding...
2
by: Kingdom | last post by:
I have a SelectBoxes.asp page that is working with multiple selection dropdown boxes to extract data and total the selection prices. Tom & Bob were kind enough to give me a big help getting this...
17
by: freemann | last post by:
Can anyone provide example code showing how to send form results to a results page, email and a comma delimited file? Notice that I need it going to all three locations. Details: I have forms...
4
by: shror | last post by:
dear all, i have started learning php 2 weeks ago and i have wrote my first script for mail sender and the script takes all my data and move to the thanks page but the problem is that the mails...
0
by: prasenjit2007 | last post by:
I have a main form for inputing the (to/from/mesg/file) with the following code:- <html> <body> <table> <tr> <td>To:</td> <td><input type="text" name="to" size="50" ...
1
by: PrabodhanP | last post by:
I have form on my website.I am sending data entered by visitor to particular email-id through php script.Now I want to add some radio buttons and depending on selacted radio button form data should...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.