سؤال

I'm trying to send an email to myself after someone has posted a question. Although I'm receiving an email, it isn't what I need: the from mailaddress is my own instead of the person who asked the question.

Is it possible to send a mail to yourself (I don't know the smtp host, username or password from the person who's asking the question).

I'll add some code in case you need it.

QuestionsController.cs

MailMessage msg = new MailMessage();
msg.From = new MailAddress(question.Author + "<" + question.MailAuthor + ">");
msg.To.Add("myGmailAddress");
msg.Subject = question.Title;
msg.Body = question.Message;
msg.Sender = new MailAddress(question.MailAuthor);
SmtpClient client = new SmtpClient();
client.Send(msg);

My code first didn't include msg.sender, but I tried it by adding a sender, but that also didn't help.

Web.config (outside the view folder)

<system.net>
    <mailSettings>
      <smtp from="myGmailAddress">
        <network 
          host="smtp.gmail.com" 
          password="myPassword" 
          userName="myGmailAddress" 
          port="587" 
          enableSsl="true" 
          defaultCredentials="false" />
      </smtp>
    </mailSettings>
</system.net>

Also tried it without the from part and defaultCredentials="false".

When I debug the code I can see that the from in msg starts with my gmail address and changes when he gets past the msg.From part. But when I go to my mail I get this: enter image description here

هل كانت مفيدة؟

المحلول

Thanks to sylon's commments I decided to create a noreply address to inform the person(s) of the new question.

نصائح أخرى

I have had they same problem and fix it without the noreply,I asume your host server does not have problem with it like the Gmail one you can try to do this:
1. send email through your host server(I didnt use Web.config):

 [HttpPost]
 public ActionResult SendMail(Question question)
  {
    string retValue = "There was an error submitting the form, please try again later.";
    if (!ModelState.IsValid)
        {
           return Content(retValue);
        }

    if (ModelState.IsValid)
        {                    
           using (var client = new SmtpClient
           {                  

              host="mail.yourDomain.com" //mail.yourDomain.be,if your using belgium,
              Port = 587,
              EnableSsl = true,
              UseDefaultCredentials=false,
              Credentials = new NetworkCredential("yourDomainEmailAddress","PasswordOfDomainMail"), 
                        DeliveryMethod = SmtpDeliveryMethod.Network})
         {
              var mail = new MailMessage();

              mail.To.Add("yourDomainEmailAddress"); 
              mail.From = new MailAddress(question.MailAuthor, question.Author);
              mail.Subject = String.Format(question.Title);
              msg.Body = question.Message;                       
              mail.ReplyToList.Add(yourDomainEmailAddress);
            try 
                {           
                 client.Send(mail);
                     retValue = "Your Request for Contact was submitted successfully. We will contact you shortly.";
                }
                        catch (Exception)
                        {

                            throw;
                        }
                    }
                }
                return Content(retValue);
            }
  1. on your host server forward your domain Email address to your Gmail adress.
  2. on your gmail account click Settings => Accounts =>Send mail as add your domain Email address, you will be asked to fill confirmation code just close it return to your gemail you will get a link click on it to get it done.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top