.NET Framework Bookmark and Share   
 index > Network Class Library (System.Net) > Problem sending an email
 

Problem sending an email

I am using the followijg code to send email trough my Gmail account but I am having two problems, first it's taking long time to send the email and second the From is showing as my Gmail account not the user email specefied in the txtEmail




        public static bool SendMail(string gMailAccount, string password, string from, string to, string subject, string message)
        {
            try
            {
                NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(from);
                msg.To.Add(new MailAddress(to));
                msg.Subject = subject;
                msg.Body = message;
                msg.IsBodyHtml = true;
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = loginInfo;
                client.Send(msg);

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            txtEmail.Enabled = false;
            txtMessage.Enabled = false;
            btnSend.Enabled = false;
            btnClose.Enabled = false;

            if (radioSuggestion.Checked == true)
                email_subject = "Suggestion from (My System) user";
            else if (radioBug.Checked == true)
                email_subject = "Bug Reported by (My System) user";
            else
                email_subject = "Email from (My System) user";

            if (SendMail("myGmail@gmail.com", "xxxxxxx", txtEmail.Text, "myHotmail@hotmail.com", email_subject, txtMessage.Text))
            {
                MessageBox.Show("Message Sent.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Cursor = Cursors.Default;
                this.Close();
            }
            else
            {
                MessageBox.Show("unable to send your email!!", "Email", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.Enabled = true;
                txtMessage.Enabled = true;
                btnSend.Enabled = true;
                btnClose.Enabled = true;
                this.Cursor = Cursors.Default;
                return;
            }
        }


Jassim Rahma
Jassim Rahma

The email is send by the email id for which you supply password in network credential. So the email address in from and email address in NetworkCredential would be same.


Gaurav Khanna
Khanna Gaurav
sorry I am confused?!! The email in the NetworkCredential is the email which I will use its SMTP gateway, meants it should be my SMTP (my email).. but the email in the from is the customer email which he will enter in the From textbox on my form. then what?
Jassim Rahma
Jassim Rahma

I meant that, NetworkCredential will contain From mailAddress and it's password.

I don't think EmailAddress in From and EmailAddress in NetworkCredential can be different.


Gaurav Khanna
Khanna Gaurav
then I guess the only way to get the email is by passing it to the ReplyTo!!

what a bad option!!!

Jassim Rahma
Jassim Rahma

You can use google to search for other answers

Custom Search

More Threads

• How can I encryption in 128 bit file Upload and Download from Server...?
• Dns.GetHostEntry(server) does not work sometimes, however Dns.GetHostByAddress(server) does
• Is this Server Efficent (Listening Method)- Case1
• Originate mail from an external dll
• Unable to read data from Transport Connection
• HttpWebRequest Sending Multiple Packets
• Link error LNK2028 in TCP/IP routines
• Solve RRAS Hangup Problem in .Net 1.1
• Force TLS using System.Net.SmtpClient
• VB.net - Enumerate all the host addresses in a network