System.Net.Mail works as localhost but when I add it to my web server it does not

952 Views Asked by At

This program works when I "Debug" in VWD 2010 as localhost. When I insert it into my webserver my Try...Catch get this error:

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

Here is my code:

Imports System.Net.Mail
Dim msg As New System.Net.Mail.MailMessage
Dim fromAdd As MailAddress = New MailAddress("[email protected]")
msg.[To].Add("[email protected]")
msg.Subject = "Email Works!!!!!!!!"
msg.From = fromAdd
msg.IsBodyHtml = True
msg.Priority = System.Net.Mail.MailPriority.Normal
msg.BodyEncoding = Encoding.Default
msg.Body = "<h1>Body!!!!!!</h1>"
Dim host = Request.Url.Host
Dim smtpClient As New SmtpClient("smtp.email.com", "587")
smtpClient.EnableSsl = True
smtpClient.UseDefaultCredentials = False
smtpClient.Credentials = New System.Net.NetworkCredential("user", "pass")
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
smtpClient.Send(Message)    
1

There are 1 best solutions below

0
On

replace port no for gmail smpt by 25,

it will worke on hosting server too.

Source : http://forums.asp.net/t/1624378.aspx