使用smtp傳送郵件是否有錯誤訊息可用?

jeff air 20 信譽點數
2024-03-27T06:02:52.8233333+00:00

以下是在網路中查詢到並可順利執行的smtp寄信程式, 但如果收件人信箱有誤或密碼等有誤, 程式仍會顯示 "finish" , 想請教眾高手, 如何獲得error message , smtp.Send(em)並不會傳回值. 先謝謝回覆者, 感謝


Try

        '***寫信***

        Dim em As Net.Mail.MailMessage = New Net.Mail.MailMessage()

        em.From = New Net.Mail.MailAddress("寄件信箱地址", "名稱", Encoding.UTF8)

        em.[To].Add(New Net.Mail.MailAddress("收件者信箱地址"))    '收件者

        em.Subject = "go"     '信件主題 

        em.SubjectEncoding = Encoding.UTF8

        em.Body = "ok"            '內容 

        em.BodyEncoding = Encoding.UTF8

        em.IsBodyHtml = True      '信件內容是否使用HTML格式

        '***寄信設定***

        Dim smtp As Net.Mail.SmtpClient = New Net.Mail.SmtpClient()

        '登入帳號認證

        smtp.Credentials = New Net.NetworkCredential("寄信信箱", "密碼")

        smtp.Port = 587 '465  

        smtp.EnableSsl = True   '啟動SSL 

        smtp.Host = "smtp-mail.outlook.com"   'SMTP伺服器

        'smtp.Host = "smtp.gmail.com"   'SMTP伺服器

        '***寄出***

        smtp.Send(em)

    Catch ex As Exception

        Result.text = ex.ToString()

    End Try

    'Result.text = "Finish"
ASP.NET
ASP.NET
.NET Framework 中的一組技術,用於建置 Web 應用程式和 XML Web 服務。
5 個問題
0 則留言 沒有留言
{count} 則投票

1 個回答

排序依據: 最實用
  1. BruceChen 0 信譽點數 MVP
    2024-03-27T08:55:30.82+00:00

    Code <--> SMTP Server <--> ...

    程式是對 SMTP Server,SMTP Server正確收到你的訊息並且回應接收完成的狀態後,你的程式就結束了。後續 SMTP Server 的處理的狀態並不會回應給你的程式碼。

    建議可以多了解一下 SMTP Server 的運作流程