Click here to Skip to main content
15,906,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my problem:

i have contact form with

from:
subject:

message body:

but the error is this code send from me to me and not define textbox that have from email(textfrom)

SmtpClient client = new SmtpClient();
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = true;
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        
       
               
                
                System.Net.NetworkCredential credentials =
                    new System.Net.NetworkCredential("my account in gmail", "my password");
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;
                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

                msg.To.Add("test@yahoo.com");
                msg.From = new MailAddress(textfrom.Text);
                
                msg.Subject = txtSubject.Text;
                msg.IsBodyHtml = true;
                msg.Body = textbody.text;
               
        
       

        try
        {
            client.Send(msg);
            Label1.Text = "Your message has been successfully sent.";
        }
        catch (Exception ex)
        {
            Label1.ForeColor = Color.Red;
            Label1.Text = "Error occured while sending your message." + ex.Message;
        }
        }
Posted
Comments
Toniyo Jackson 19-Jul-11 2:22am    
What is the error?
walterhevedeich 19-Jul-11 2:22am    
What's the exact error?
shms_rony 19-Jul-11 2:31am    
the error is this code send from me to me and not define textbox that have from email(textfrom)

1 solution

Look at the MailAddress overloads[^]:
msg.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
That may solve your problem.
There is a generic Email routine which is fully working here: Sending an Email in C# with or without attachments: generic routine.[^] - that may also help you understand.
 
Share this answer
 
Comments
shms_rony 19-Jul-11 2:52am    
ok.. this is good but the error now is that the message sending from NetworkCredential mail to (msg.to)

i want to send from (textfrom "msg.From") to (msg.to)
OriginalGriff 19-Jul-11 3:16am    
I'm sorry, but I don't understand what you are saying.
Please try to explain in more detail.
shms_rony 19-Jul-11 7:32am    
ok...when i send message successfully but it send from the e-mail that exist here NetworkCredential("my account in gmail", "my password");
i want to send from the email the i entered in textboxfrom

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900