user4451265
user4451265

Reputation:

Configuring web.config to send emails?

I'm about to deploy my first ASP.NET website to Azure: There is a contact form in my website with 4 text boxes (email, name, subject and body) and send button. I use this configuration to send emails:

 <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="Mohamed &lt;[email protected]&gt;">
        <network host="Smtp.live.com" port="587" enableSsl="true" userName="[email protected]" password="mypassword"/>
      </smtp>
    </mailSettings>
  </system.net>

And this is the handler for the send button

protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string fileName = Server.MapPath("~/App_Data/Message.txt");
            string mailBody = File.ReadAllText(fileName);

            mailBody = mailBody.Replace("##Name##", TextBoxName.Text);
            mailBody = mailBody.Replace("##Email##", TextBoxEmail.Text);
            mailBody = mailBody.Replace("##Subject##", TextBoxSubject.Text);
            mailBody = mailBody.Replace("##Body##", TextBoxBody.Text);
            MailMessage visitorMessage = new MailMessage();
            visitorMessage.Subject = "New Message: " + TextBoxSubject.Text;
            visitorMessage.Body = mailBody;

            visitorMessage.From = new MailAddress(TextBoxEmail.Text, TextBoxName.Text);
            visitorMessage.To.Add(new MailAddress("[email protected]", "Mohamed"));
            visitorMessage.ReplyToList.Add(new MailAddress(TextBoxEmail.Text));
            SmtpClient mySmtpClient = new SmtpClient();
            mySmtpClient.Send(visitorMessage);
            LabelIRespond.Visible = true;

        }
    }

I use these settings for the IIS server, obviously it's not working with production server. Please tell me what changes should I make to enable this function?

EDIT when I send message from the form, The process terminates, and I receive a privacy warning to my email.

Upvotes: 1

Views: 1373

Answers (1)

Kritner
Kritner

Reputation: 13765

Here is a blog post on sending emails using windows azure:

http://blog.smarx.com/posts/emailtheinternet-com-sending-and-receiving-email-in-windows-azure

TLDR - don't use your personal email on a cloud host to send emails, it's likely these services black list cloud host IP address ranges to help prevent spam. Get a legit SMTP server set up either on prem, in the cloud, or use a third party service as pointed out in the post linked.

Upvotes: 1

Related Questions