本文整理汇总了C#中SendGrid.SendGridMessage.EnableClickTracking方法的典型用法代码示例。如果您正苦于以下问题:C# SendGridMessage.EnableClickTracking方法的具体用法?C# SendGridMessage.EnableClickTracking怎么用?C# SendGridMessage.EnableClickTracking使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SendGrid.SendGridMessage
的用法示例。
在下文中一共展示了SendGridMessage.EnableClickTracking方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Send
public async Task<string> Send(IMessage message)
{
var msg = new SendGridMessage();
msg.Subject = message.Subject;
msg.From = new MailAddress(message.From);
msg.Html = message.Body;
msg.AddTo(message.To);
msg.EnableClickTracking();
msg.EnableOpenTracking();
try
{
var transportWeb = new Web(_apiKey);
await transportWeb.DeliverAsync(msg);
}
catch (Exception ex)
{
throw new MailerException(ex.Message, MailerException.enReason.RejectByProvider);
}
return null;
}
示例2: SendNotificationAsync
public override async Task<bool> SendNotificationAsync(PushNotificationItem notificationItem, object message, CancellationToken ct)
{
var cfg = (SendGridDeliveryProviderConfiguration)Configuration;
var sent = false;
//implicit conversion operator
MailMessage smsg = message as SerializableMailMessage;
if (smsg != null)
{
try
{
var hView = smsg.AlternateViews.First(v => v.ContentType.MediaType == "text/html");
var tView = smsg.AlternateViews.First(v => v.ContentType.MediaType == "text/plain");
var sendGridMessage = new SendGridMessage
{
To = new[]
{
new MailAddress(notificationItem.Destination.DestinationAddress,
notificationItem.Destination.SubscriberName)
},
From = new MailAddress(cfg.FromAddress, cfg.FromDisplayName),
Subject = smsg.Subject,
Html = hView.ContentStream.ReadToString(),
Text = tView.ContentStream.ReadToString()
};
if (cfg.EnableClickTracking ?? false)
{
sendGridMessage.EnableClickTracking();
}
if (cfg.EnableGravatar ?? false)
{
sendGridMessage.EnableGravatar();
}
if (cfg.EnableOpenTracking ?? false)
{
sendGridMessage.EnableOpenTracking();
}
if (cfg.SendToSink ?? false)
{
sendGridMessage.SendToSink();
}
var transport = new Web(cfg.ApiKey);
await transport.DeliverAsync(sendGridMessage);
sent = true;
}
catch
{
sent = false;
//TODO: log this somewhere
}
}
return sent;
}
示例3: Send
public void Send()
{
SendGridMessage client = new SendGridMessage();
client.From = new MailAddress(From);
client.AddTo(To);
client.Subject = Subject;
client.Text = Regex.Replace(Body, "<.*?>", string.Empty);
client.Html = Body.Replace("\r\n", "<br>");
client.EnableClickTracking();
client.EnableOpenTracking();
Web transport = new Web(ConfigurationManager.AppSettings[GetType().Name]);
transport.DeliverAsync(client).ConfigureAwait(false);
}
示例4: SendMailBatch
/// <summary>
/// Sends the mail batch using the SendGrid API
/// </summary>
/// <param name="mail">The mail.</param>
/// <param name="recipients">The recipients.</param>
/// <param name="onlyTestDontSendMail">if set to <c>true</c> [only test dont send mail].</param>
/// <returns></returns>
/// <exception cref="System.NotImplementedException"></exception>
public override bool SendMailBatch(MailInformation mail, IEnumerable<JobWorkItem> recipients, bool onlyTestDontSendMail)
{
var settings = GetSettings();
if (recipients == null || recipients.Any() == false)
throw new ArgumentException("No workitems", "recipients");
if (recipients.Count() > 1000)
throw new ArgumentOutOfRangeException("recipients", "SendGrid supports maximum 1000 recipients per batch send.");
var msg = new SendGridMessage();
msg.From = new MailAddress(mail.From);
msg.Subject = mail.Subject;
msg.Html = mail.BodyHtml;
msg.Text = mail.BodyText;
// Add recipinets to header, to hide other recipients in to field.
List<string> addresses = recipients.Select(r => r.EmailAddress).ToList();
msg.Header.SetTo(addresses);
msg.AddSubstitution("%recipient%", addresses);
// To send message we need to have a to address, set that to from
msg.To = new MailAddress[] { msg.From };
if (mail.EnableTracking)
{
// true indicates that links in plain text portions of the email
// should also be overwritten for link tracking purposes.
msg.EnableClickTracking(true);
msg.EnableOpenTracking();
}
if(mail.CustomProperties.ContainsKey("SendGridCategory"))
{
string category = mail.CustomProperties["SendGridCategory"] as string;
if (string.IsNullOrEmpty(category) == false)
msg.SetCategory(category);
}
var credentials = new NetworkCredential(settings.Username, settings.Password);
// Create an Web transport for sending email.
var transportWeb = new Web(credentials);
transportWeb.Deliver(msg);
return true;
}
示例5: SendEmail
public async Task SendEmail(string subject, string fromEmail, string fromName, List<string> toEmailsList, string plainBody, string htmlBody)
{
var myMessage = new SendGridMessage {From = new MailAddress(fromEmail, fromName)};
myMessage.AddTo(toEmailsList);
myMessage.Subject = subject;
if (!string.IsNullOrEmpty(htmlBody))
{
myMessage.Html = htmlBody;
}
if (!string.IsNullOrEmpty(plainBody))
{
myMessage.Text = plainBody;
}
//myMessage.EnableFooter("PLAIN TEXT FOOTER", "<p><em>HTML FOOTER</em></p>");
myMessage.EnableClickTracking(true);
var credentials = new NetworkCredential(UserName, Password);
var transportWeb = new Web(credentials);
await transportWeb.DeliverAsync(myMessage);
}
示例6: SendEmail
public async Task<HttpResponseMessage> SendEmail(Email email)
{
var myMessage = new SendGridMessage();
myMessage.From = new MailAddress(email.from);
List<string> recipients = email.recipients.Split(',', ';').ToList<string>();
myMessage.AddTo(recipients);
myMessage.Subject = email.subject;
myMessage.Text = string.IsNullOrEmpty(email.text) ? null : email.text;
myMessage.Html = string.IsNullOrEmpty(email.html) ? null : email.html;
myMessage.EnableClickTracking(email.clickTracking);
var transportWeb = new Web(ConfigurationManager.AppSettings["SendgridApiKey"]);
await transportWeb.DeliverAsync(myMessage);
return Request.CreateResponse(HttpStatusCode.OK);
}
示例7: Test_EnablingClickTracking_true
public void Test_EnablingClickTracking_true()
{
var mail = BasicMailBuilder
.EnableClickTracking(true)
.Build();
var message = new SendGridMessage();
message.EnableClickTracking(true);
Assert.IsFalse(string.IsNullOrEmpty(message.Header.JsonString()));
Assert.AreEqual(message.Header.JsonString(), mail.Header.JsonString());
}
示例8: sendMailWithSendgrid
private static Task sendMailWithSendgrid(string email, string dni, string url)
{
// Create the email object first, then add the properties.
SendGridMessage mensaje = new SendGridMessage();
mensaje.AddTo(email);
mensaje.From = new MailAddress("[email protected]", "Administracion Cent11");
mensaje.Subject = "Creando usuario con dni " + dni;
mensaje.Text = String.Format("<a href='{0}'>Click para activar usuario</a>", url);
mensaje.Html = String.Format("<a href='{0}'>Click para activar usuario</a>", url);
mensaje.EnableClickTracking(true);
// Create an Web transport for sending email.
var transportWeb = new Web(SendGridMailApiKey);
// Send the email, which returns an awaitable task.
return transportWeb.DeliverAsync(mensaje);
}
示例9: EnableClickTrackingEmail
/// <summary>
/// Point the urls to Sendgrid Servers so that the clicks can be logged before
/// being directed to the appropriate link
/// http://docs.sendgrid.com/documentation/apps/click-tracking/
/// </summary>
public void EnableClickTrackingEmail()
{
//create a new message object
var message = new SendGridMessage();
//set the message recipients
foreach (var recipient in _to)
{
message.AddTo(recipient);
}
//set the sender
message.From = new MailAddress(_from);
//set the message body
var timestamp = DateTime.Now.ToString("HH:mm:ss tt");
message.Html = "<p style='color:red';>Hello World HTML </p> <a href='http://microsoft.com'>Checkout Microsoft!!</a>";
message.Html += "<p>Sent At : " + timestamp + "</p>";
message.Text = "hello world http://microsoft.com";
//set the message subject
message.Subject = "Hello World Click Tracking Test";
//create an instance of the Web transport mechanism
var transportInstance = new Web(new NetworkCredential(_username, _password));
//enable clicktracking
message.EnableClickTracking();
//send the mail
transportInstance.DeliverAsync(message);
}
示例10: SendEmailViaSendGrid
public static void SendEmailViaSendGrid(string to, string from, string subject, string htmlBody, MailType type, string textBody, string[] multipleTo = null)
{
try
{
//var message = SendGrid.GenerateInstance();
var message = new SendGridMessage();
if (String.IsNullOrEmpty(to))
message.AddTo(multipleTo);
else
message.AddTo(to);
//if (multipleTo != null)
// message.AddTo(multipleTo);
//else
// message.AddTo(to);
message.From = new System.Net.Mail.MailAddress(from);
message.Subject = subject;
if (type == MailType.TextOnly)
message.Text = textBody.Replace(@"\r\n", Environment.NewLine);
else if (type == MailType.HtmlOnly)
message.Html = htmlBody;
else
{
message.Html = htmlBody;
message.Text = textBody;
}
//Dictionary<string, string> collection = new Dictionary<string, string>();
//collection.Add("header", "header");
//message.Headers = collection;
message.EnableOpenTracking();
message.EnableClickTracking();
message.DisableUnsubscribe();
message.DisableFooter();
message.EnableBypassListManagement();
//var transportInstance = SMTP.GenerateInstance(new System.Net.NetworkCredential(SendGridUsername, SendGridPassword), SendGridSmtpHost, SendGridSmtpPort);
var transportInstance = new Web(new System.Net.NetworkCredential(SendGridUsername, SendGridPassword));
transportInstance.Deliver(message);
if (String.IsNullOrEmpty(to))
Console.WriteLine("SendGrid: Email was sent successfully to " + multipleTo);
else
Console.WriteLine("SendGrid: Email was sent successfully to " + to);
}
catch (Exception)
{
if (String.IsNullOrEmpty(to))
Console.WriteLine("SendGrid: Unable to send email to " + multipleTo);
else
Console.WriteLine("SendGrid: Unable to send email to " + to);
throw;
}
}
示例11: EnableClickTracking
public void EnableClickTracking()
{
var header = new Header();
var sendgrid = new SendGridMessage(header);
sendgrid.EnableClickTracking(true);
var json = header.JsonString();
Assert.AreEqual("{\"filters\" : {\"clicktrack\" : {\"settings\" : {\"enable\" : \"1\",\"enable_text\" : \"1\"}}}}",
json);
}
示例12: SendSendGridEmail
/// <summary>
/// This function will send the email via SendGrid.
/// </summary>
/// <param name="kMessage">MailMessage - Message object to send</param>
protected void SendSendGridEmail(MailMessage kMessage)
{
try
{
StringBuilder sb = new StringBuilder();
sb.Append("Date:");
sb.AppendLine();
sb.Append(DateTime.Now.ToString());
sb.AppendLine();
// Create the email object first, then add the properties.
var sgMessage = new SendGridMessage();
// Add the message properties.
sgMessage.From = new MailAddress(kMessage.From.ToString());
sb.Append("From:");
sb.AppendLine();
sb.Append(sgMessage.From.Address);
sb.AppendLine();
// Add multiple addresses to the To field.
sb.Append("To:");
sb.AppendLine();
foreach (MailAddress address in kMessage.To)
{
sgMessage.AddTo(address.Address);
sb.Append(address.Address + ";");
}
sb.AppendLine();
sgMessage.Subject = kMessage.Subject;
sb.Append("Subject:");
sb.AppendLine();
sb.Append(sgMessage.Subject);
sb.AppendLine();
// HTML & plain-text
if (kMessage.AlternateViews.Count > 0)
{
foreach (AlternateView view in kMessage.AlternateViews)
{
// Position must be reset first
if (view.ContentStream.CanSeek)
{
view.ContentStream.Position = 0;
}
using (StreamWrapper wrappedStream = StreamWrapper.New(view.ContentStream))
{
using (StreamReader reader = StreamReader.New(wrappedStream))
{
if (view.ContentType.MediaType == MediaTypeNames.Text.Html)
{
sgMessage.Html = reader.ReadToEnd();
}
else if (view.ContentType.MediaType == MediaTypeNames.Text.Plain)
{
sgMessage.Text = reader.ReadToEnd();
}
}
}
}
}
sb.Append("Body:");
if (ValidationHelper.GetString(sgMessage.Html, "") != "")
{
sb.Append(sgMessage.Html);
}
else
{
sb.Append(sgMessage.Text);
}
sb.AppendLine();
//Handle any attachments
sb.Append("Attachments:");
sb.AppendLine();
foreach (Attachment attachment in kMessage.Attachments)
{
sgMessage.AddAttachment(attachment.ContentStream, attachment.Name);
sb.Append(attachment.Name);
sb.AppendLine();
}
//Enable click tracking
sgMessage.EnableClickTracking(true);
// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential("[SendGridLogin]", "[SendGridPassword]");
// Create an Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email.
transportWeb.Deliver(sgMessage);
//.........这里部分代码省略.........
示例13: SendSMTP
public void SendSMTP(string from, string to, string subject, string message, string cc = null, string bcc = null, string attachments = null, string alternateText = null, bool trackLinks = false)
{
SendGridMessage mailMessage = new SendGridMessage();
MailAddress fromAddress = new MailAddress(from);
List<string> recipients = to.Replace(",", ";").Split(';').ToList();
mailMessage.From = fromAddress;
mailMessage.AddTo(recipients);
mailMessage.Subject = subject;
mailMessage.Html = message;
mailMessage.Text = alternateText;
if(!String.IsNullOrEmpty(attachments))
{
List<string> attachmentFiles = attachments.Replace(",", ";").Split(';').ToList();
foreach(string attachmentFile in attachmentFiles)
{
mailMessage.AddAttachment(attachmentFile);
}
}
mailMessage.EnableClickTracking(trackLinks);
NetworkCredential credentials = new NetworkCredential(this._smtpUser, this._smtpPassword);
var webTransport = new Web(credentials);
webTransport.DeliverAsync(mailMessage).Wait();
}