net网站,其中包含发送电子邮件的表单。当我在本地测试此表单并按提交时,一切正常,但是当我在Microsoft Azure上托管网站时,每次都无法发送电子邮件。
Http代码:
<form method="post" enctype="multipart/form-data" asp-controller="Front" asp-action="Index">
<div id="namebox">
<label asp-for="Name">Name:</label>
<input asp-for="Name" type="text" placeholder="ENTER YOUR NAME">
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div id="emailbox">
<label asp-for="Email">Email:</label>
<input asp-for="Email" type="email" placeholder="ENTER YOUR EMAIL ADDRESS">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div id="phonebox">
<label asp-for="Phone">Phone:</label>
<input asp-for="Phone" type="tel" placeholder="ENTER YOUR PHONE NUMBER">
<span asp-validation-for="Phone" class="text-danger"></span>
</div>
<div id="enquirybox">
<label asp-for="Enquiry">Enquiry:</label>
<select asp-for="Enquiry" asp-items="Model.PossibleEnquiry"></select>
</div>
<label asp-for="Message">Your Message:</label>
<textarea asp-for="Message" placeholder="ENTER YOUR MESSAGE"></textarea>
<span asp-validation-for="Phone" class="text-danger"></span>
<input type="submit" value="SUBMIT">
</form>
电子邮件发件人代码:
try
{
SmtpClient client = new SmtpClient("smtp.office365.com", 587)
{
UseDefaultCredentials = false,
EnableSsl = true,
Credentials = new NetworkCredential(-->account, -->password, "office365.com")
};
MailMessage mailMessage = new MailMessage
{
From = new MailAddress(-->account),
Subject = subject,
Body = message
};
mailMessage.To.Add(new MailAddress(-->email));
client.Send(mailMessage);
}
catch
{
_logger.LogError("Email message failed to send.");
}