2

I have 2 "Send mail as" in my Gmail account:

enter image description here

On my C# code I have this:

MailAddress from = new MailAddress("second@domain.org", "Contact");
MailAddress to = new MailAddress("myotheremail@domain.org");

MailMessage message = new MailMessage(from, to);
message.Subject = "Subject";
message.From = new MailAddress("second@domain.org");

SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("first@domain.org", "password");

client.Send(message);

The address second@domain.org is a group of addresses in GSuit.

Even using 2 "From" in "MailMessage" I couldn't send emails from my secondary address, it keeps sending from my main (first@domain.org)

If I try to compose an email in my Gmail account I can choose my secondary address and send an email.

Bruno
  • 57
  • 1
  • 9
  • 2
    what error do you get? I'm fairly sure the network credentials need to be your actual gmail account and password. – JonasH Mar 05 '20 at 10:15
  • No errors are shown. It just sends the email using the main address ("first@domain.org") – Bruno Mar 05 '20 at 10:22
  • 1
    Does this answer your question? [How to change from-address when using gmail smtp server](https://stackoverflow.com/questions/1332510/how-to-change-from-address-when-using-gmail-smtp-server) – mortb Mar 05 '20 at 10:37
  • @mortb Looks like i am only able to send emails from address `second@domain.org` if i log in into it, but i can't login because it is a group of addresses... So it is impossible to send emails from my `second@domain.org` then – Bruno Mar 05 '20 at 10:59
  • @Bruno did you try this: https://stackoverflow.com/a/51460935/1257728 – mortb Mar 05 '20 at 11:15
  • @mortb That's what i already did. The image in my post represents it. – Bruno Mar 05 '20 at 11:22
  • There is a property `ReplyTo` if you want the default address for answers to you mail to be sent to `second@domain.org`. https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.replyto?view=netframework-4.8 Of course it may or may not be supported by gmail smtp – mortb Mar 05 '20 at 11:58

1 Answers1

0

Looks like what I had to do was just wait... Now it works fine without changing any code.

Bruno
  • 57
  • 1
  • 9