Send raw data (*.eml file)

This post describes how to send raw eml email data as email message using SMTP server.

In most cases you don’t need to send raw eml data.
Take a look at those topics, they may be better describing your needs:

*.eml extension is a standard extension used for storing email files. Eml file contains raw data received from POP3 or IMAP server. You can use GetMessageByUID on Pop3 or Imap class to obtain it. It can be also created using IMail.Render method:

MailBuilder builder = new MailBuilder();
builder.Subject = "Test";
builder.Text = "This is plain text message.";
builder.From.Add(new MailBox("alice@mail.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
IMail email = builder.Create();
byte[] eml = email.Render();

Eml file includes all attachments, visual elements, email body in text, HTML (if defined) formats and email headers (such as: from, to, subject, date and so on).

When sending email message using SMTP protocol you must fill three SmtpMail class properties:

  • From – FROM command dialogue
  • To – RCPT TO command in SMTP dialogue
  • RawEmlData – DATA command in SMTP dialogue

As you can see both ‘from address’ and ‘recipient’ are duplicated – they also appear in eml data as email From and To headers. SmtpMail has helper methods, such as CreateFromEmlFile or CreateFromEml that parse eml data and fill all properties required for SMTP communication.

// C# version

using Limilabs.Client.SMTP;

class Program
{
    static void Main(string[] args)
    {
        using (Smtp smtp = new Smtp())
        {
            smtp.Connect("server.company.com");
            smtp.UseBestLogin("user", "password");

            SmtpMail smtpMail = SmtpMail.CreateFromEmlFile(@"c:\email.eml");

            smtp.SendMessage(smtpMail);
            smtp.Close();
        }
    }
};
' VB.NET version

Imports Limilabs.Client.SMTP

Public Module Module1
    Public Sub Main(ByVal args As String())
        Using smtp As New Smtp()
            smtp.Connect("server.company.com")
            smtp.UseBestLogin("user", "password")

            Dim smtpMail As SmtpMail = smtpMail.CreateFromEmlFile("c:\email.eml")

            smtp.SendMessage(SmtpMail)
            smtp.Close()
        End Using
    End Sub
End Module

Tags: , , ,

2 Responses to “Send raw data (*.eml file)”

  1. Rex Says:

    I am using the trial version to see whether it fits our needs, but got two issues here:
    1. how to add CC/BCC after I load an eml file?
    2. the email at recipients does not show “To” address, which I do need it.

  2. Limilabs support Says:

    1. how to add CC/BCC after I load an eml file?

    Use To, Cc and Bcc properties on IMail object instance.

    You can also use IMail.ToBuilder method – it creates MailBuilder instance, which you can use to modify an email.

    2. the email at recipients does not show “To” address, which I do need it.

    I’m not sure what you mean, but if you want to access email address and name of one of the recipients in To collection, please read this article: How to access To, Cc, Bcc

Questions?

Consider using our Q&A forum for asking any questions.