Send email via gmail SMTP

I am trying to send an email using gmail SMTP in C # using the following code

MailMessage message = new MailMessage(); message.To.Add("my email"); message.Subject = "subject"; message.From = new MailAddress("any email"); message.Body = "body"; message.Attachments.Add(new System.Net.Mail.Attachment(path)); SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587); smtp.EnableSsl = true; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential("my user", "my pass"); smtp.Send(message); 

When I receive an email, the FROM field is populated by my user . I use UseDefaultCredentials as false . When I look at the result, the OT field is populated by my user . Should the OT field be filled in with any email ? How to send an email using any email as a sender?

+7
source share
2 answers

Running the code snippet, I get:

 Return-Path: <my user> Received: from Psi ([80.92.234.64]) by mx.google.com with ESMTPS id f1sm20531634wiy.2.2012.10.08.10.07.49 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 08 Oct 2012 10:07:49 -0700 (PDT) Message-ID: < 50730865.2152b40a.13ea.28ec@mx.google.com > Sender: Roman R. <my user> MIME-Version: 1.0 From: any email To: my email Date: Mon, 08 Oct 2012 10:07:49 -0700 (PDT) Subject: Subject Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Body 

Sender is the email address used to authenticate with Google Mail. From is the "from" value specified in the code. The recipient application may be misleading two, and the rest will look as expected. Some email clients present From + Sender (when they are different) as "sent by the sender on behalf of From."

You may be worried about the fact that Google Mail still displays the account with which the email is sent through the Sender field, but this is how it works. You are submitting this account.

And another possible reason is the From email address. If you added it to your Google Mail account as one of your own addresses (and confirmed using a test message via the link), then Google Mail will allow you to place it in the From field. Otherwise, he may discard it and replace it with Sender .

+4
source

I use the same to send emails using GMail as a service. I initially set the .From property to " noreply@mydomain.com ", but the message still arrives with the From header set to the account used for authentication.

Faced with this problem, I used the ReplyToList property ( .ReplyToList.Add(MailAddress)) ) so that the recipients who responded to the message sent a response to an email account other than the “automated” one that we use to send outgoing messages.

Edit:

For more information, see this thread on Google Groups . Also, a related stack overflow response .

+5
source

All Articles