2014-07-14 98 views
6

我正在使用正在使用的应用程序中的Gmail API。但是,我不确定如何将其Java或Python示例更改为C#。现有样本如何改变?在C#中为Gmail API创建消息

Sample found here.

+2

Java是如此接近C#,你一定试图转换它,并失败了一些如何?也许发布你试图转换的内容对于人们来说是个很好的开始,帮助你在错误的地方 – Prescott

+0

嗯,我正在寻找'MimeMessage','Properties'等的等价物,而且快速谷歌搜索没有帮助接着就,随即。 – muttley91

+0

Google API都是REST API。这些文档告诉你哪些参数需要进入REST API调用。 – Casey

回答

1

看起来这是不是真的使用Gmail API的问题。你应该寻找的是“C#创建电子邮件”(可能增加“RFC 2822”,因为这是定义这些电子邮件格式的RFC)。一旦你有了这样一个有效的“电子邮件信息”(真正的,可以用于SMTP或IMAP等的整个电子邮件内容),那么使用它与Gmail API应该是相当简单的。

+0

你说得对。我在Gmail API下发帖,认为有人可能以前遇到过这个问题并且有过答案,但我应该更直接指向C#。谢谢。 – muttley91

+0

@rar你有什么发现?试图解决同样的问题。 – pettys

+0

不幸的不是。我们不得不放弃Gmail API,因为它没有正常工作,并且我们没有足够的时间花在它上面,因为我们只能使用更简单的电子邮件发件人。谷歌真的可以改善他们的文档... – muttley91

8

这是一个棘手的问题要解决,但我最终得到它。我使用NuGet包AE.Net.Mail来获得RFC 2822位。

using System.IO; 
using System.Net.Mail; 
using Google.Apis.Gmail.v1; 
using Google.Apis.Gmail.v1.Data; 

public class TestEmail { 

    public void SendIt() { 
    var msg = new AE.Net.Mail.MailMessage { 
     Subject = "Your Subject", 
     Body = "Hello, World, from Gmail API!", 
     From = new MailAddress("[you]@gmail.com") 
    }; 
    msg.To.Add(new MailAddress("[email protected]")); 
    msg.ReplyTo.Add(msg.From); // Bounces without this!! 
    var msgStr = new StringWriter(); 
    msg.Save(msgStr); 

    var gmail = new GmailService(MyOwnGoogleOAuthInitializer); 
    var result = gmail.Users.Messages.Send(new Message { 
     Raw = Base64UrlEncode(msgStr.ToString()) 
    }, "me").Execute(); 
    Console.WriteLine("Message ID {0} sent.", result.Id); 
    } 

    private static string Base64UrlEncode(string input) { 
    var inputBytes = System.Text.Encoding.UTF8.GetBytes(input); 
    // Special "url-safe" base64 encode. 
    return Convert.ToBase64String(inputBytes) 
     .Replace('+', '-') 
     .Replace('/', '_') 
     .Replace("=", ""); 
    } 
} 

相同的代码一点点进一步分析原因,张贴在这里: http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/

+0

我试过使用你的代码,但是我得到错误错误CS0103:Context.GoogleOAuthInitializer上的当前上下文中不存在名称'Context'。你从哪里获得上下文? – Rossini

+0

@Rossini对不起,这是对我自己代码的引用,它返回一个Google.Apis.Services.BaseClientService.Initializer,它设置为将OAuth2承载令牌添加到每个请求。您可能只能使用默认的GmailService()构造函数 - 取决于您的Gmail服务如何进行身份验证。我会更新我的代码,以便更清楚地说明问题。 – pettys

+1

此代码适用于纯文本电子邮件。但不适用于HTML电子邮件+附件。 –

5

这是我能得到工作,使用MimeKit

public void SendEmail(MyInternalSystemEmailMessage email) 
{ 
    var mailMessage = new System.Net.Mail.MailMessage(); 
    mailMessage.From = new System.Net.Mail.MailAddress(email.FromAddress); 
    mailMessage.To.Add(email.ToRecipients); 
    mailMessage.ReplyToList.Add(email.FromAddress); 
    mailMessage.Subject = email.Subject; 
    mailMessage.Body = email.Body; 
    mailMessage.IsBodyHtml = email.IsHtml; 

    foreach (System.Net.Mail.Attachment attachment in email.Attachments) 
    { 
     mailMessage.Attachments.Add(attachment); 
    } 

    var mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mailMessage); 

    var gmailMessage = new Google.Apis.Gmail.v1.Data.Message { 
     Raw = Encode(mimeMessage.ToString()) 
    }; 

    Google.Apis.Gmail.v1.UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(gmailMessage, ServiceEmail); 

    request.Execute(); 
} 

public static string Encode(string text) 
{ 
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text); 

    return System.Convert.ToBase64String(bytes) 
     .Replace('+', '-') 
     .Replace('/', '_') 
     .Replace("=", ""); 
} 

@Eric Gmail API缺乏,因为它需要消费者必须经过几次舞蹈才能做一些相对基本的事情。对于.Net,它应该像填充Message对象(包括Subject,From,To,Body等)并将该对象传递给处理编码的Send函数一样简单。

Api文档至少应该提供一个完整的工作示例,不需要像MimeKit或AE.Net.Mail这样的第三方库。

注意:如果您收到电子邮件退回问题,可能是由于没有设置ReplyToList字段。见:GMail API Emails Bouncing