المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Problem sending an emai



C# Programming
09-05-2009, 10:42 PM
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

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("myGmailAccount@gmail.com", "xxxxxxxxx", txtEmail.Text, "myHotMailAccount@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;
}
}



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;
}

}