Email sending code working on localhost but not on IONOS server

835 views Asked by At

I have the below script in ASP.NET as my "Contact Us" page. The script works in VS (localhost) but not working after deployed on the IONOS server.

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("[email protected]");



                //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("[email protected]", "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;
        }
    }
1

There are 1 answers

0
Kevin Taheri On

I found the solution.
You just need to edit the below script (your email address and password).

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("[email protected]");

            //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("[email protected]", "XXPasswordXX");
            smtp.EnableSsl = true;

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

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

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

    else
    {
        Label2.Visible = true;
    }
}