I do not want smtp solutions
I downloaded the APIs from Google's developers section so I will be able to access the Gmail. I managed to send a simple message and now I am trying to send an attachment. I sent a message with an image, but when I opened the image it said "Invalid image" and the file weights only 10KB, which means it didn't send the actual file. I tried many things that I found on the net but nothing worked.
That's the code. I think that it doesn't really get the file.
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;
}