【问题标题】:Email sending code working on localhost but not on IONOS server电子邮件发送代码在 localhost 但不在 IONOS 服务器上工作
【发布时间】:2021-05-02 03:36:24
【问题描述】:

我在 ASP.NET 中有以下脚本作为我的“联系我们”页面。该脚本在 VS (localhost) 中运行,但在部署到 IONOS 服务器后无法运行。

protected void send_Click(object sender, EventArgs e)
    {
            try
            {
                MailMessage Msg = new MailMessage();
                //Sender e-mail address.
                Msg.From = new MailAddress(txtEmail.Text);
                //Recipient e-mail address.
                Msg.To.Add("XXXXXX@gmail.com");



                //Meaages Subject
                Msg.Subject = "A New Email from Contact Us page";

                StringBuilder sb = new StringBuilder();
                sb.Append("Subject: " + txtSubject.Text + "\r\n");
                sb.Append("Message: " + txtBody.Text + "\r\n");

                if (FileUpload1.HasFile)
                {
                    string FileName = FileUpload1.PostedFile.FileName;
                    Msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));

                }

                Msg.Body = sb.ToString();

                // SMTP server IP.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;

                smtp.Timeout = 10000;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                
                smtp.Credentials = new System.Net.NetworkCredential("XXXXXXXXXX@gmail.com", "XXX-MyPassword-XXX");
                smtp.EnableSsl = true;

                Msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                smtp.Send(Msg);

                //Mail Message
                Response.Write("<Script>alert('Thanks for contact us, our team will be contact you as soon as possible')</Script>");

            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "');</script>");
            }
        }

        else
        {
            Label2.Visible = true;
        }
    }

【问题讨论】:

    标签: localhost mail-form ionos


    【解决方案1】:

    我找到了解决方案。
    您只需要编辑以下脚本(您的电子邮件地址和密码)。

    protected void send_Click(object sender, EventArgs e) { 尝试 { MailMessage 消息 = 新的 MailMessage(); //发件人电子邮件地址。 Msg.From = 新的 邮件地址(txtEmail.Text); //收件人电子邮件地址。 Msg.To.Add("XXXXXX@XXX.com");

                //Meaages Subject
                Msg.Subject = "A New Email from Contact Us page";
    
                StringBuilder sb = new StringBuilder();
                sb.Append("Subject: " + txtSubject.Text + "\r\n");
                sb.Append("Message: " + txtBody.Text + "\r\n");
    
                if (FileUpload1.HasFile)
                {
                    string FileName = FileUpload1.PostedFile.FileName;
                    Msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));
    
                }
    
                Msg.Body = sb.ToString();
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.ionos.com";
                smtp.Port = 25;
                smtp.Timeout = 10000;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
    
                smtp.Credentials = new System.Net.NetworkCredential("XXXXX@XXX.com", "XXPasswordXX");
                smtp.EnableSsl = true;
    
                Msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                smtp.Send(Msg);
    

    //邮件留言 Response.Write("alert('感谢您与我们联系,我们的团队将尽快与您联系')");

            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "');</script>");
            }
        }
    
        else
        {
            Label2.Visible = true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-25
      • 1970-01-01
      • 2019-09-20
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 2021-09-27
      相关资源
      最近更新 更多