0

I've been struggling with an unexpected behaviour from Gmail API.

Until now, I get the body of every mail using mail.Payload.Parts[0].Body.Data, which contains the message body in base64. In this case, the MimeType of the part is text/plain.

The thing is that when the mail I'm retrieving has an attachment, parts[0] MimeType is "multipart/alternative", and its body has only null fields.

How am I supposed to get the mail body if it has attachments?

Thanks!

  • Maybe [**this answer**](https://stackoverflow.com/questions/32655874/cannot-get-the-body-of-email-with-gmail-php-api/32660892#32660892) can be of some help. – Tholle Nov 23 '17 at 13:02

1 Answers1

0

You can try the solution in this site.

C# simplifies network programming in .Net framework. C# describes various protocols using communication programming like Socket communications , SMTP mail , UDP , URL etc. The System.Net classes uses to communicate with other applications by using the HTTP, TCP, UDP, Socket etc. In the previous program we saw how to SMTP email from C# describes how to send an email with text body . Here we are sending an email with an attachment.

Here is the sample code provided,

using System;
using System.Windows.Forms;
using System.Net.Mail;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("your_email_address@gmail.com");
                mail.To.Add("to_address");
                mail.Subject = "Test Mail - 1";
                mail.Body = "mail with attachment";

                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment("your attachment file");
                mail.Attachments.Add(attachment);

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

You can also refer to the official .NET Quickstart from the documentation, here the in-depth discussion about the api was introduced together with sample code and step by step set-up process.

For further reference, you can visit this SO post.

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65