我正在嘗試創建一個電子郵件頁面部分,並且我在下面的代碼中找到了一篇文章和其中一個答案,但仍然說發送郵件失敗,我幾乎找不到原因?。我需要幫助才能找到補救措施,以解釋爲什麼我的代碼無法發送電子郵件。在這裏我的代碼供您參考。請指教......謝謝。發送郵件失敗
protected void Button1_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.live.com"; //Im not sure about this,I just copy it from the article
client.Port = 4548; //This is my ASP.Net Development Server Port,Im not sure also if this is correct.
client.Credentials = new System.Net.NetworkCredential(
@"email_account",
@"email_password"); //Im not sure about this code if this correct, I just copy it
client.EnableSsl = true;
// create message
MailMessage message = new MailMessage();
message.From = new MailAddress(TextBox4.Text, "Mackmellow"); //Textbox4 is my email address
message.To.Add(new MailAddress(TextBox1.Text, "Mackmellow")); // Textbox1 is the email add I want to send
message.Subject = TextBox2.Text; //Textbox2 is my subject
message.Body = TextBox3.Text; // Textbox3 is my message
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// send message
try
{
client.Send(message);
}
catch (SmtpException ex)
{
Response.Write(ex.Message);
}
finally
{
// Clean up.
message.Dispose();
}
請糾正我的代碼並告訴我我失蹤了什麼?在此先感謝...
的就是你得到實際的錯誤信息(和/或內部異常)? 「發送郵件失敗」就像告訴汽車技工「汽車不會運行」一般。更多細節總是一件好事:) – Tim
失敗是否有錯誤? –
您必須確保您的電子郵件(from_email)屬於電子郵件提供商(在您的情況下爲「smtp.live.com」),否則它將失敗,並且您的端口不正確,應該是live.com接受的任何內容smtp on – Alex