简体   繁体   中英

Send animated GIF via e-mail in C#

I'm trying to send an animated GIF via e-mail using a Gmail account. I can send normal picture with this code:

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("my adress", "Kinomaton");
mail.To.Add(richTextBox1.Text);
mail.Subject = Kinomaton.Properties.Settings.Default.objet;
mail.Body = Kinomaton.Properties.Settings.Default.mail;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(@"F:\gif_temp\gif" + (Kinomaton.Properties.Settings.Default.folder - 1) + "\\gif_final3.jpg");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("my adress", "my password");
SmtpServer.EnableSsl = true;
SmtpServer.Timeout = 500000000;
SmtpServer.Send(mail);

The problem is that when I try to replace the .jpg file by a .gif file, it doesn't work. Anyone can help me please ?

Step 1: Upload the gif to imgur

Step 2: Send an email containing the link to your gif

Step 3: Party

This will ensure that the recipient can actually view the gif. As mentioned in the comments, alot of email clients wont display gifs anyways.

*Edit* To upload the file via code:

using (var w = new WebClient())
{
    var values = new NameValueCollection
    {
        { "key", "<api-key>" },
        { "image", Convert.ToBase64String(File.ReadAllBytes(@"<file>")) }
    };

    byte[] response = w.UploadValues("http://imgur.com/api/upload.xml", values);

    Console.WriteLine(XDocument.Load(new MemoryStream(response)));
}

The output will tell you what the link of the file is on Imgur so you can send it to whoever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM