Some SMTP servers automatically put sent messages in the sent folder, but this is not a SMTP protocol requirement and many servers don’t do that.
In such case, you’ll need to manually upload message to the sent folder using IMAP. Unfortunately there is no standard that would allow checking, if SMTP server puts send messages in Sent folder.
Create email message
First we’ll create new email message:
// C#
MailBuilder builder = new MailBuilder();
builder.Subject = @"Subject";
builder.Html = @"Html with an image: <img src=""cid:lena"" />";
builder.From.Add(new MailBox("alice@mail.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
MimeData visual = builder.AddVisual(@"c:\lena.jpeg");
visual.ContentId = "lena";
MimeData attachment = builder.AddAttachment(@"c:\tmp.doc");
attachment.FileName = "document.doc";
IMail email = builder.Create();
' VB.NET
Dim builder As MailBuilder = New MailBuilder()
builder.Subject = "Subject"
builder.Html = "Html with an image: <img src=""cid:lena"" />"
builder.From.Add(New MailBox("alice@mail.com", "Alice"))
builder.[To].Add(New MailBox("bob@mail.com", "Bob"))
Dim visual As MimeData = builder.AddVisual("c:\lena.jpeg")
visual.ContentId = "lena"
Dim attachment As MimeData = builder.AddAttachment("c:\tmp.doc")
attachment.FileName = "document.doc"
Dim email As IMail = builder.Create()
Alternatively you can use fluent interface to create an email:
// C#
IMail email = Limilabs.Mail.Fluent.Mail
.Html(@"Html with an image: <img src=""cid:lena"" />")
.AddVisual(@"c:\lena.jpeg").SetContentId("lena")
.AddAttachment(@"c:\tmp.doc").SetFileName("document.doc")
.To("to@example.com")
.From("from@example.com")
.Subject("Subject")
.Create();
' VB.NET
Dim email As IMail = Mail _
.Html("Html with an image: <img src=""cid:lena"" />") _
.AddVisual("C:\lena.jpeg").SetContentId("lena") _
.AddAttachment("C:\tmp.doc").SetFileName("document.doc") _
.To("to@example.com") _
.From("from@example.com") _
.Subject("Subject") _
.Create()
Send email message
Then we’ll send it using SMTP protocol:
// C#
ISendMessageResult result;
using (Smtp smtp = new Smtp())
{
smtp.Connect("smtp.server.com"); // or ConnectSSL for SSL
smtp.UseBestLogin("user", "password");
result = smtp.SendMessage(email);
smtp.Close();
}
' VB.NET
Dim result As ISendMessageResult
Using smtp As New Smtp()
smtp.Connect("smtp.server.com") ' or ConnectSSL for SSL
smtp.UseBestLogin("user", "password")
result = smtp.SendMessage(email)
smtp.Close()
End Using
Upload email message
Finally we’ll connect to IMAP server, get Sent folder and upload message to it:
// C#
if (result.Status == SendMessageStatus.Success)
{
using (Imap imap = new Imap())
{
imap.Connect("imap.server.com"); // or ConnectSSL for SSL
imap.UseBestLogin("user", "password");
FolderInfo sent = new CommonFolders(imap.GetFolders()).Sent;
imap.UploadMessage(sent, email);
imap.Close();
}
}
' VB.NET
If result.Status = SendMessageStatus.Success Then
Using imap As New Imap()
imap.Connect("imap.server.com") ' or ConnectSSL for SSL
imap.UseBestLogin("user", "password")
Dim sent As FolderInfo = New CommonFolders(imap.GetFolders()).Sent
imap.UploadMessage(sent, email)
imap.Close()
End Using
End If
Entire code
Here’s the full code:
// C#
MailBuilder builder = new MailBuilder();
builder.Subject = @"Subject";
builder.Html = @"Html with an image: <img src=""cid:lena"" />";
builder.From.Add(new MailBox("alice@mail.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
MimeData visual = builder.AddVisual(@"c:\lena.jpeg");
visual.ContentId = "lena";
MimeData attachment = builder.AddAttachment(@"c:\tmp.doc");
attachment.FileName = "document.doc";
IMail email = builder.Create();
ISendMessageResult result;
using (Smtp smtp = new Smtp())
{
smtp.Connect("smtp.server.com"); // or ConnectSSL for SSL
smtp.UseBestLogin("user", "password");
result = smtp.SendMessage(email);
smtp.Close();
}
if (result.Status == SendMessageStatus.Success)
{
using (Imap imap = new Imap())
{
imap.Connect("imap.server.com"); // or ConnectSSL for SSL
imap.UseBestLogin("user", "password");
FolderInfo sent = new CommonFolders(imap.GetFolders()).Sent;
imap.UploadMessage(sent, email);
imap.Close();
}
}
' VB.NET
Dim builder As MailBuilder = New MailBuilder()
builder.Subject = "Subject"
builder.Html = "Html with an image: <img src=""cid:lena"" />"
builder.From.Add(New MailBox("alice@mail.com", "Alice"))
builder.[To].Add(New MailBox("bob@mail.com", "Bob"))
Dim visual As MimeData = builder.AddVisual("c:\lena.jpeg")
visual.ContentId = "lena"
Dim attachment As MimeData = builder.AddAttachment("c:\tmp.doc")
attachment.FileName = "document.doc"
Dim email As IMail = builder.Create()
Dim result As ISendMessageResult
Using smtp As New Smtp()
smtp.Connect("smtp.server.com") ' or ConnectSSL for SSL
smtp.UseBestLogin("user", "password")
result = smtp.SendMessage(email)
smtp.Close()
End Using
If result.Status = SendMessageStatus.Success Then
Using imap As New Imap()
imap.Connect("imap.server.com") ' or ConnectSSL for SSL
imap.UseBestLogin("user", "password")
Dim sent As FolderInfo = New CommonFolders(imap.GetFolders()).Sent
imap.UploadMessage(sent, email)
imap.Close()
End Using
End If