- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 asp.net mvc Identity 和 SendGrid 的新手,但我真的很想使用它们的功能。
我想让用户使用身份注册表进行注册,然后使用 SendGrid v3 发送一个模板(内置在我的 SendGrid 帐户中)作为帐户注册确认电子邮件。我已经创建了一个交易模板并拥有一个 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");
然后我在我的 web.config 的应用程序设置中设置了我的 sendGrid apiKey 和帐户凭据,以便我可以在我的代码中使用它们。
<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("me@us.com", "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"
}
}
}
}
任何关于这方面的帮助都会很棒,因为我只是不确定从这里去哪里。
谢谢
最佳答案
您可以使用以下代码在您的电子邮件中发送交易模板:
var apiKey = AppConstants.JuketserSendGridKey;
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage();
msg.SetFrom(new EmailAddress("admin@jukester.com", "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();
Edit For Your Other question:
对于电子邮件确认场景,您必须在用户注册时向注册的电子邮件发送电子邮件。创建验证 token 并将其保存在数据库中。该电子邮件将包含一些链接或按钮。此链接或按钮将带有该验证 token 。一旦用户点击该链接/按钮,将在项目中调用 webapi 或操作方法,在那里您将验证验证码,然后更新数据库中 EmailConfirmed 的状态。
以下是我完成的一些代码片段,它们可能对你有帮助。
以下代码创建验证码并更新数据库中的用户记录。
var encryptedToken = Utility.Crypt(user.Email);
var updateStatus = await UpdateVerificationCode(userToAdd, encryptedToken);
下面再将这个验证码传给邮件中需要发送的数据。 “paramList”是一个数据列表。
if (updateStatus)
{
paramList.Add(encryptedToken);
var emailModel = Utility.CreateEmailModel(user.Email, paramList, AppConstants.RegistrationTemplateId, (int)EmailType.Register);
await Helper.SendEmail(emailModel);
}
现在,此代码将附加在发送给用户的电子邮件中的链接或按钮中,以进行电子邮件验证。当用户单击该链接/按钮时,将调用以下用于电子邮件验证的 Web API 操作方法。
public async Task<GenericResponse> ConfirmEmail(SetPasswordBindingModel model)
{
var response = new GenericResponse();
if (model != null)
{
try
{
var user = await _aspNetUserService.GetByEmail(model.Email);
if (user != null)
{
if (!string.IsNullOrEmpty(model.VerificationCode))
{
//if time difference is less than 5 minutes then proceed
var originalKey = Utility.Decrypt(model.VerificationCode);
if (user.Email == originalKey)
{
var emailConfirmationCode = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var result = await UserManager.ConfirmEmailAsync(user.Id, emailConfirmationCode);
if (result.Succeeded)
{
var status = await _aspNetUserService.ResetVerificationCode(model.Email);
if (status)
{
response.Message = AppConstants.EmailConfirmed;
response.IsSuccess = true;
}
}
else
{
response.Message = AppConstants.Error;
response.IsSuccess = false;
}
}
else
{
response.Message = AppConstants.InvalidVerificationCode;
response.IsSuccess = false;
}
}
else
{
response.Message = AppConstants.InvalidVerificationCode;
response.IsSuccess = false;
}
}
else
{
response.Message = AppConstants.NoUserFound;
response.IsSuccess = false;
}
}
catch (Exception ex)
{
//response.Message = AppConstants.Error;
response.Message = ex.Message;
}
}
return response;
}
您可以看看它,如果它对您的需求有帮助,请使用它。谢谢
关于c# - 使用 SendGrid v3 的身份将交易模板作为确认电子邮件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47636507/
我听过很多次公司喜欢HBase的强一致性。我阅读了 HBase 并喜欢它。然后我想到了mongodb write和那时候的区别。查了一下MongoDB似乎也有很强的一致性。但它是一致的吗?看起来 HB
区块链入门 ③ - 交易 交易 概述 比特币交易本质上包含交易参与者价值转移的相关信息数据结构。比特币区块链是一本全球复式记账总账簿,每笔交易都是在比特币区块链上的一个公开记录.
我有以下情况: 我正在迭代我的Affiliate 实体,对于每个实体,我需要在一个唯一的事务中保存和更新数据。因此,我有一个服务,其方法用 Spring @Transactional 注释(其中创建和
我无法理解 DaoManager 的默认行为。 DaoManager.createDao(connectionSource, theClass); 这需要一个 connectionSource - 而
我是 Spring 新手,有一个关于事务的问题。 我知道对于每个 http 请求都有一个 servlet 线程,它有自己的堆栈。据我所知,所有局部变量和方法都驻留在堆栈上。因此,如果我有一个方法 pu
我想设计一个简单的应用程序(没有 j2ee 和 jms),可以处理大量消息(比如在交易系统中) 我创建了一个服务,可以接收消息并将它们放入队列中,这样系统就不会在过载时卡住。 然后我创建了一个包装队列
如果使用 PDO 事务,是否需要锁定表? 如果用户 a 有 50 笔钱,将 50 笔转给用户 b,PDO 交易是否会确保它们都无误地执行? 另外,如果说我有一个 if 语句, if ($user['m
我正在实现一个方法,它会做类似的事情: ... try { myPojo.setProperty("foo"); myService.execute(myPojo); } catch (E
我正在尝试使用 ActiveRecord::Base.transaction。我认为使用 Rails 1.2.6 和 mysql 5.0 默认情况下回滚不起作用。多玩一点我发现 autocommit
我在我的网站上使用嵌入式支付,支付交易直接从买家到卖家发起,服务充当 API 调用方。商品价格由卖家以美元设定,以简化国际贸易。 当发件人和收件人都是俄罗斯居民时,发件人会收到错误消息: The pa
如果我删除我的应用程序中的数据,然后重新购买一些我知道该帐户已经拥有的托管 IAP,iOS 会给我原生的“您确定要重新购买该项目吗?您不会被收取费用”对话框。这符合预期。 当购买返回到我的应用程序时,
我一直在阅读 transactions & jooq但我很难看到如何在实践中实现它。 假设我为 JOOQ 提供了一个自定义 ConnectionProvider,它恰好使用了一个自动提交设置为 fal
我们正在使用 Entity Framework 并在事务范围内运行单元测试。我们最初在标题中遇到错误。 我已经设法将问题隔离开来。 using (TransactionScope scope1 = n
我有一个注册页面,基本上我需要将数据插入到 4 个表中。我是 PDO 的新手,对某些事情感到困惑。 基本上,如果任何插入失败,我不想向数据库中添加任何内容,这看起来很简单。 我的困惑是,我需要首先在我
我使用枢轴点进行交易。我在屏幕指示器上使用以下指标“CM_Pivots_Filtered”、“Pivots”、“CD_PivotR”和“CM_Gaps_Intra-Day_V2.1”。这些枢轴工作得很
我正在努力解决 Sonar 问题: squid:S2229 "Methods should not call same-class methods with incompatible "@Transa
在我的 Controller 中,我有一些类似的代码... ... if user.save something = Something.where("thing = ?", thing)
我使用 StoreKit 进行应用内购买。我发现当用户按下“取消”按钮时,API 的行为很奇怪。 例如,如果我在“确认您的应用内购买”屏幕上按“取消”,我会收到一个带有 error.code == S
AppStore 在自动续订自动续订订阅时是否会发出交易?如果是这样,如果应用程序将自己设置为观察者,那么下次应用程序加载时是否可以可靠地检测到它: [[SKPaymentQueue defaultQ
我正在研究 EMV 技术,并寻找终端和发行者之间的通信(请求/响应)以进行授权/在线 PIN 检查。 我知道离线数据验证仅在终端上进行检查,然后终端将数据发送给发行者。我想知道授权过程需要发送哪些数据
我是一名优秀的程序员,十分优秀!