1

I am building a Contact Us form for our Company's website.My requirement is that when a user sends any inquiry through our Contact Us page, it should be sent to our company's email id.Lets say sales@mycompany.in.I have been provided SMTP details smtp.mycompany.in. I earlier tried sending mail from gmail it was working. Here is my code.

MailMessage mail = new MailMessage();
mail.From = new MailAddress(txtEmail.Text);
mail.To.Add("sales@mycompany.in");
mail.Subject = "Mail from www.mycompany.in";
mail.Body =  emailbody.ToString();
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("mycompany@gmail.com", "xxxxxx");
//Or your Smtp Email ID and Password
// smtp.Port=80; 
smtp.EnableSsl = true;
smtp.Send(mail);

The problem with this code is that when I send mail it is recieved at sales@mycompany.in but it always says that the message is from mycompany@gmail.com instead of the typed email adress txtEmail.Text.I know that is because of the gmail credentials that I provided.So my queston to you guys how can I get it to work such that I know the email id of the sender or the person contacting us.Any suggestions are welcome. Thanks.

Priyank Patel
  • 6,898
  • 11
  • 58
  • 88
  • 2
    Please search the site for "sending email trough gmail [c#]" [link](http://stackoverflow.com/search?q=sending+email+trough+gmail+%5Bc%23%5D). This has been asked so many times here. – Oded Jun 11 '12 at 12:21
  • possible duplicate of [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Oded Jun 11 '12 at 12:23
  • @Oded I know it has been asked so many times.I just need to sort the problem so that I get to know the email id from the mail has been sent. – Priyank Patel Jun 11 '12 at 12:24
  • @Oded I am not asking about sending mail through gmail. – Priyank Patel Jun 11 '12 at 12:24
  • 2
    @freebird: Then you should better change your question title with your problem !!! – huMpty duMpty Jun 11 '12 at 12:25

2 Answers2

2

In Theory

You can't send a message “on behalf of” somebody else if you're using Gmail SMTP.
It won't allow you to set custom Reply-To address either.

If you roll out your own SMTP server, you should be good though.

In Practice

One way to make it less painful both for customers and the support is to set up a ticketing system.
You can code one yourself, if you like, or use any of plenty of existing ones.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • That means I should include it in my body so that I am aware of the sender.Thanks a lot. – Priyank Patel Jun 11 '12 at 12:27
  • 1
    The smtp standard doesn't prevent you from using any from-address. But I do believe GMail only allows it if that email address has specifically allowed sending on its behalf. – comecme Jun 11 '12 at 12:30
  • Yeah. One way to simplify this for end-users would be to use ticketing system. – Dan Abramov Jun 11 '12 at 12:31
2

You can't do it in this way, as some one is abc@yahoo.com other is xyz@hotmail.com, and you can't send it to appear from the account of someone else.

My suggestion is to add the email address of sender in the body of email that the guy with this email is contacting us but the solution you are trying to do, i think is not possible.

Imran Balouch
  • 2,170
  • 1
  • 18
  • 37