本文整理汇总了C#中System.Net.Mail.AlternateView类的典型用法代码示例。如果您正苦于以下问题:C# AlternateView类的具体用法?C# AlternateView怎么用?C# AlternateView使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AlternateView类属于System.Net.Mail命名空间,在下文中一共展示了AlternateView类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendMail
public static void SendMail(IEmailAccountSettingsProvider provider, string to, string subject, string body, Attachment attach, AlternateView av, bool isHTML)
{
if (provider.SmtpFrom != String.Empty)
{
if (provider.SmtpHost == "") return;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(provider.SmtpFrom);
mailMessage.To.Add(to);
mailMessage.Subject = subject;
if (av != null)
mailMessage.AlternateViews.Add(av);
mailMessage.BodyEncoding = Encoding.UTF8;
mailMessage.Body = body;
if (attach != null)
mailMessage.Attachments.Add(attach);
if (isHTML)
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = provider.SmtpHost;
smtpClient.Port = Convert.ToInt32(provider.SmtpPort);
smtpClient.UseDefaultCredentials = Convert.ToBoolean(provider.SmtpCredentials);
smtpClient.Credentials = new NetworkCredential(provider.SmtpUser, provider.SmtpPwd);
smtpClient.EnableSsl = Convert.ToBoolean(provider.SmtpSSL);
smtpClient.Send(mailMessage);
}
}
示例2: Execute
public MailAttributes Execute(MailAttributes mailAttributes)
{
var newMailAttributes = new MailAttributes(mailAttributes);
foreach (var view in mailAttributes.AlternateViews)
{
using (var reader = new StreamReader(view.ContentStream))
{
var body = reader.ReadToEnd();
if (view.ContentType.MediaType == MediaTypeNames.Text.Html)
{
var inlinedCssString = PreMailer.MoveCssInline(body);
byte[] byteArray = Encoding.UTF8.GetBytes(inlinedCssString.Html);
var stream = new MemoryStream(byteArray);
var newAlternateView = new AlternateView(stream, MediaTypeNames.Text.Html);
newMailAttributes.AlternateViews.Add(newAlternateView);
}
else
{
newMailAttributes.AlternateViews.Add(view);
}
}
}
return newMailAttributes;
}
示例3: GetSerializeableAlternateView
internal static SerializeableAlternateView GetSerializeableAlternateView(AlternateView av)
{
if (av == null)
return null;
var sav = new SerializeableAlternateView();
sav._baseUri = av.BaseUri;
sav._contentId = av.ContentId;
if (av.ContentStream != null)
{
var bytes = new byte[av.ContentStream.Length];
av.ContentStream.Read(bytes, 0, bytes.Length);
sav._contentStream = new MemoryStream(bytes);
}
sav._contentType = SerializeableContentType.GetSerializeableContentType(av.ContentType);
foreach (LinkedResource lr in av.LinkedResources)
sav._linkedResources.Add(SerializeableLinkedResource.GetSerializeableLinkedResource(lr));
sav._transferEncoding = av.TransferEncoding;
return sav;
}
示例4: AddImagesToView
/// <summary>
/// Adds recorded <see cref="LinkedResource"/> image references to the given <see cref="AlternateView"/>.
/// </summary>
public void AddImagesToView(AlternateView view)
{
foreach (var image in images)
{
view.LinkedResources.Add(image.Value);
}
}
示例5: BuildMessageAndViews
public static MailMessage BuildMessageAndViews(EmailMessage message, out AlternateView textView, out AlternateView htmlView)
{
var smtpMessage = new MailMessage { BodyEncoding = Encoding.UTF8, From = new MailAddress(message.From) };
if(message.To.Count > 0) smtpMessage.To.Add(string.Join(",", message.To));
if(message.ReplyTo.Count > 0) smtpMessage.ReplyToList.Add(string.Join(",", message.ReplyTo));
if(message.Cc.Count > 0) smtpMessage.CC.Add(string.Join(",", message.Cc));
if(message.Bcc.Count > 0) smtpMessage.Bcc.Add(string.Join(",", message.Bcc));
htmlView = null;
textView = null;
if (!string.IsNullOrWhiteSpace(message.HtmlBody))
{
var mimeType = new ContentType("text/html");
htmlView = AlternateView.CreateAlternateViewFromString(message.HtmlBody, mimeType);
smtpMessage.AlternateViews.Add(htmlView);
}
if (!string.IsNullOrWhiteSpace(message.TextBody))
{
const string mediaType = "text/plain";
textView = AlternateView.CreateAlternateViewFromString(message.TextBody, smtpMessage.BodyEncoding, mediaType);
textView.TransferEncoding = TransferEncoding.SevenBit;
smtpMessage.AlternateViews.Add(textView);
}
return smtpMessage;
}
示例6: SerializableAlternateView
private SerializableAlternateView(AlternateView view) {
BaseUri = view.BaseUri;
LinkedResources = new SerializableLinkedResourceCollection();
foreach (LinkedResource res in view.LinkedResources)
LinkedResources.Add(res);
ContentId = view.ContentId;
ContentStream = new MemoryStream();
view.ContentStream.CopyTo(ContentStream);
view.ContentStream.Position = 0;
ContentType = view.ContentType;
TransferEncoding = view.TransferEncoding;
}
示例7: SendEmail
public static void SendEmail(string FromEmail, string FromDispalyName, string ToList, string Subject, string HtmBody, string BCCList = "", AlternateView view = null)
{
MailMessage msgMail = new MailMessage();
if (view != null)
msgMail.AlternateViews.Add(view);
if (!string.IsNullOrEmpty(ToList))
{
string[] toList = ToList.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (string s in toList)
{
msgMail.To.Add(s);
}
}
if (!string.IsNullOrEmpty(BCCList))
{
string[] bccList = BCCList.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (string s in bccList)
{
msgMail.Bcc.Add(s);
}
}
msgMail.From = new MailAddress(FromEmail, FromDispalyName, Encoding.UTF8);
msgMail.Subject = Subject;
msgMail.Body = HtmBody;
msgMail.IsBodyHtml = true;
msgMail.Priority = MailPriority.High;
msgMail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
SmtpClient Client = new SmtpClient(SystemConfigs.SMTPServer)
{
Port = Convert.ToInt32(SystemConfigs.SMTPPort),
EnableSsl = Convert.ToBoolean(SystemConfigs.SMTPIsSSL)
};
string UserName = SystemConfigs.SMTPUserName;
string Password = SystemConfigs.SMTPPassword;
if (!string.IsNullOrEmpty(UserName))
{
NetworkCredential Ceredentials = new NetworkCredential(UserName, Password);
Client.Credentials = Ceredentials;
}
else
{
Client.Credentials = CredentialCache.DefaultNetworkCredentials;
}
Client.Send(msgMail);
}
示例8: GetAlternateView
internal AlternateView GetAlternateView()
{
var sav = new AlternateView(_contentStream);
sav.BaseUri = _baseUri;
sav.ContentId = _contentId;
sav.ContentType = _contentType.GetContentType();
foreach (SerializeableLinkedResource lr in _linkedResources)
sav.LinkedResources.Add(lr.GetLinkedResource());
sav.TransferEncoding = _transferEncoding;
return sav;
}
示例9: GetAlternateView
public AlternateView GetAlternateView()
{
var sav = new AlternateView(ContentStream)
{
BaseUri = BaseUri,
ContentId = ContentId,
ContentType = ContentType.GetContentType(),
TransferEncoding = TransferEncoding,
};
foreach (var linkedResource in LinkedResources)
sav.LinkedResources.Add(linkedResource.GetLinkedResource());
return sav;
}
示例10: SerializableAlternateView
public SerializableAlternateView(AlternateView alternativeView)
{
BaseUri = alternativeView.BaseUri;
ContentId = alternativeView.ContentId;
ContentType = new SerializableContentType(alternativeView.ContentType);
TransferEncoding = alternativeView.TransferEncoding;
if (alternativeView.ContentStream != null)
{
byte[] bytes = new byte[alternativeView.ContentStream.Length];
alternativeView.ContentStream.Read(bytes, 0, bytes.Length);
ContentStream = new MemoryStream(bytes);
}
foreach (var lr in alternativeView.LinkedResources)
LinkedResources.Add(new SerializableLinkedResource(lr));
}
示例11: GenerateHtmlMessage
private MailMessage GenerateHtmlMessage(string from, string to, string subject, string content, string[] attachmentFilepaths)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
mail.To.Add(to);
mail.Subject = subject;
string body = null;
if (attachmentFilepaths != null && attachmentFilepaths.Length > 0)
{
StringBuilder sb = new StringBuilder();
sb.Append("MIME-Version: 1.0\r\n");
sb.Append("Content-Type: multipart/mixed; boundary=unique-boundary-1\r\n");
sb.Append("\r\n");
sb.Append("This is a multi-part message in MIME format.\r\n");
sb.Append("--unique-boundary-1\r\n");
sb.Append("Content-Type: text/html\r\n"); //could use text/plain as well here if you want a plaintext message
sb.Append("Content-Transfer-Encoding: 7Bit\r\n\r\n");
sb.Append(content);
if (!content.EndsWith("\r\n"))
sb.Append("\r\n");
sb.Append("\r\n\r\n");
foreach (string filepath in attachmentFilepaths)
{
sb.Append(GenerateRawAttachement(filepath));
}
body = sb.ToString();
}
else
{
body = "Content-Type: text/html\r\nContent-Transfer-Encoding: 7Bit\r\n\r\n" + content;
}
//input your certification and private key.
X509Certificate2 cert = new X509Certificate2("emailcertification.pfx", "6522626", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
ContentInfo contentInfo = new ContentInfo(Encoding.UTF8.GetBytes(body));
SignedCms signedCms = new SignedCms(contentInfo, false);
CmsSigner Signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, cert);
signedCms.ComputeSignature(Signer);
byte[] signedBytes = signedCms.Encode();
MemoryStream stream = new MemoryStream(signedBytes);
AlternateView view = new AlternateView(stream, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");
mail.AlternateViews.Add(view);
return mail;
}
示例12: CreateView
public static AlternateView CreateView( IHtmlDocument document )
{
var stream = new MemoryStream();
document.Render( stream, Encoding.UTF8 );
stream.Seek( 0, SeekOrigin.Begin );
var resources = GetResources( document );
var view = new AlternateView( stream, "text/html" );
view.TransferEncoding = TransferEncoding.Base64;
view.BaseUri = document.DocumentUri;
foreach ( var r in resources )
view.LinkedResources.Add( r );
return view;
}
示例13: SetContent
private void SetContent(bool allowUnicode)
{
//the attachments may have changed, so we need to reset the message
if (_bodyView != null)
{
_bodyView.Dispose();
_bodyView = null;
}
if (AlternateViews.Count == 0 && Attachments.Count == 0)
{
if (!string.IsNullOrEmpty(_body))
{
_bodyView = AlternateView.CreateAlternateViewFromString(_body, _bodyEncoding, (_isBodyHtml ? MediaTypeNames.Text.Html : null));
_message.Content = _bodyView.MimePart;
}
}
else if (AlternateViews.Count == 0 && Attachments.Count > 0)
{
MimeMultiPart part = new MimeMultiPart(MimeMultiPartType.Mixed);
if (!string.IsNullOrEmpty(_body))
{
_bodyView = AlternateView.CreateAlternateViewFromString(_body, _bodyEncoding, (_isBodyHtml ? MediaTypeNames.Text.Html : null));
}
else
{
_bodyView = AlternateView.CreateAlternateViewFromString(string.Empty);
}
part.Parts.Add(_bodyView.MimePart);
foreach (Attachment attachment in Attachments)
{
if (attachment != null)
{
//ensure we can read from the stream.
attachment.PrepareForSending(allowUnicode);
part.Parts.Add(attachment.MimePart);
}
}
_message.Content = part;
}
else
{
// we should not unnecessarily use Multipart/Mixed
// When there is no attachement and all the alternative views are of "Alternative" types.
MimeMultiPart part = null;
MimeMultiPart viewsPart = new MimeMultiPart(MimeMultiPartType.Alternative);
if (!string.IsNullOrEmpty(_body))
{
_bodyView = AlternateView.CreateAlternateViewFromString(_body, _bodyEncoding, null);
viewsPart.Parts.Add(_bodyView.MimePart);
}
foreach (AlternateView view in AlternateViews)
{
//ensure we can read from the stream.
if (view != null)
{
view.PrepareForSending(allowUnicode);
if (view.LinkedResources.Count > 0)
{
MimeMultiPart wholeView = new MimeMultiPart(MimeMultiPartType.Related);
wholeView.ContentType.Parameters["type"] = view.ContentType.MediaType;
wholeView.ContentLocation = view.MimePart.ContentLocation;
wholeView.Parts.Add(view.MimePart);
foreach (LinkedResource resource in view.LinkedResources)
{
//ensure we can read from the stream.
resource.PrepareForSending(allowUnicode);
wholeView.Parts.Add(resource.MimePart);
}
viewsPart.Parts.Add(wholeView);
}
else
{
viewsPart.Parts.Add(view.MimePart);
}
}
}
if (Attachments.Count > 0)
{
part = new MimeMultiPart(MimeMultiPartType.Mixed);
part.Parts.Add(viewsPart);
MimeMultiPart attachmentsPart = new MimeMultiPart(MimeMultiPartType.Mixed);
foreach (Attachment attachment in Attachments)
{
if (attachment != null)
{
//ensure we can read from the stream.
attachment.PrepareForSending(allowUnicode);
attachmentsPart.Parts.Add(attachment.MimePart);
}
}
//.........这里部分代码省略.........
示例14: CreateAlternateViewFromString
public static AlternateView CreateAlternateViewFromString (string content)
{
if (content == null)
throw new ArgumentNullException ();
MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (content));
AlternateView av = new AlternateView (ms);
av.TransferEncoding = TransferEncoding.QuotedPrintable;
return av;
}
示例15: CreateAlternateView
/// <summary>
/// Creates the alternate view.
/// </summary>
/// <param name="view">The view.</param>
/// <returns></returns>
private AlternateView CreateAlternateView(MimeEntity view)
{
AlternateView alternateView = new AlternateView(view.Content, view.ContentType);
alternateView.TransferEncoding = view.ContentTransferEncoding;
alternateView.ContentId = TrimBrackets(view.ContentId);
return alternateView;
}