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

Sending Mail and SMS using Twilio in ASP.NET

我想在 select 发送验证码给用户时发送电子邮件,然后在 UserEmail 中向用户发送验证码,如果用户 Select Phone 然后向用户发送验证码在他的 phone 中使用

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

但是我的代码不起作用

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

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

if(!IsPostBack)

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

当代码采用意外路径时,这些类型的错误可以通过 using the debugger 单步执行代码或添加日志记录轻松找到。