I Could not understand what is happening on my code. When i try to send an email to another email address it keeps on sending on my gmail account.
eg.
From:xxx@gmail.com
to: yyy@yahoo.com

The email which supposed to be sent on yyy@yahoo.com, instead it send to xxx@gmail.com.
here is my code. I used gmail server on this.

private void btnSend_Click(object sender, EventArgs e)
{
            try
            {
                
                MailMessage mail=new MailMessage();
                MailAddress myAddress = new MailAddress(txtFrom.Text);
                MailAddress myReceipient = new MailAddress(txtTo.Text);

                mail.From = myAddress;
                mail.To.Add(myReceipient.Address);
                
                mail.Subject = txtSubject.Text;
                mail.Body = txtBody.Text;

                client.Host = "smtp.gmail.com";
                client.Port = 587;
                            
                client.Credentials = new NetworkCredential("xxx@gmail.com","<somepassword>");
                client.DeliveryMethod = SmtpDeliveryMethod.Network;   
                client.EnableSsl = true;
                
                client.Send(mail);
                MessageBox.Show("Message Sent");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

Recommended Answers

All 4 Replies

Is the email you are sending from a valid address for that domain? If not then gmail is probably not sending using that address as it doesn't comply with the security credentials you supplied.

Is the email you are sending from a valid address for that domain? If not then gmail is probably not sending using that address as it doesn't comply with the security credentials you supplied.

yes.. it is a valid email address, perhaps the things that i dont understand is the first time i run it work perfectly but the moment when i re run again, their the problem starts. so i need to close again me work, open again. i do not know if their is a sort of refresh method that refresh the smpt server??

Are you manually logging in to your gmail account between runs of the program?
If so, you could be creating a cache of your credentials that the server is using by default.

Do you have a yahoo account? Can you use the yahoo server to send mail instead of the gmail server?

Are you manually logging in to your gmail account between runs of the program?
If so, you could be creating a cache of your credentials that the server is using by default.

Do you have a yahoo account? Can you use the yahoo server to send mail instead of the gmail server?

Thanks you all. I have already solve the problem. I just found out that one of my variable is affecting the the text that had been displayed on my txtTo Textbox. Fool of me i did not see it right away. but any way the code is working it might help on others. Thanks.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.