When I take the Raw produced from encode(below) and put it in https://developers.google.com/apis-explorer/?hl=en_GB#p/gmail/v1/gmail.users.messages.send it works fine with main email but not with delegate.
is there a way to map delegate user/service account to a gmail for sending emails?
public static bool SendEmail(string to, string subject, string body)
{
var mailService = AuthenticateServiceAccount(serviceAccountEmail, path, Scopes);
var mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From = new System.Net.Mail.MailAddress(cID);
mailMessage.To.Add(to);
mailMessage.ReplyToList.Add(cID);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
var mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mailMessage);
var gmailMessage = new Message
{
Raw = Encode(mimeMessage.ToString())
};
Send(mailService, cID, gmailMessage);
return true;
}
public static string Encode(string text)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);
return System.Convert.ToBase64String(bytes)
.Replace('+', '-')
.Replace('/', '_')
.Replace("=", "");
}
public static Message Send(GmailService service, string userId, Message body)
{
try
{
// Initial validation.
if (service == null)
throw new ArgumentNullException("service");
if (body == null)
throw new ArgumentNullException("body");
if (userId == null)
throw new ArgumentNullException(userId);
// Make the request.
return service.Users.Messages.Send(body, userId).Execute();
}
catch (Exception ex)
{
throw new Exception("Request Send failed.", ex);
}
}