Oscail Leabharlann Foinse .NET do IMAP, POP3 & SMTP
Leabharlann C# .NET in aisce chun Teachtaireacht a Ghiniúint le Ceangaltáin, Criptigh/Díchriptigh Teachtaireachtaí le PGP/MIME.
Leabharlann Foinse Oscailte .NET é MailKit do IMAP, POP3, agus SMTP. Is leabharlann cliant ríomhphoist tras-ardán é a tógadh ar bharr MimeKit. Tá sé mar aidhm ag an tionscadal feidhmiúcháin cliant SMTP, POP3, agus IMAP láidir, lán-léirithe agus RFC-chomhlíontach a sholáthar.
Tacaíonn an API le roinnt gnéithe tábhachtacha a bhaineann le Fíordheimhniú SASL, tacaíocht seachfhreastalaí, cliant SMTP, cliant POP3, cliant IMAP4, sórtáil taobh an chliaint, agus snáithiú teachtaireachtaí.
Tús a chur le MailKit
Is é an bealach is éasca MailKit a shuiteáil trí NuGet. Chun é a úsáid ó Chonsól Bainisteoir Pacáiste Visual Studio, cuir isteach an t-ordú seo a leanas.
shuiteáil Mailkit trí NuGet
Install-Package MailKit
Suiteáil Mailkit trí GitHub
git clone --recursive https://github.com/jstedfast/MailKit.git
Cruthaigh Teachtaireachtaí Nua trí .NET
Cuireann leabharlann MailKit API Foinse Oscailte ar chumas forbróirí bogearraí teachtaireachtaí MIME a chruthú le roinnt orduithe simplí. Is éard atá i TextPart ná cuid nód MIME le cineál meán téacs. Sonraíonn an chéad argóint le cruthaitheoir TextPart an fochineál meán, sa chás seo, simplí. Fochineál meáin eile a bhfuil tú eolach air is dócha ná an fochineál HTML. Is é an bealach is éasca chun ábhar teaghrán na coda MIME a fháil agus a shocrú araon ná an t-airí Téacs.
Cuireann leabharlann MailKit API Foinse Oscailte ar chumas forbróirí bogearraí teachtaireachtaí MIME a chruthú gan mórán orduithe simplí. Is éard atá i TextPart ná cuid duille nód MIME le cineál meán téacs. Sonraíonn an chéad argóint le cruthaitheoir TextPart an fochineál meán, sa chás seo, simplí. Fochineál meáin eile a bhfuil tú eolach air is dócha ná an fochineál HTML. Is é an bealach is éasca chun ábhar teaghrán na coda MIME a fháil agus a shocrú ná an t-airí Téacs.
Gin agus Seol Teachtaireacht Saor in Aisce ag úsáid C #
var message = new MimeMessage();
message.From.Add(new MailboxAddress("fred", "This email address is being protected from spam-bots. You need JavaScript enabled to view it."));
message.To.Add(new MailboxAddress("frans", "This email address is being protected from spam-bots. You need JavaScript enabled to view it."));
message.Subject = "FileFormat ";
message.Body = new TextPart("plain")
{
Text = "File Format Developer Guide"
};
using (var client = new SmtpClient())
{
// For demo-purposes,
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("smtp.test.com", 587, false);
// Note: only needed if the SMTP server requires authentication
client.Authenticate("frans", "password");
client.Send(message);
client.Disconnect(true);
}
Gin Teachtaireacht le Ceangaltáin ag baint úsáide as .NET API
Soláthraíonn MailKit API gnéithe chun teachtaireacht a ghiniúint le ceangaltáin taobh istigh d'fheidhmchláir .NET. Tá ceangaltáin díreach cosúil le haon MimePart eile; is í an phríomhdhifríocht ná go bhfuil luach sealúchais ceanntásc inneachair-diúscartha den cheangaltán iontu in ionad ceanntásc inlíne nó gan aon cheanntásc Ábhar-Diúscairt ar chor ar bith. Chun téacs/HTML agus téacs/leagan simplí den teachtaireacht a sheoladh amach, ní mór duit TextPart a chruthú do gach cuid agus ansin iad a chur le hilpháirt nó rogha eile.
Cruthaigh Teachtaireacht le Ceangaltáin trí C#
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
message.Subject = "How you doin?";
// create our message text, just like before (except don't set it as the message.Body)
var body = new TextPart ("plain") {
Text = @"Hey Alice,
What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.
Will you be my +1?
-- Joey
"
};
// create an image attachment for the file located at path
var attachment = new MimePart ("image", "gif") {
Content = new MimeContent (File.OpenRead (path), ContentEncoding.Default),
ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = Path.GetFileName (path)
};
// now create the multipart/mixed container to hold the message text and the
// image attachment
var multipart = new Multipart ("mixed");
multipart.Add (body);
multipart.Add (attachment);
// now set the multipart/mixed as the message body
message.Body = multipart;
Criptigh/Díchriptigh Teachtaireachtaí le PGP/MIME
Soláthraíonn leabharlann MailKit gnéithe chun teachtaireachtaí ríomhphoist a chriptiú le feidhmchláir PGP/MIME taobh istigh de .NET. Úsáideann an PGP/MIME cuid MIME le cineál míme ilpháirteach/criptithe chun sonraí criptithe a chuimsiú. Más mian leat teachtaireacht a chriptiú, is cur chuige níos fearr i gcónaí SecureMailboxAddress a úsáid in ionad MailboxAddress do gach faighteoir, rud a ligfidh d’úsáideoirí méarloirg uathúil eochair PGP gach faighteora a shonrú.
Teachtaireachtaí á chriptiú le PGP/MIME trí C#
public void Encrypt (MimeMessage message)
{
// encrypt our message body using our custom GnuPG cryptography context
using (var ctx = new MyGnuPGContext ()) {
// Note: this assumes that each of the recipients has a PGP key associated
// with their email address in the user's public keyring.
//
// If this is not the case, you can use SecureMailboxAddresses instead of
// normal MailboxAddresses which would allow you to specify the fingerprint
// of their PGP keys. You could also choose to use one of the Encrypt()
// overloads that take a list of PgpPublicKeys.
message.Body = MultipartEncrypted.Encrypt (ctx, message.To.Mailboxes, message.Body);
}
}
Teachtaireachtaí PGP/MIME a dhíchriptiú
public MimeEntity Decrypt (MimeMessage message)
{
if (message.Body is MultipartEncrypted) {
// the top-level MIME part of the message is encrypted using PGP/MIME
var encrypted = (MultipartEncrypted) entity;
return encrypted.Decrypt ();
} else {
// the top-level MIME part is not encrypted
return message.Body;
}
}