2014-03-13 184 views
0

我正在處理髮送電子郵件的應用程序。我開始與libcurlnet庫的工作,這是我得到的時刻代碼:LibCurlNet支持SMTP協議?

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

根據這個例子(smtp-mail),我可以使用CURLOPT_MAIL_FROM和CURLOPT_MAIL_RCPT。像這樣:

easyHandle.SetOpt(CURLoption.CURLOPT_MAIL_FROM, "[email protected]"); 

但它不起作用。它不包含CURLOPT_MAIL_FROM的定義。 有人使用C#和LibCurlNet庫嗎? 請幫助我。

回答

0

我想你會發現使用MailKit比LibCurlNet更簡單。 nuget.org上也有NuGet軟件包:http://www.nuget.org/packages/MailKit/

+0

問題是我的老闆真的想使用的libcurl要做到這一點,所以如果我想改變,我需要確保的libcurl真的沒有在C#的工作smtp協議。 –

+0

這是不可能的。您可以在這裏看到LibCurlNet的代碼:http://libcurl-net.cvs.sourceforge.net/viewvc/libcurl-net/libcurlnet/src/ - 它在9年內沒有更新,並且不支持SMTP。 – jstedfast

+0

對不起,但我不明白。爲什麼你說這是不可能的?你之前嘗試過?由於您發送的鏈接沒有關於「不支持SMTP」的內容。 –

0

您可以使用CurlSharp。 CurlSharp是LibCurlNet的後繼者。它被積極維護並支持最新的libcurl版本。在CurlSharp

SMTP客戶端:

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, "<[email protected]>"); 
        using (var recipients = new CurlSlist()) 
        { 
         recipients.Append("<[email protected]>"); 
         recipients.Append("<[email protected]>"); 
         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: <[email protected]>\r\n", 
            "From: <[email protected]> (Example User)\r\n", 
            "Cc: <[email protected]> (Another example User)\r\n", 
            "Message-ID: <[email protected]>\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; 
     } 
    } 
}