开发者

MailMessage ASP.NET Wrong from email?

开发者 https://www.devze.com 2023-02-12 21:20 出处:网络
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

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 = "myemail@gmail.com";
    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("myemail@gmail.com");
    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" pass开发者_开发技巧word="mypassword" enableSsl="true"/>
    </smtp>
  </mailSettings>

any help?


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号