2014-09-17 14 views
13

Próbuję dowiedzieć się, jak dodać zmienne do istniejącego szablonu (przykład: łącze sieciowe lub nazwa dynamicznie), które zostało utworzone w silniku szablonu sendgrid, nie jestem pewien jak to zrobić, korzystając z bibliotek C# .NET SendGrid. Zastanawiam się, czy ktoś mógłby mi pomóc.Jak dodawać zmienne niestandardowe do wiadomości e-mail SendGrid za pośrednictwem interfejsu API C# i szablonu

// Create the email object first, then add the properties. 
SendGridMessage myMessage = new SendGridMessage(); 

myMessage.AddTo("[email protected]"); 
myMessage.From = new MailAddress("[email protected]", "Mr test"); 
myMessage.Subject = " "; 

var timestamp = DateTime.Now.ToString("HH:mm:ss tt"); 
myMessage.Html = "<p></p> "; 

myMessage.EnableTemplate("<p> <% body %> Hello</p>"); 
myMessage.EnableTemplateEngine("9386b483-8ad4-48c2-9ee3-afc7618eb56a"); 
var identifiers = new Dictionary<String, String>(); 
identifiers["USER_FULLNAME"] = "Jimbo Jones"; 
identifiers["VERIFICATION_URL"] = "www.google.com"; 
myMessage.AddUniqueArgs(identifiers); 

// Create credentials, specifying your user name and password. 
var credentials = new NetworkCredential("username", "password"); 
// Create an Web transport for sending email. 
var transportWeb = new Web(credentials); 
// Send the email. 
transportWeb.Deliver(myMessage); 

Odpowiedz

6

znalazłem rozwiązanie:

replacementKey = "*|VERIFICATION_URL|*"; 
substitutionValues = new List<string> { VERIFICATION_URL }; 

myMessage.AddSubstitution(replacementKey, substitutionValues); 
+0

Trzeba było wyjąć coś z kodu w pytaniu? – Danilo

+0

Nie rozumiem o co ci chodzi.? –

+0

Zignorujcie mój komentarz, rozwiązałem mój problem. Dzięki, – Danilo

9

mój email

Hello -REP- 

<%body%> 

Fotter 

Mam C# kod

myMessage.AddSubstitution("-REP-", substitutionValues); 

Works PERFECT !!!

3

Użyłem następującego podejścia. Zauważ, że musisz podać wartości mail.Text i mail.Html - używam pustego łańcucha i tagu <p></p>, jak widać w przykładzie. Twój szablon SendGrid nadal musi zawierać domyślne tokeny <%body%> i <%subject%>, ale zostaną one zastąpione rzeczywistym obiektem i wartością ciała określoną we właściwościach mail.Text i.

public void Send(string from, string to, string subject, IDictionary<string, string> replacementTokens, string templateId) 
{ 
    var mail = new SendGridMessage 
    { 
    From = new MailAddress(from) 
    }; 

    mail.AddTo(to); 
    mail.Subject = subject; 

    // NOTE: Text/Html and EnableTemplate HTML are not really used if a TemplateId is specified 
    mail.Text = string.Empty; 
    mail.Html = "<p></p>"; 
    mail.EnableTemplate("<%body%>"); 

    mail.EnableTemplateEngine(templateId); 

    // Add each replacement token 
    foreach (var key in replacementTokens.Keys) 
    { 
    mail.AddSubstitution(
     key, 
     new List<string> 
     { 
      replacementTokens[key] 
     }); 
    } 

    var transportWeb = new Web("THE_AUTH_KEY"); 
    var result = transportWeb.DeliverAsync(mail); 
} 

To może być nazywany tak:

Send(
"[email protected]", 
"TO_ADDRESS", 
"THE SUBJECT", 
new Dictionary<string, string> { 
{ "@Param1!", "Parameter 1" }, 
{ "@Param2!", "Parameter 2" } }, 
"TEMPLATE_GUID"); 
3

Po zrobił wiele RND. Mój poniższy kod działa dobrze & Testowany również.

  SendGridMessage myMessage = new SendGridMessage(); 
      myMessage.AddTo(email); 
      myMessage.AddBcc("[email protected]"); 
      myMessage.AddBcc("[email protected]"); 
      myMessage.From = new MailAddress("[email protected]", "DriverPickup"); 
      //myMessage.Subject = "Email Template Test 15."; 
      myMessage.Headers.Add("X-SMTPAPI", GetMessageHeaderForWelcome("[email protected]", callBackUrl)); 
      myMessage.Html = string.Format(" "); 

      // Create an Web transport for sending email. 
      var transportWeb = new Web(SendGridApiKey); 

      // Send the email, which returns an awaitable task. 
      transportWeb.DeliverAsync(myMessage); 

Stworzyłem oddzielna metoda uzyskiwania JSON nagłówek

private static string GetMessageHeaderForWelcome(string email, string callBackUrl) 
    { 

     var header = new Header(); 
     //header.AddSubstitution("{{FirstName}}", new List<string> { "Dilip" }); 
     //header.AddSubstitution("{{LastName}}", new List<string> { "Nikam" }); 
     header.AddSubstitution("{{EmailID}}", new List<string> { email }); 
     header.AddSubstitution("-VERIFICATIONURL-", new List<string>() { callBackUrl }); 
     //header.AddSubstitution("*|VERIFICATIONURL|*", new List<string> { callBackUrl }); 
     //header.AddSubstitution("{{VERIFICATIONURL}}", new List<string> { callBackUrl }); 
     header.AddFilterSetting("templates", new List<string> { "enabled" }, "1"); 
     header.AddFilterSetting("templates", new List<string> { "template_id" }, WelcomeSendGridTemplateID); 
     return header.JsonString(); 

    } 

poniższy kod użyłem w moim szablonu HTML Sendgrid.

<div>Your {{EmailID}} register. Please <a class="btn-primary" href="-VERIFICATIONURL-">Confirm email address</a></div> 

W przypadku jakichkolwiek pytań proszę o kontakt.

inline HTML wymienić trzeba użyć -YourKeyForReplace- & tekstu wymienić trzeba użyć {{UserKeyForReplace}}

Powiązane problemy