0

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

lopezbertoni
  • 3,551
  • 3
  • 37
  • 53
  • Not all email severs don't let you send email from anyone. Its to do with security. http://en.wikipedia.org/wiki/Open_mail_relay Unless you verify in gmail that test@mail.be is owned by you, gmail will not let you email using that as the from address. – lahsrah Mar 18 '13 at 20:48
  • I also get it when I use a valid mail address like a yahoo address – Nathalie De Hertogh Mar 18 '13 at 21:05
  • Can you send email through your Gmail (NOT code) using this as a from address? Have you actually added it to your Gmail as a valid from address? Read this: http://support.google.com/mail/answer/22370?hl=en – lahsrah Mar 18 '13 at 22:16
  • the thing is, I don't want to send an e-mail, other people have to send me a message (when they ask a question, I have to get an authomatic e-mail with the address of the person who asked a question and other stuff, or I have to get a norification that someone has posted a message e.g via noreply@mail.com) – Nathalie De Hertogh Mar 18 '13 at 22:30
  • this is actually a test, the mail address has to change to something else and depends on the person someone has chosen. – Nathalie De Hertogh Mar 18 '13 at 22:35
  • Then it will be easier to do the latter. noreply@yourdomain.com and in subject put who it is / their email. And if you need to send email as any user then you need to use your own mail server, no public mail server like gmail will let you send emails from any email address. – lahsrah Mar 18 '13 at 22:36
  • I also thought the last one would be easer, but is it the other one even possible? – Nathalie De Hertogh Mar 19 '13 at 06:51
  • Yes it is possible if you have a mail server that allows it. I have done it before with my own server, but it of course wont work with gmail's server. Another option is to use your own from address but change the "Name" to the user that's sending the comment and set "ReplyTo" address as their address. – lahsrah Mar 19 '13 at 23:04

2 Answers2

0

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

0

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.
TLama
  • 75,147
  • 17
  • 214
  • 392
Khalleo
  • 1
  • 1