在ASP.NET中使用Twilio发送邮件和SMS

时间:2017-04-08 12:35:28

标签: c# asp.net webforms twilio postback

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

在手机中向用户发送验证码

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

但我的代码无效

Verify.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
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

if(!IsPostBack)

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

using the debugger可以轻松找到代码占用意外路径时出现的这类错误,以便逐步执行代码或添加日志记录。