2017-09-09 71 views
0

我在發送郵件時遇到問題。我的代碼非常簡單,如下所示。 代碼啓動時,它提供了錯誤:發送郵件時出錯vb.net

Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

當我搜索這個錯誤,他們建議改變的web.config充分的信任設置或端口從587到25和其他的東西,但沒有工作。它還能是什麼?

Sub MailGonder() 
    Dim SmtpServer As New SmtpClient() 
    Dim mail As New MailMessage() 
    SmtpServer.Host = "mail.excelinefendisi.com" 
    SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "mypassword") 
    SmtpServer.Port = 587 
    mail.From = New MailAddress("[email protected]") 
    mail.To.Add("[email protected]") 
    mail.Subject = "Test Mail" 
    mail.Body = "This is for testing SMTP" 
    SmtpServer.EnableSsl = True 
    SmtpServer.Send(mail) 

End Sub 
+0

請註明完整例外。 –

+0

請求'System.Net.Mail.SmtpPermission,System,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'類型的權限失敗。 – ExcelinEfendisi

回答

0

使用此代碼,爲我工作完美:

Dim iphost As IPHostEntry = Dns.GetHostByName(Dns.GetHostName()) 
    Dim Emailmessage As New MailMessage() 
    Emailmessage.From = New MailAddress("[email protected]") 
    Emailmessage.To.Add("[email protected]") 
    Emailmessage.Subject = "tTest Mail" 
    Emailmessage.Body = ("This is for testing SMTP") 
    Dim smtp As New SmtpClient("smtp.live.com") 
    smtp.Port = 587 
    smtp.EnableSsl = True 
    smtp.Credentials = New System.Net.NetworkCredential("[email protected]", "Password") 
    smtp.Send(Emailmessage)