我无法从asp.net(C#)网站发送邮件 [英] I am not able to send mail from the asp.net(C#) website

查看:108
本文介绍了我无法从asp.net(C#)网站发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

安全例外



描述:应用程序尝试执行安全策略不允许的操作。要授予此应用程序所需的权限,请与您的系统管理员联系或在配置文件中更改应用程序的信任级别。



异常详细信息:System.Security.SecurityException:请求类型'System.Net.Mail.SmtpPermission,System,Version = 4.0.0.0,Culture =的权限中性,PublicKeyToken = b77a5c561934e089'失败。



来源错误:





第34行:SmtpClient客户端=新的SmtpClient();< br /> 
第35行:client.Credentials = new System.Net.NetworkCredential(abc@gmail.com, 1234);< br />
第36行:client.Port = 587;< br />
第37行:client.Host =smtp.gmail.com;< br / >
第38行:client.EnableSsl = true;







我有一个表格哪三个文本框和一个按钮,当我点击按钮然后发送邮件。



我使用此代码。



 protected void Button1_Click(object sender,EventArgs e)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage ();
mail.To.Add(platinumrealtee@gmail.com);
mail.From = new MailAddress(TextBox1.Text,Platinum Realtee的要求,System.Text.Encoding.UTF8);
mail.Subject = TextBox2.Text;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = TextBox4.Text;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient客户端=新的SmtpClient();
client.Credentials = new System.Net.NetworkCredential(abc@gmail.com,1234);
client.Port = 587;
client.Host =smtp.gmail.com;
client.EnableSsl = true;
尝试
{
client.Send(mail);
Page.RegisterStartupScript(UserMsg,< script> alert('Successfully Send ...'); if(alert){window.location ='SendMail.aspx';}< / script> );
}
catch(exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while(ex2!= null)
{
errorMessage + = ex2.ToString();
ex2 = ex2.InnerException;
}
Page.RegisterStartupScript(UserMsg,< script> alert('Sending Failed ...'); if(alert){window.location ='SendMail.aspx';}< ; /脚本>中);
}
}

解决方案

错误信息非常清楚 - 甚至告诉你该做什么!

应用程序尝试执行安全策略不允许的操作。要授予此应用程序所需的权限,请与系统管理员联系或更改配置文件中应用程序的信任级别。

所以请谈谈托管服务...


邮件服务器似乎阻止了您用来发送电子邮件的端口。也许您需要尝试使用端口465 - 如何配置Gmail SMTP设置 [ ^ ]

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

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

Source Error:


Line 34:     SmtpClient client = new SmtpClient();<br />
Line 35:     client.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "1234");<br />
Line 36:     client.Port = 587;<br />
Line 37:     client.Host = "smtp.gmail.com";<br />
Line 38:     client.EnableSsl = true;




I have one form in which three text box and one button,when i click on the button then mail is send.

I used this code for this.

protected void Button1_Click(object sender, EventArgs e)
    {
      System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    mail.To.Add("platinumrealtee@gmail.com");
    mail.From = new MailAddress(TextBox1.Text, "Requirement of Platinum Realtee", System.Text.Encoding.UTF8);
    mail.Subject = TextBox2.Text;
    mail.SubjectEncoding = System.Text.Encoding.UTF8;
    mail.Body = TextBox4.Text;
    mail.BodyEncoding = System.Text.Encoding.UTF8;
    mail.IsBodyHtml = true;
    mail.Priority = MailPriority.High;
    SmtpClient client = new SmtpClient();
    client.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "1234");
    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    try
    { 
        client.Send(mail);
        Page.RegisterStartupScript("UserMsg", "<script>alert('Successfully Send...');if(alert){ window.location='SendMail.aspx';}</script>");
    }
    catch (Exception ex)
    {
        Exception ex2 = ex;
        string errorMessage = string.Empty;
        while (ex2 != null)
        {
            errorMessage += ex2.ToString();
            ex2 = ex2.InnerException;
        }
        Page.RegisterStartupScript("UserMsg", "<script>alert('Sending Failed...');if(alert){ window.location='SendMail.aspx';}</script>");
    }
}

解决方案

The error message is pretty clear -and even tells you what to do!
"The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file."
So talk to the hosting service...


The mail server seems to be blocking the port you are using to send the email. Maybe you need to try by using port 465 - How to configure Gmail SMTP settings [^].


这篇关于我无法从asp.net(C#)网站发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆