Unable to send email from ASP.Net page

Discussion in 'Email' started by Brad Ashforth, Mar 26, 2012.

  1. I created a standard "contact us" form, using the sample code on WinSite. I kept getting errors, so I tried different settings (incl port). Although I had created a mail account "[email protected]" and wanted to send the mail to that account, WinSite support suggested I authenticate using the postmaster account. Still no luck.
    My current code below has debug output as well.
    protected void btnSend_Click(object sender, EventArgs e)
    {
    try
    {
    //code from Winhost
    //create the mail message
    MailMessage mail = new MailMessage();
    //set the addresses
    mail.From = new MailAddress(txtFrom.Text.Trim());
    mail.To.Add("[email protected]");
    mail.To.Add(txtFrom.Text.Trim());
    //set the content
    mail.Subject = "SupportRequest/Comment: " + txtSubject.Text.Trim();
    mail.Body = txtMessage.Value.Trim();

    NetworkCredential Credentials = new NetworkCredential();
    Credentials.Domain = "ComputerWhizard.com";
    lblStatus.Text = lblStatus.Text + " set network credentials domain: " + Credentials.Domain.ToString();
    Credentials.UserName = "[email protected]";
    lblStatus.Text = lblStatus.Text + " setting network credentials username=" + Credentials.UserName.ToString();
    Credentials.Password = "********"; //rem out password
    lblStatus.Text = lblStatus.Text + " setting network credentials username=" + Credentials.Password.ToString();

    SmtpClient smtp = new SmtpClient("mail.computerwhizard.com");
    smtp.Credentials = Credentials;
    smtp.Host = "mail.computerwhizard.com";
    lblStatus.Text = lblStatus.Text + " smpt.host=" + smtp.Host.ToString();
    //smtp.Port = 587;
    //lblStatus.Text = lblStatus.Text + " smpt.Port=" + smtp.Port.ToString();
    lblStatus.Text = lblStatus.Text + " sending mail";
    smtp.Send(mail);
    //smtp.Send(txtFrom.Text.Trim(), "[email protected]; " + txtFrom.Text.Trim(), "SupportRequest/Comment: " + txtSubject.Text.Trim(), txtMessage.Value.Trim());
    lblStatus.Text = lblStatus.Text + " Mail Sent";
    }
    catch (Exception ex)
    {
    lblStatus.Text = ex.ToString();
    }
    }

    The exception is:
    System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) at System.Security.CodeAccessPermission.Demand() at System.Net.NetworkCredential.get_Domain() at CWAspNetWebApp.About.btnSend_Click(Object sender, EventArgs e) in C:\Users\Brad\Documents\My Web Sites\CWAspNetWebApp\CWAspNetWebApp\About.aspx.cs:line 36 The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.EnvironmentPermission The Zone of the assembly that failed was: MyComputer

    I saw some posts with Mail issues from ASP.net, but not this error. Any suggestions?
     
    Last edited by a moderator: Oct 14, 2015
  2. Thank you Allegro!

    That was it.
     

Share This Page