0

This should be simple, but there is something I'm not getting. The user fills out info and the btnClick does the code.

I would think the msgToAdd would be who the e-mail is going to. (Testing as my email.)

And that the msgFromAdd would be who the message is from, which is input by the person browsing. (Testing as my wife's email.)

Then the credentials I put in my email/password. (Which in reality, anything "mine" would be x company. So I would need to code in the company email password? :-/)

Anyway, I successfully send email to my address, but it says it's coming from my address and I need it to come from whatever address the user input.

I've searched everywhere, but all the code is the same and it makes no sense to me. Thanks.

        SmtpClient smtpClient = new SmtpClient(); 

        MailMessage mailMsg = new MailMessage();

        MailAddress msgToAdd = new MailAddress("myemail@gmail.com");
        MailAddress msgFromAdd = new MailAddress(tbxEmailAdd.Text);

        mailMsg.To.Add(msgToAdd);   
        mailMsg.From = msgFromAdd;
        mailMsg.Subject = ddlEmail.SelectedValue;
        mailMsg.IsBodyHtml = true;
        mailMsg.Body = tbxEmail.Text;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
        smtp.EnableSsl = true;
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.Send(mailMsg);

So even with the overkill below, it still doesn't show the real sender's address.

        MailAddress msgFromAdd = new MailAddress(tbxEmailAdd.Text);

        mailMsg.From = msgFromAdd;
        mailMsg.ReplyToList.Add(msgFromAdd);
        mailMsg.ReplyTo = msgFromAdd;
        mailMsg.Sender = msgFromAdd;

Also, the following DOES make the name show up. Still no email address though!

MailAddress msgFromAdd = new MailAddress(tbxEmailAdd.Text, "NAME");

I'm seriously losing it.

Jamie
  • 1,579
  • 8
  • 34
  • 74
VBeginner
  • 1
  • 2

3 Answers3

3

Sounds like google don't let you mess with the "from" header? You might want to try using the "Sender" property, doesn't always render nice in all email clients tho.

MailMessage, difference between Sender and From properties

Community
  • 1
  • 1
russau
  • 8,928
  • 6
  • 39
  • 49
2

I would check your web.config (if you have one). There may be a setting like:

<system.net>
    <mailSettings>
      <smtp from="your address here">
        <network host="localhost" password="" userName=""/>
      </smtp>
    </mailSettings>
 </system.net>

that could be overriding your settings.

davecoulter
  • 1,806
  • 13
  • 15
  • Thanks. There isn't anything in the web.config that is overriding. The email goes through just fine. It just doesn't want to say that it's being sent from the inputted address. – VBeginner Jul 22 '11 at 01:14
0

you're sending email with your smtp credential, so the "from" should be your email. the "From" property in MailMessage is just an optional choise in case you're using an open smtp that needn't credentials.

gekowa
  • 452
  • 2
  • 10
  • Thanks. That makes sense... I think. From all that I've searched, I could only find examples with credential stuff like this. At the end of the day, I just want the user to input their email address, message, and hit send. And for it to go to my email address saying that it's from them, (and not me, which is what's happening), so when I reply it goes to their email. – VBeginner Jul 22 '11 at 01:27