【问题标题】:Sending Mail and SMS using Twilio in ASP.NET在 ASP.NET 中使用 Twilio 发送邮件和 SMS
【发布时间】:2017-09-03 18:50:19
【问题描述】:

我想在用户选择电子邮件时向用户发送验证码,然后在 UserEmail 中向用户发送验证码,如果用户选择电话,则使用

向用户发送验证码

在我的代码中,我采用了一个 DropDownList(with AutoPostBack=true) 使用该用户可以选择他们的选择

但我的代码不起作用

验证.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{ }

protected void dpd_SelectedIndexChanged(object sender, EventArgs e)
{
    String email = Request.QueryString["email"];
    String phn = "+91" + Request.QueryString["phn"];
    if (!IsPostBack)
    {
        if (dpd.SelectedValue.Equals("Email"))
        {
            var chars = 
       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var stringChars = new char[8];
            var random = new Random();
            for (int i = 0; i < stringChars.Length; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }
            var finalString = new String(stringChars);
            String mailbody = @"

<html><head></head><body><div style=background:#000099;border:1px solid 
#000099;padding: 10px;><img src='+logo.png+' /></div><br /><br /><hr 
style=color:#FF0000><p><strong>Hello User</strong>,<br />  <font face=Arial, 
Helvetica, sans-serif color=#66CCFF size=+1>Please Verify Your your account 
information</font></p><p>&nbsp;</p><p>Thanks For Joining Us...<br>  
<strong>Gaurav Bothra</strong></p></body></html>";

            // Specify the from and to email address
            MailMessage mailMessage = new MailMessage("keystroke165@gmail.com", email);
            // Specify the email body
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = mailbody;
            // Specify the email Subject
            mailMessage.Subject = "Welcome to THERAPEUTIC";

            // No need to specify the SMTP settings as these 
            // are already specified in web.config
            SmtpClient smtpClient = new SmtpClient();
            // Finall send the email message using Send() method
            smtpClient.Send(mailMessage);
        }
        if (dpd.SelectedValue.Equals("Phone"))
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var stringChars = new char[8];
            var random = new Random();

            for (int i = 0; i < stringChars.Length; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }

            var finalString = new String(stringChars);

            var accountSid = "MYSIDXXXXX";
            // Your Auth Token from twilio.com/console
            var authToken = "MYTOKENXXXX";

            TwilioClient.Init(accountSid, authToken);

            var message = MessageResource.Create(
                to: new PhoneNumber(phn),
                from: new PhoneNumber("MYTwilioNO"),
                body: "Hello User????\nPlease Verify Your your account information\nyour otp is\n" + finalString);
            ///End
        }
    }
}

【问题讨论】:

  • 描述不工作。你需要说出它在做什么,提及任何错误,描述你尝试过的其他事情。
  • 没有错误但是邮件和短信收不到
  • 您是否使用调试器单步执行代码以验证它通过您的代码采用了正确的路径?
  • 我在代码中调试我的代码没有逻辑问题
  • 真的吗?当您看到代码逐行执行时,它采用了您期望的路径?我不相信你。

标签: c# asp.net webforms twilio postback


【解决方案1】:

此事件通常仅在 DropDownList 的选定索引更改并引发调用 dpd_SelectedIndexChanged 的回发时触发。但是,您的大部分逻辑都包含在:

if(!IsPostBack)

因此永远无法达到发送电子邮件或短信的逻辑。您需要删除 if 语句。

using the debugger 可以很容易地发现代码采用意外路径时出现的这类错误,以单步执行您的代码或添加日志记录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    相关资源
    最近更新 更多