2017-12-04 25 views
1

저는 asp.net mvc Identity와 SendGrid를 처음 사용했지만 두 가지 기능을 모두 사용하고 싶습니다.SendGrid v3를 사용하여 신원 확인 전자 메일로 트랜잭션 템플릿을 보내는 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();