Permission error when trying to send emails from WCF service.

Discussion in 'Email' started by wassimmansour, Dec 26, 2010.

  1. Hi everybody,
    I am reveiving this error when I try to send an email from a WCF service:
    "Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
    When I use the same code from my local machine the email is sent successfully.
    I am using the email account I got from Winhost and everything seems OK until I upload the service to the website.
    What should I do to make it work?
     
    Last edited by a moderator: Oct 14, 2015
  2. Ray

    Ray

    Last edited by a moderator: Oct 14, 2015
  3. Hi,
    Thanks for the reply.
    I already have the website running under full trust. I tried setting that using IIS Manager and the web.config and still, the problem persists.
    Here's the code I'm using, it's pretty much as simple as it gets:

    Code:
    MailMessage mailMessage = new MailMessage("FromAddress", "ToAddress");
    mailMessage.Body = "messageBody";
    mailMessage.Subject = "Note From Website!";
    // ---
    SmtpClient theClient = new SmtpClient("Host");
    theClient.Port = 587;
    theClient.UseDefaultCredentials = false;
    System.Net.NetworkCredential theCredential = new System.Net.NetworkCredential ("UserName", "Password");
    theClient.Credentials = theCredential;
    theClient.Send(mailMessage);
    
     
  4. Ray

    Ray

    The error message is synonymous for s trust level. Try setting the trust level to Medium save it, then reset it back to Full. I've seen sometimes that the trust level settings doesn't fully set in the account.
    Also when you are passing the SMTP authentication, make sure you hard code it in and for now do not pass any values or variables for the SMTP authentication.
     
  5. I am using the email account I have setup with Winhost to send these messages. Once I changed the account to my Gmail account everything started working correctly.
    Is there any special settings I need to take care of to use my Winhost email account to send these messages?
     
  6. Ray

    Ray

    No there isn't. But like I said sometime the trust level doesn't set correctly and either recycling your application pool or setting the trust level to Medium, saving it, and then setting it back to Full and then saving it usually does the trick.
     
  7. Thanks a lot... I was trying playing with the Trust Level all night yesterday and it didn't work, but I just recycled the Application Pool and everything started to work fine.
     

Share This Page