I'm creating a Windows app and have a form where the user can fill out his/her name and email address, a subject line, and a message, then clicking a Send button to send an email to me about any question or issues they may be experiencing using my app. I'm testing out what I have, but having some issues. I'm using System.Net.Mail for sending the email. First I tried (but including a try-catch):
private void btnSend_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(txtEmailAddress.Text, txtSender.Text);
mail.To.Add(strMyEmailAddress);
mail.Subject = txtSubject.Text;
mail.Body = txtMessage.Text;
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
}
This gave me the following error in my catch:
Error: System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
Then I tried:
private void btnSend_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(txtEmailAddress.Text, txtSender.Text);
mail.To.Add(strMyEmailAddress);
mail.Subject = txtSubject.Text;
mail.Body = txtMessage.Text;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(strMyGmailAddress, strMyGmailPassword);
smtp.Timeout = 20000;
smtp.Send(mail);
}
This worked, but in my inbox, the "sender" was my own Gmail address... not my test email address I filled into my winform in the "From" field. I want (need) the sender's email address.
Any ideas on where I went wrong in my first attempt? Or how I can work around my second attempt to get the sender's email address?