1
votes

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
I know some mail servers will rewrite the From/From: based on the auth user (i'm assuming you're doing smtp auth) as a security feature. Depending on what your intent is, it may be simpler/easier to just use the reply-to header instead - see msdn.microsoft.com/en-us/library/… - alternatively, if you want to validate that the rewrite is what's going on, run your own mail server for testing (I love hMailServer for this - hmailserver.com) - James Manning
Okay, I tried the below with something like this and still not working. message = new MailMessage(ReplyToList[0].toString(), toEmail, subject, comment); - timmy
How is your smtp set up? To which STMP server are you connected? Are you authenticating the gmail server? If so, it might be preventing you from changing the 'from' address to prevent spam etc... I've never experienced this situation when I have authenticated against my corporate Exchange server using my NetworkCredentials. - CodeMonkeyKing
I tried the below and still not working. I added my web.config. Any other suggestions? - timmy

3 Answers

2
votes

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);
0
votes

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
votes

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;