Posts Tagged ‘SMTP’

The handshake failed due to an unexpected packet format

Monday, July 26th, 2010

Most likely your server requires implicit SSL. So first try to connect without SSL:

// C#

client.Connect("imap.example.com");
' VB.NET

client.Connect("imap.example.com");

Then, before logging-in, start implicit SSL negotiation. The command name differs for different protocols:

In case of IMAP:

// C#

client.StartTLS();
' VB.NET

client.StartTLS()

In case of POP3:

// C#

client.STLS();
' VB.NET

client.STLS()

In case of SMTP:

// C#

client.StartTLS();
' VB.NET

client.StartTLS()

Now, your connection is secured.

Remember that you can ignore SSL certificate errors using ServerCertificateValidate event:

// C#

client.ServerCertificateValidate +=
    (sender, e) => { e.IsValid = true; };
' VB.NET

client.ServerCertificateValidate += Function(sender, e) Do
	e.IsValid = True
End Function

Sending email with embedded image

Monday, December 21st, 2009

We’ll use Mail.dll email client.

  1. We need to create an email message
  2. Use src=”cid:imageID” as image source in HTML
  3. Add the image to Visuals collection using AddVisual method
  4. Apply image id by setting ContentId
  5. And finally send the message
// Use builder class to create an email
MailBuilder builder = new MailBuilder();

// Set From, To
builder.From.Add(new MailBox("alice@mail.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
builder.Subject = "Test";

// Set HTML content (Notice the src="cid:..." attribute)
builder.SetHtmlData(@"<html><body><img src=""cid:image1"" /></body></html>");

// SetHtmlData automatically extracts plaint text from html so,
// unless you want to specify plain text explicitly, you don't need to use this method:
builder.SetTextData("This is text version of th message.");

// Add image and set its Content-Id
MimeData visual = builder.AddVisual(@"c:\image.jpg");
visual.ContentType = new ContentType(MimeType.Image, MimeSubtype.Jpeg);
visual.ContentId = "image1";

IMail email = builder.Create();

// Send the message
using (Smtp smtp = new Smtp())
{
    smtp.Connect("mail.host.com");
    smtp.Ehlo(HeloType.EhloHelo, "yourname");
    smtp.Login("user", "password");
    smtp.SendMessage(email);
    smtp.Close(false);
}

And of course the VB.NET version of the same code:

' Use builder class to create an email
Dim builder As New MailBuilder()

' Set From, To
builder.From.Add(New MailBox("alice@mail.com", "Alice"))
builder.To.Add(New MailBox("bob@mail.com", "Bob"))
builder.Subject = "Test"

' Set HTML content (Notice the src="cid:..." attribute)
builder.SetHtmlData("<html><body><img src=""cid:image1"" /></body></html>")

' SetHtmlData automatically extracts plaint text from html so,
' unless you want to specify plain text explicitly, you don't need to use this method:
builder.SetTextData("This is text version of th message.")

' Add image and set its Content-Id
Dim visual As MimeData = builder.AddVisual("c:\image.jpg")
visual.ContentType = New ContentType(MimeType.Image, MimeSubtype.Jpeg)
visual.ContentId = "image1"

Dim email As IMail = builder.Create()

' Send the message
Using smtp As New Smtp()
	smtp.Connect("mail.host.com")
	smtp.Ehlo(HeloType.EhloHelo, "yourname")
	smtp.Login("user", "password")
	smtp.SendMessage(email)
	smtp.Close(False)
End Using

Same code using fluent interface:

Mail.Html(@"<html><body><img src=""cid:image1"" /></body></html>")
        .Text("This is text version of th message.")
        .From(new MailBox("alice@mail.com", "Alice"))
        .To(new MailBox("bob@mail.com", "Bob"))
        .Subject("Test")
        .AddVisual(@"c:\image.jpg")
        .SetContentId("image1")
        .SetContentType(
                new ContentType(MimeType.Image, MimeSubtype.Jpeg))
        .UsingNewSmtp()
        .WithCredentials("lesnikowski","password")
        .Server("mail.host.com")
        .Send();

And of course the VB.NET version using fluent interface:

Mail.Html("<html><body><img src=""cid:image1"" /></body></html>") _
	.Text("This is text version of th message.") _
	.From(New MailBox("alice@mail.com", "Alice")) _
	.To(New MailBox("bob@mail.com", "Bob")) _
	.Subject("Test").AddVisual("c:\image.jpg") _
	.SetContentId("image1") _
	.SetContentType( _
                New ContentType(MimeType.Image, MimeSubtype.Jpeg)) _
	.UsingNewSmtp() _
	.WithCredentials("lesnikowski", "password") _
	.Server("mail.host.com") _
	.Send()

You can download Mail.dll email client here.