2017-12-04 9 views
1

私はasp.net mvc IdentityとSendGridを初めて使いましたが、本当に両方の機能を使用したいと思います。SendGrid v3を使用して、トランザクションテンプレートを確認メールとして送信するID

私はID登録フォームを使用してサインアップし、SendGrid v3を使用して(SendGridアカウントで作成された)テンプレートをアカウント登録確認メールとして送信します。私はTransactionalテンプレートを作成し、Api Keyを持っています。

私はアイデンティティに確認メールを有効にしている:

await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); 

       // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 
       // Send an email with this link 
       string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
       var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
       await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

       return RedirectToAction("Index", "Home"); 

を私はその後、私のsendGrid APIKEYを設定し、私は私のコードでそれらを使用することができますので、私のweb.configのアプリの設定で資格情報を占めています。

<appSettings> 
    <add key="SendGridUsername" value="xxxxxxx" /> 
    <add key="SendGridPassword" value="xxxxxxx" /> 
    <add key="SendGridApiKey" value="xxxxxxxxxxxxxxxxxxxxxxxx" /> 
</appSettings> 

私はIdentityConfig.csで私EmailServiceにこれを追加したが、私はここからどこへ行くにこだわっている:

public class EmailService : IIdentityMessageService 
{ 
    public async Task SendAsync(IdentityMessage message) 
    { 
     // Plug in your email service here to send an email. 
     var apiKey = WebConfigurationManager.AppSettings["SendGridApiKey"]; 
     var client = new SendGridClient(apiKey); 
     var from = new EmailAddress("[email protected]", "Me"); 
     var subject = message.Subject; 
     var to = new EmailAddress(message.Destination); 
     var email = MailHelper.CreateSingleEmail(from, to, subject, "", message.Body); 
     await client.SendEmailAsync(email); 
    } 
} 

私も次を読んだが、どこまで理解することはできませんそれを実装:

https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/smtpapi.html

{ 
    "filters": { 
    "templates": { 
     "settings": { 
     "enable": 1, 
     "template_id": "5997fcf6-2b9f-484d-acd5-7e9a99f0dc1f" 
     } 
    } 
    } 
} 

この上の任意のヘルプは次のようになり私はちょうどここからどこに行くか分からないので素晴らしいです。

おかげ

+0

は誰もがこれを 答えることはできますか?そこにコードの例がないようです - 私は日を探していた! – DBoi

答えて

0

あなたは以下のコードを使用してあなたの電子メールでのトランザクションテンプレートを送ることができます。

 var apiKey = AppConstants.JuketserSendGridKey; 
     var client = new SendGridClient(apiKey); 

     var msg = new SendGridMessage(); 
     msg.SetFrom(new EmailAddress("[email protected]", "Jukester")); 
     //msg.SetSubject("I'm replacing the subject tag"); 
     msg.AddTo(new EmailAddress(model.EmailTo)); 
     //msg.AddContent(MimeType.Text, "I'm replacing the <strong>body tag</strong>"); 
     msg.SetTemplateId("Your TemplateId here"); 

     var response = await client.SendEmailAsync(msg); 
     var status = response.StatusCode.ToString(); 
関連する問題