简体   繁体   中英

error with sending mail vb.net

i'm having a problem with sending mail. my code is so easy as below. when this code starts it gives the error:

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

when i searched for this error, they recommend changing web.config trust full setting or port from 587 to 25 and other stuff, but none work. what else could it be?

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

End Sub

Use this code, work perfect for me :

Dim iphost As IPHostEntry = Dns.GetHostByName(Dns.GetHostName())
    Dim Emailmessage As New MailMessage()
    Emailmessage.From = New MailAddress("from_Email@hotmail.com")
    Emailmessage.To.Add("To_Email@hotmail.com")
    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("your_email@hotmail.com.com", "Password")
    smtp.Send(Emailmessage)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM