我不想要smtp解决方案
我从Google的开发者部分下载了API,因此我可以访问Gmail。我设法发送了一条简单的消息,现在我正在尝试发送附件。我发送了一张带有图片的信息,但是当我打开图片时它说“#34;图片无效"并且文件权重仅为10KB,这意味着它没有发送实际文件。我尝试了很多我在网上找到的东西,但没有任何效果。
代码。我认为它并没有真正得到这个文件。
public static void SendMailWithFile(string email, UserCredential credential, string fileName)
{
var messege = new AE.Net.Mail.MailMessage
{
Subject = "Test mail",
Body = "This test message was sent from a C# app..",
From = new MailAddress("email@gmail.com")
};
messege.To.Add(new MailAddress(email));
messege.ReplyTo.Add(messege.From);
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
AE.Net.Mail.Attachment file = new AE.Net.Mail.Attachment(GetMimeType(fileName), fileName, fileName, true);
messege.Attachments.Add(file);
var msgStr = new StringWriter();
messege.Save(msgStr);
var gmail = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "EmailTest"
});
try
{
//AE.Net.Mail.Attachment att = new AE.Net.Mail.Attachment();
var result = gmail.Users.Messages.Send(new Message
{
Raw = Base64UrlEncode(msgStr.ToString())
}, "me").Execute();
Console.WriteLine("Message ID {0} sent.", result.Id);
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
}
private static string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}