1

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;
    }
kenorb
  • 118,428
  • 63
  • 588
  • 624
  • take a look here - http://stackoverflow.com/questions/2825950/sending-email-with-attachments-from-c-attachments-arrive-as-part-1-2-in-thunde if this does not fit then do a simple google search `Adding Attachments to smtp email` – MethodMan Feb 26 '15 at 21:05
  • These are two different kinds of attachments and the one I am using doesn't have the `ContentDisposition` function. And it is impossible to convert them. Also I prefer using Google's API instead. – user3144416 Feb 26 '15 at 21:23

0 Answers0