Uploading emails using IMAP
Uploading emails to the IMAP server is fairly easy with Mail.dll IMAP client
First we’ll use Mail.dll to upload an existing email in eml format to the IMAP server:
// C# code
using (Imap imap = new Imap())
{
imap.Connect("server");
imap.Login("user", "password");
string eml = File.ReadAllText("email.eml", Encoding.GetEncoding(1252));
// The name of the folder depends on your IMAP server
imap.UploadMessage("[Gmail]/Sent Mail", eml);
imap.Close();
}
' VB.NET code
Using imap As New Imap()
imap.Connect("server")
imap.Login("user", "password")
Dim eml As String = File.ReadAllText("email.eml", Encoding.GetEncoding(1252))
' The name of the folder depends on your IMAP server
imap.UploadMessage("[Gmail]/Sent Mail", eml)
imap.Close()
End Using
Second sample shows how to create new email message and upload it to IMAP server.
// C# code
using (Imap imap = new Imap())
{
imap.Connect("server");
imap.Login("user", "password");
// Create new mail message
MailBuilder builder = new MailBuilder();
builder.Subject = "subject";
builder.From.Add(new MailBox("alice@email.com", "Alice"));
builder.To.Add(new MailBox("bob@email.com", "Bob"));
builder.Text = "This is plain text email";
// Upload
// The name of the folder depends on your IMAP server
imap.UploadMessage("[Gmail]/Sent Mail", builder.Create());
imap.Close();
}
' VB.NET code
Using imap As New Imap()
imap.Connect("server")
imap.Login("user", "password")
' Create new mail message
Dim builder As New MailBuilder()
builder.Subject = "subject"
builder.From.Add(New MailBox("alice@email.com", "Alice"))
builder.[To].Add(New MailBox("bob@email.com", "Bob"))
builder.Text = "This is plain text email"
' Upload
' The name of the folder depends on your IMAP server
imap.UploadMessage("[Gmail]/Sent Mail", builder.Create())
imap.Close()
End Using
Please note that only few IMAP servers are going to send the message to the actual recipients. Most servers will only upload the message without sending it.
You should use SMTP protocol for this.
You can download Mail.dll IMAP client here.

May 10th, 2011 at 13:02
Hello,
I try to submit an email, through this method, with many attachments (total size approx. 19MB) and I jump an error: “Unable to write data to the transport connection: An error occurred during connection attempt because the connected party did not properly respond after a period of time, or failed to established connection and connected host has failed to respond. ”
You know that it can be? The mail then you are on gmail.
Thanks
May 10th, 2011 at 15:07
@Roberto,
1.
Uploading message to IMAP server is a different thing than sending email.
You should use SMTP server if you want to send an email.
2.
I don’t know how your code looks like, you might have simply reached Gmail’s 25MB message limit.
(Typically, encoding makes the size of the files grow slightly 19 * 1,33 = 25,27)
3.
Could you please contact us directly, and provide a bit more info (code, stack trace, log)
[Edit]
In fact I was able to upload 10MB, 15MB, 18 MB and 19MB messages.
With 19MB Gmail randomly disconnected without any error message.
Seems it’s their bug.
December 12th, 2011 at 10:32
Hello,
vb.net 2010
I want to upload an email using imap but mark it as unread
imap.UploadMessage("Inbox", builder2.Create())works as described
BUT
Dim messageinfo As New Lesnikowski.Client.IMAP.UploadMessageInfo messageinfo.Flags.Add(Lesnikowski.Client.IMAP.Flag.Unseen) imap.UploadMessage("Inbox", builder2.Create(),messageinfo)gives the error:
Expected more data request, but received: e9f70402ea13473e BAD APPEND
Any help would be appreciated – thanks
December 12th, 2011 at 15:12
@Daryl
Most likely Unseen flag can not by used in this way by your IMAP server.
Some server accepts negative flags in this context (hMailServer) others don’t (Gmail).
Please simply remove messageinfo.Flags.Add(Lesnikowski.Client.IMAP.Flag.Unseen) line:
It should work as expected.