I’m working on a C# application that needs to send emails through Gmail’s SMTP server. The problem I’m facing is that even though I set a custom sender address in the email headers, recipients always see the Gmail account address as the sender instead of my intended from address.
Here’s my current implementation:
SmtpClient mailClient = new SmtpClient();
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = true;
mailClient.EnableSsl = true;
mailClient.Host = "smtp.gmail.com";
mailClient.Port = 587;
mailClient.Credentials = new NetworkCredential("[email protected]", "mypassword");
var senderEmail = "[email protected]";
var recipientEmail = "[email protected]";
var emailSubject = "Order Confirmation";
var emailContent = "<html><body>Your order has been processed</body></html>";
MailMessage message = new MailMessage();
message.From = new MailAddress(senderEmail);
message.To.Add(new MailAddress(recipientEmail));
message.Subject = emailSubject;
message.Body = emailContent;
message.IsBodyHtml = true;
mailClient.Send(message);
When I check the received email in Outlook, it shows the Gmail address instead of my custom sender address. How can I make the custom from address appear correctly?
Gmail’s security policy causes this. When you authenticate with Gmail SMTP, their servers automatically replace the From header with your authenticated account address to stop email spoofing. I ran into this exact problem building a notification system for a client portal. The fix I used was setting up Gmail aliases through Google Workspace admin console or personal Gmail settings. Add your company domain as a verified sending alias - then Gmail will accept that address as legit. Or switch to dedicated email services like Amazon SES or Postmark. They don’t have this restriction and are built for transactional emails, so you get full control over sender addresses plus solid deliverability.
Gmail’s anti-spam protection is doing this - they override the from field when you authenticate through their SMTP to stop spoofing. Easy fix: add a display name like new MailAddress("[email protected]", "My Company Support"). Shows up as “My Company Support [email protected]” which looks way more professional than the raw Gmail address.
Gmail blocks sender spoofing by forcing your authenticated address as the From header when you use their SMTP servers. I ran into this same problem a few years back with an automated invoicing system. Here’s what worked for me: set a Reply-To header instead. Add message.ReplyToList.Add(new MailAddress("[email protected]")); to your code. The sender still shows your Gmail address, but replies come back to your company email. If you absolutely need your company email as the visible sender, try Gmail’s alias feature (if your domain allows it) or switch to SendGrid or Mailgun - they don’t have this restriction.