0

i'm working on a aplication for sending emails. I started working with libcurlnet library, and this is the code i got at the moment:

Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
Easy easyHandle=new Easy();
easyHandle.SetOpt(CURLoption.CURLOPT_URL, "smtp.gmail.com");

According to this example(smtp-mail), i could use CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT. Like this:

easyHandle.SetOpt(CURLoption.CURLOPT_MAIL_FROM, "abcd@gmail.com");

But it doesn't work. It does not contain a definition for CURLOPT_MAIL_FROM. Does anyone worked with C# and LibCurlNet library? Help me please.

2 Answers2

0

I think you'll find using MailKit far simpler to use than LibCurlNet. There are NuGet packages on nuget.org as well: http://www.nuget.org/packages/MailKit/

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • the problem is my boss really want to use libcurl to do that, so if i want to change i need to be sure that libcurl really doesn't work on c# for smtp protocol. – Emmanuel Tavares Mar 13 '14 at 22:22
  • It's not possible. You can see the code for LibCurlNet here: http://libcurl-net.cvs.sourceforge.net/viewvc/libcurl-net/libcurlnet/src/ - it has not been updated in 9 years and has no support for SMTP. – jstedfast Mar 13 '14 at 23:12
  • Sorry but i don't understand. Why you say it's not possible? You tried before? Because the link you sent, there is nothing about "Don't support SMTP". – Emmanuel Tavares Mar 14 '14 at 09:42
  • libcurl, according to their [release notes](http://curl.haxx.se/changes.html) did not add SMTP support until v7.20 released in Feb 2010. LibCurlNet hasn't been updated since 2004. It seems obvious that LibCurlNet does not support SMTP. – jstedfast Mar 14 '14 at 16:33
  • FWIW, their API docs specify the first version that contain each API as well: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTMAILRCPT – jstedfast Mar 14 '14 at 16:38
  • thank you. I think i'll work with the system.net.mail library then. – Emmanuel Tavares Mar 16 '14 at 23:55
0

You can use CurlSharp. CurlSharp is the successor to LibCurlNet. It is actively maintained and supports the latest libcurl version.

SMTP client in CurlSharp:

using System;
using System.Runtime.InteropServices;
using CurlSharp;

namespace SmtpMail
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct UploadContext
    {
        public int LinesRead;
    }

    internal class SmtpMail
    {
        private static void Main(string[] args)
        {
            try
            {
                Curl.GlobalInit(CurlInitFlag.All);

                using (var curl = new CurlEasy())
                {
                    curl.Url = "smtp://localhost:25";
                    curl.Upload = true;

                    curl.ReadFunction = PayloadSource;
                    curl.ReadData = new UploadContext();
                    curl.SetOpt(CurlOption.MailFrom, "<from@example.com>");
                    using (var recipients = new CurlSlist())
                    {
                        recipients.Append("<to@example.net>");
                        recipients.Append("<cc@example.org>");
                        var s = recipients.Strings;
                        curl.SetOpt(CurlOption.MailRcpt, recipients.Handle);

                        curl.Perform();
                    }
                }

                Curl.GlobalCleanup();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private static byte[] GetBytes(string str)
        {
            var bytes = new byte[str.Length*sizeof (char)];
            Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }

        private static int PayloadSource(byte[] buf, int size, int nmemb, object extradata)
        {
            var payloadText = new[]
                              {
                                  "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
                                  "To: <to@example.net>\r\n",
                                  "From: <from@example.org> (Example User)\r\n",
                                  "Cc: <cc@example.org> (Another example User)\r\n",
                                  "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n",
                                  "Subject: SMTP example message\r\n",
                                  "\r\n", /* empty line to divide headers from body, see RFC5322 */
                                  "The body of the message starts here.\r\n",
                                  "\r\n",
                                  "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
                                  "Check RFC5322.\r\n"
                              };

            var ctxUpload = (UploadContext) extradata;

            if ((ctxUpload.LinesRead >= 0) &&
                (ctxUpload.LinesRead < payloadText.Length) &&
                (size != 0) && (nmemb != 0) &&
                ((size*nmemb) > 0))
            {
                var line = payloadText[ctxUpload.LinesRead++];
                var lineBuf = GetBytes(line);
                Buffer.BlockCopy(lineBuf, 0, buf, 0, lineBuf.Length);
                return lineBuf.Length;
            }
            return 0;
        }
    }
}
masroore
  • 9,668
  • 3
  • 23
  • 28