MailMessage ASP.NET Wrong from email?

2.7k Views Asked by At

I have an event for a button, that simply emails me whatever the user entered. I get the message fine....subject....message. But the from email part is just showing from myself (same as the one it is going to)

How to do I have it show from whatever email address they entered?

I'm using gmail smtp to a gmail account that I have set up.

MailMessage has 4 parameters (from, to, subject, body)

txtEmail.Text does hold their email address correctly.

protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
    this.toEmail = "[email protected]";
    this.subject = txtSubject.Text;
    this.fromEmail = txtEmail.Text;
    this.comment = txtComment.Text;
    message = new MailMessage(fromEmail, toEmail, subject, comment);
    smtp.Send(message);
    message.Dispose();
}

I tried the suggestion like below with something like this... and still showing from myself.

message = new MailMessage(ReplyToList[0].toString(), toEmail, subject, comment);

I even tried doing it this way and still shows from myself. I even stepped through the code to make sure, it was holding different email addresses and it is.

protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{

    this.subject = txtSubject.Text;
    this.comment = txtComment.Text;

    to = new MailAddress("[email protected]");
    from = new MailAddress(txtEmail.Text);

    MailMessage message = new MailMessage(from, to);
    message.Subject = txtSubject.Text;
    message.Body = txtComment.Text;
    message.Headers.Add("Reply-To", txtEmail.Text);
    smtp.Send(message);
    message.Dispose();
}

In the code I just call SmtpClient client = new SmtpClient();

Then in my web.config I have

  <mailSettings>
    <smtp from="bob">
      <network host="smtp.gmail.com" port="587" userName="myemail" password="mypassword" enableSsl="true"/>
    </smtp>
  </mailSettings>

any help?

3

There are 3 best solutions below

0
On

As James Manning has suggested an easy way of doing this would be to set a reply-to header on the email before sending as follows:

this.ReplyToList.Add(txtEmail.Text);
1
On

Try creating your SmtpClient like this (you didn't include it so this may not be your problem).

    var client
            = new SmtpClient(SmtpHostname)
              {
                  DeliveryMethod = SmtpDeliveryMethod.Network,
                  Credentials = CredentialCache.DefaultNetworkCredentials
              };
0
On

This always has worked for me although not verified through gmail smtp servers:

MailMessage message = new MailMessage(from, to);
message.ReplyToList.Add(new MailAddress(from));
message.Subject = txtSubject.Text;