您可以使用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;
}
}
}
問題是我的老闆真的想使用的libcurl要做到這一點,所以如果我想改變,我需要確保的libcurl真的沒有在C#的工作smtp協議。 –
這是不可能的。您可以在這裏看到LibCurlNet的代碼:http://libcurl-net.cvs.sourceforge.net/viewvc/libcurl-net/libcurlnet/src/ - 它在9年內沒有更新,並且不支持SMTP。 – jstedfast
對不起,但我不明白。爲什麼你說這是不可能的?你之前嘗試過?由於您發送的鏈接沒有關於「不支持SMTP」的內容。 –