本文整理汇总了C#中MimeKit.MailboxAddress类的典型用法代码示例。如果您正苦于以下问题:C# MailboxAddress类的具体用法?C# MailboxAddress怎么用?C# MailboxAddress使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MailboxAddress类属于MimeKit命名空间,在下文中一共展示了MailboxAddress类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestEncodingMailboxWithArabicName
public void TestEncodingMailboxWithArabicName()
{
var mailbox = new MailboxAddress ("هل تتكلم اللغة الإنجليزية /العربية؟", "[email protected]");
var list = new InternetAddressList ();
list.Add (mailbox);
var expected = "=?utf-8?b?2YfZhCDYqtiq2YPZhNmFINin2YTZhNi62Kk=?=\n =?utf-8?b?INin2YTYpdmG2KzZhNmK2LLZitipIC/Yp9mE2LnYsdio2YrYqdif?=\n\t<[email protected]>";
var actual = list.ToString (UnixFormatOptions, true);
Assert.AreEqual (expected, actual, "Encoding arabic mailbox did not match expected result: {0}", expected);
Assert.IsTrue (InternetAddressList.TryParse (actual, out list), "Failed to parse arabic mailbox");
Assert.AreEqual (mailbox.Name, list[0].Name);
}
示例2: TestPgpMimeSigning
public void TestPgpMimeSigning ()
{
var self = new MailboxAddress ("MimeKit UnitTests", "[email protected]");
var cleartext = new TextPart ("plain");
cleartext.Text = "This is some cleartext that we'll end up signing...";
using (var ctx = new DummyOpenPgpContext ()) {
var multipart = MultipartSigned.Create (ctx, self, DigestAlgorithm.Sha1, cleartext);
Assert.AreEqual (2, multipart.Count, "The multipart/signed has an unexpected number of children.");
var protocol = multipart.ContentType.Parameters["protocol"];
Assert.AreEqual (ctx.SignatureProtocol, protocol, "The multipart/signed protocol does not match.");
Assert.IsInstanceOfType (typeof (TextPart), multipart[0], "The first child is not a text part.");
Assert.IsInstanceOfType (typeof (ApplicationPgpSignature), multipart[1], "The second child is not a detached signature.");
var signatures = multipart.Verify (ctx);
Assert.AreEqual (1, signatures.Count, "Verify returned an unexpected number of signatures.");
foreach (var signature in signatures) {
try {
bool valid = signature.Verify ();
Assert.IsTrue (valid, "Bad signature from {0}", signature.SignerCertificate.Email);
} catch (DigitalSignatureVerifyException ex) {
Assert.Fail ("Failed to verify signature: {0}", ex);
}
}
}
}
示例3: TestSimpleAddrSpec
public void TestSimpleAddrSpec ()
{
var expected = new InternetAddressList ();
var mailbox = new MailboxAddress ("", "");
InternetAddressList result;
string text;
expected.Add (mailbox);
text = "[email protected]";
mailbox.Address = "[email protected]lixcode.com";
Assert.IsTrue (InternetAddressList.TryParse (text, out result), "Failed to parse: {0}", text);
AssertInternetAddressListsEqual (text, expected, result);
result = InternetAddressList.Parse (text);
AssertInternetAddressListsEqual (text, expected, result);
text = "fejj";
mailbox.Address = "fejj";
Assert.IsTrue (InternetAddressList.TryParse (text, out result), "Failed to parse: {0}", text);
AssertInternetAddressListsEqual (text, expected, result);
result = InternetAddressList.Parse (text);
AssertInternetAddressListsEqual (text, expected, result);
}
示例4: SignEntity
/*
* tries to sign a MimeEntity
*/
public static MimeEntity SignEntity(MimeEntity entity, MailboxAddress signer)
{
using (WindowsSecureMimeContext ctx = new WindowsSecureMimeContext(sys.StoreLocation.CurrentUser))
{
return MultipartSigned.Create(ctx, signer, DigestAlgorithm.Sha1, entity);
}
}
示例5: TestDecodedMailboxHasCorrectCharsetEncoding
public void TestDecodedMailboxHasCorrectCharsetEncoding()
{
var latin1 = Encoding.GetEncoding ("iso-8859-1");
var mailbox = new MailboxAddress (latin1, "Kristoffer Brånemyr", "[email protected]");
var list = new InternetAddressList ();
list.Add (mailbox);
var encoded = list.ToString (UnixFormatOptions, true);
InternetAddressList parsed;
Assert.IsTrue (InternetAddressList.TryParse (encoded, out parsed), "Failed to parse address");
Assert.AreEqual (latin1.HeaderName, parsed[0].Encoding.HeaderName, "Parsed charset does not match");
}
示例6: CanSign
/*
* returns true if email address can sign email
*/
public static bool CanSign(string email)
{
try
{
MailboxAddress signer = new MailboxAddress("", email);
TextPart entity = new TextPart("text");
using (WindowsSecureMimeContext ctx = new WindowsSecureMimeContext(sys.StoreLocation.CurrentUser))
{
MultipartSigned.Create(ctx, signer, DigestAlgorithm.Sha1, entity);
return true;
}
}
catch
{
return false;
}
}
示例7: TestArgumentExceptions
public void TestArgumentExceptions ()
{
var mailbox = new MailboxAddress ("MimeKit Unit Tests", "[email protected]");
var list = new InternetAddressList ();
list.Add (new MailboxAddress ("Example User", "[email protected]"));
Assert.Throws<ArgumentNullException> (() => new InternetAddressList (null));
Assert.Throws<ArgumentNullException> (() => list.Add (null));
Assert.Throws<ArgumentNullException> (() => list.AddRange (null));
Assert.Throws<ArgumentNullException> (() => list.CompareTo (null));
Assert.Throws<ArgumentNullException> (() => list.Contains (null));
Assert.Throws<ArgumentNullException> (() => list.CopyTo (null, 0));
Assert.Throws<ArgumentOutOfRangeException> (() => list.CopyTo (new InternetAddress[0], -1));
Assert.Throws<ArgumentNullException> (() => list.IndexOf (null));
Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, mailbox));
Assert.Throws<ArgumentNullException> (() => list.Insert (0, null));
Assert.Throws<ArgumentNullException> (() => list.Remove (null));
Assert.Throws<ArgumentOutOfRangeException> (() => list.RemoveAt (-1));
Assert.Throws<ArgumentOutOfRangeException> (() => list[-1] = mailbox);
Assert.Throws<ArgumentNullException> (() => list[0] = null);
}
示例8: TestPgpMimeEncryption
public void TestPgpMimeEncryption()
{
var self = new MailboxAddress ("MimeKit UnitTests", "[email protected]");
var recipients = new List<MailboxAddress> ();
// encrypt to ourselves...
recipients.Add (self);
var cleartext = new TextPart ("plain");
cleartext.Text = "This is some cleartext that we'll end up encrypting...";
using (var ctx = new DummyOpenPgpContext ()) {
var encrypted = MultipartEncrypted.Create (ctx, recipients, cleartext);
//using (var file = File.Create ("pgp-encrypted.asc"))
// encrypted.WriteTo (file);
var decrypted = encrypted.Decrypt (ctx);
Assert.IsInstanceOfType (typeof (TextPart), decrypted, "Decrypted part is not the expected type.");
Assert.AreEqual (cleartext.Text, ((TextPart) decrypted).Text, "Decrypted content is not the same as the original.");
}
}
示例9: TestMimeMessageSign
public void TestMimeMessageSign ()
{
var body = new TextPart ("plain") { Text = "This is some cleartext that we'll end up signing..." };
var self = new MailboxAddress ("MimeKit UnitTests", "[email protected]com");
var message = new MimeMessage { Subject = "Test of signing with OpenPGP" };
message.From.Add (self);
message.Body = body;
using (var ctx = new DummyOpenPgpContext ()) {
message.Sign (ctx);
Assert.IsInstanceOf<MultipartSigned> (message.Body);
var multipart = (MultipartSigned) message.Body;
Assert.AreEqual (2, multipart.Count, "The multipart/signed has an unexpected number of children.");
var protocol = multipart.ContentType.Parameters["protocol"];
Assert.AreEqual (ctx.SignatureProtocol, protocol, "The multipart/signed protocol does not match.");
Assert.IsInstanceOf<TextPart> (multipart[0], "The first child is not a text part.");
Assert.IsInstanceOf<ApplicationPgpSignature> (multipart[1], "The second child is not a detached signature.");
var signatures = multipart.Verify (ctx);
Assert.AreEqual (1, signatures.Count, "Verify returned an unexpected number of signatures.");
foreach (var signature in signatures) {
try {
bool valid = signature.Verify ();
Assert.IsTrue (valid, "Bad signature from {0}", signature.SignerCertificate.Email);
} catch (DigitalSignatureVerifyException ex) {
Assert.Fail ("Failed to verify signature: {0}", ex);
}
}
}
}
示例10: GetCmsRecipient
/// <summary>
/// Gets the <see cref="CmsRecipient"/> for the specified mailbox.
/// </summary>
/// <returns>A <see cref="CmsRecipient"/>.</returns>
/// <param name="mailbox">The mailbox.</param>
/// <exception cref="CertificateNotFoundException">
/// A certificate for the specified <paramref name="mailbox"/> could not be found.
/// </exception>
protected override CmsRecipient GetCmsRecipient (MailboxAddress mailbox)
{
var now = DateTime.Now;
foreach (var certificate in certificates) {
if (certificate.NotBefore > now || certificate.NotAfter < now)
continue;
var keyUsage = certificate.GetKeyUsageFlags ();
if (keyUsage != 0 && (keyUsage & X509KeyUsageFlags.KeyEncipherment) == 0)
continue;
if (certificate.GetSubjectEmailAddress () == mailbox.Address) {
var recipient = new CmsRecipient (certificate);
EncryptionAlgorithm[] algorithms;
if (capabilities.TryGetValue (certificate, out algorithms))
recipient.EncryptionAlgorithms = algorithms;
return recipient;
}
}
throw new CertificateNotFoundException (mailbox, "A valid certificate could not be found.");
}
示例11: SignAndEncryptEntity
/*
* tries to sign and encrypt a MimeEntity
*/
public static MimeEntity SignAndEncryptEntity(MimeEntity entity, MailboxAddress signer, IEnumerable<MailboxAddress> list)
{
using (WindowsSecureMimeContext ctx = new WindowsSecureMimeContext(sys.StoreLocation.CurrentUser))
{
return ApplicationPkcs7Mime.SignAndEncrypt(ctx, signer, DigestAlgorithm.Sha1, list, entity);
}
}
示例12: FlushCommandQueue
void FlushCommandQueue (MailboxAddress sender, IList<MailboxAddress> recipients, CancellationToken cancellationToken)
{
if (queued.Count == 0)
return;
try {
var responses = new List<SmtpResponse> ();
Exception rex = null;
int rcpt = 0;
// Note: queued commands are buffered by the stream
Stream.Flush (cancellationToken);
// Note: we need to read all responses from the server before we can process
// them in case any of them have any errors so that we can RSET the state.
try {
for (int i = 0; i < queued.Count; i++)
responses.Add (Stream.ReadResponse (cancellationToken));
} catch (Exception ex) {
// Note: save this exception for later (it may be related to
// an error response for a MAIL FROM or RCPT TO command).
rex = ex;
}
for (int i = 0; i < responses.Count; i++) {
switch (queued[i]) {
case SmtpCommand.MailFrom:
ProcessMailFromResponse (responses[i], sender);
break;
case SmtpCommand.RcptTo:
ProcessRcptToResponse (responses[i], recipients[rcpt++]);
break;
}
}
if (rex != null)
throw new SmtpProtocolException ("Error reading a response from the SMTP server.", rex);
} finally {
queued.Clear ();
}
}
示例13: Send
/// <summary>
/// Send the specified message using the supplied sender and recipients.
/// </summary>
/// <remarks>
/// Sends the message by uploading it to an SMTP server using the supplied sender and recipients.
/// </remarks>
/// <param name="options">The formatting options.</param>
/// <param name="message">The message.</param>
/// <param name="sender">The mailbox address to use for sending the message.</param>
/// <param name="recipients">The mailbox addresses that should receive the message.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress reporting mechanism.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="message"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="sender"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="recipients"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="SmtpClient"/> has been disposed.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="SmtpClient"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// Authentication is required before sending a message.
/// </exception>
/// <exception cref="System.InvalidOperationException">
/// <para>A sender has not been specified.</para>
/// <para>-or-</para>
/// <para>No recipients have been specified.</para>
/// </exception>
/// <exception cref="System.NotSupportedException">
/// Internationalized formatting was requested but is not supported by the server.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation has been canceled.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="SmtpCommandException">
/// The SMTP command failed.
/// </exception>
/// <exception cref="SmtpProtocolException">
/// An SMTP protocol exception occurred.
/// </exception>
public override void Send (FormatOptions options, MimeMessage message, MailboxAddress sender, IEnumerable<MailboxAddress> recipients, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null)
{
if (options == null)
throw new ArgumentNullException ("options");
if (message == null)
throw new ArgumentNullException ("message");
if (sender == null)
throw new ArgumentNullException ("sender");
if (recipients == null)
throw new ArgumentNullException ("recipients");
var rcpts = recipients.ToList ();
if (rcpts.Count == 0)
throw new InvalidOperationException ("No recipients have been specified.");
Send (options, message, sender, rcpts, cancellationToken, progress);
}
示例14: RcptTo
void RcptTo (MimeMessage message, MailboxAddress mailbox, CancellationToken cancellationToken)
{
var command = string.Format ("RCPT TO:<{0}>", mailbox.Address);
if ((capabilities & SmtpCapabilities.Dsn) != 0) {
var notify = GetDeliveryStatusNotifications (message, mailbox);
if (notify.HasValue)
command += " NOTIFY=" + GetNotifyString (notify.Value);
}
if ((capabilities & SmtpCapabilities.Pipelining) != 0) {
QueueCommand (SmtpCommand.RcptTo, command, cancellationToken);
return;
}
ProcessRcptToResponse (SendCommand (command, cancellationToken), mailbox);
}
示例15: GetCmsSigner
/// <summary>
/// Gets the <see cref="CmsSigner"/> for the specified mailbox.
/// </summary>
/// <returns>A <see cref="CmsSigner"/>.</returns>
/// <param name="mailbox">The mailbox.</param>
/// <param name="digestAlgo">The preferred digest algorithm.</param>
/// <exception cref="CertificateNotFoundException">
/// A certificate for the specified <paramref name="mailbox"/> could not be found.
/// </exception>
protected override CmsSigner GetCmsSigner (MailboxAddress mailbox, DigestAlgorithm digestAlgo)
{
var now = DateTime.Now;
foreach (var certificate in certificates) {
AsymmetricKeyParameter key;
if (certificate.NotBefore > now || certificate.NotAfter < now)
continue;
var keyUsage = certificate.GetKeyUsageFlags ();
if (keyUsage != 0 && (keyUsage & SecureMimeContext.DigitalSignatureKeyUsageFlags) == 0)
continue;
if (!keys.TryGetValue (certificate, out key))
continue;
if (certificate.GetSubjectEmailAddress () == mailbox.Address) {
var signer = new CmsSigner (certificate, key);
signer.DigestAlgorithm = digestAlgo;
return signer;
}
}
throw new CertificateNotFoundException (mailbox, "A valid signing certificate could not be found.");
}