First the code that I use to send an email by using the GMail API:
public class SendMail
{
public void Send()
{
var stream = new StringWriter();
stream.NewLine = "\r\n";
stream.WriteLine($"From:{_from}");
if (_to.Any()) stream.WriteLine($"To:{_to.ToCSV()}");
if (_cc.Any()) stream.WriteLine($"Cc:{_cc.ToCSV()}");
if (_bcc.Any()) stream.WriteLine($"Bcc:{_bcc.ToCSV()}");
stream.WriteLine($"Subject:{_subject}");
stream.WriteLine($"Reply-to:{_replyTo}");
stream.WriteLine($"Domain:{User.Domain}");
stream.WriteLine($"Content-type:text/html;charset=UTF-8");
stream.WriteLine($"Company:Knowledge and Technology Joyfully Engaged");
stream.WriteLine("");
stream.WriteLine($"{_body}");
stream.WriteLine("");
stream.WriteLine("With kind regards,<br />");
stream.WriteLine($"<a href=\"mailto:{User.Email}\">{User.DisplayName}</a>, {User.Occupations.Last()}<br />");
foreach (var org in User.Companies.Where(c => !string.IsNullOrWhiteSpace(c.Domain)))
{
stream.WriteLine($"<a href=\"https://www.{org.Domain}\">{org.Name}</a><br />");
}
var msg = stream.ToString();
string message = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(msg));
try
{
User.GMail.Users.Messages.Send(new Message { Raw = message, }, "me").Execute();
Console.WriteLine($"User {User.DisplayName} with subject {_subject} sent.");
Console.WriteLine($"{msg.Split("\r\n\r\n").First()}");
}
catch (Exception ex)
{
Console.WriteLine($"User {User.DisplayName} with subject {_subject} results in error {ex.Message} with these headers:");
Console.WriteLine($"{msg.Split("\r\n\r\n").First()}\r\n");
}
}
// More stuff, but boring...
The whole thing works fine and this is just an internal application. The line User.GMail.Users.Messages.Send(new Message { Raw = message, }, "me").Execute();
is where the actual magic basically happens for an authenticated user. The GMail property is of type GmailService
and the code works fine as it will send all emails for all users. This is part of a tool that has to send daily updates from every user to a bunch of other people.
But my problem is that the GMail API tends to ignore the "from" field for some users, while it accepts the set value for other users. I want this field to be "Display name [email protected]" but for several users this doesn't happen. They are just "[email protected]" instead...
My workspace has 8 users and this happens with half the users. And while I'm typing this, I suddenly realize that it's because these four users haven't set an alias for the specific from-address that I'm using. So now I have to tell each user to just create an alias for their account.
Okay, solved. Too much text to discard so hopefully someone learns from my mistake...
The problem was that I have multiple domains. (example.com and example.org.) I'm sending emails from the .org domain, but all users are part of the .com domain. Half of them have an .org alias, the other half doesn't.
GMail checks if the "from" address is an alias or not. If not, it uses the original user account. If I use their main address then my from-field is okay with the display name. If they have an alias for the .org domain then it also goes well. But if they don't have the from-address as main address or alias then GMail will refuse it, and use their main address instead.
Well, nice to remember...