System.Security.SecurityException: Request for the permission of type 'System.Net.Mail.SmtpPermission, System, 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.Mail.SmtpClient.set_Port(Int32 value)
at feedbackcontrol.SendEmail(String toAddress, String subject, String body) in d:\inetpub\vhosts\platinumrealtee.com\httpdocs\feedbackcontrol.ascx.cs:line 38
The action that failed was: Demand The type of the first permission that failed was: System.Net.Mail.SmtpPermission The Zone of the assembly that failed was: MyComputer
在代码隐藏文件中使用此代码。
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = SendEmail(txtTo.Text, txtsubject.Text, txtMsg.Text);
}
protected string SendEmail(string toAddress, string subject, string body)
{
string result = "Message Sent Successfully..!!";
string senderID = "abc@gmail.com";// use sender’s email id here..
const string senderPassword ="abcd"; // sender password here…
try
{
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com", // smtp server address here…
Port = 590,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
Timeout = 30000,
};
MailMessage message = new MailMessage(senderID, toAddress, subject, body);
smtp.Send(message);
}
catch (Exception ex)
{
result = ex.ToString();
}
return result;
}
答案 0 :(得分:1)
根据你想要的,看看下面的
Send Mail functionality in asp.net
相关邮件代码: -
protected void btnSend_Click(object sender, EventArgs e)
{
MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com");
mm.Subject = txtSubject.Text;
mm.Body = "Name: " + txtName.Text + "<br /><br />Email: " + txtEmail.Text + "<br />" + txtBody.Text;
if (FileUpload1.HasFile)
{
string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
mm.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "xxxxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
lblMessage.Text = "Email Sent SucessFully.";
}
此外,您需要正确添加smtp port
和其他相关信息才能按要求工作
<强>更新强>
由于它无法在服务器上运行,因此您需要在web.config
文件中添加一个设置才能生效。
<configuration>
<system.web>
.....
<trust level="Full" originUrl=""/>
</system.web>
</configuration>
您需要设置trust level set to full
摘自here
答案 1 :(得分:0)
这是另一个例子。请记住,如果smtp.port = 25,它会更好;
try
{
MailMessage msg = new MailMessage ();
MailAddress fromAdd = new MailAddress("fromemail@email.com");
msg.[To].Add("toemail@email.com");
msg.Subject = "Choose Session Members";
msg.From = fromAdd;
msg .IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg .BodyEncoding = Encoding.Default;
msg.Body = "<center><table><tr><td><h1>Your Message</h1><br/><br/></td></tr>";
msg.Body = msg.Body + "</table></center>";
SmtpClient smtpClient = new SmtpClient ("smtp.yourserver.com", "25");
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("yourname@yourserver.com", "password");
smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(msg);
smtpClient.Dispose();
}
catch (exception ex){
Label1.Text = ex.ToString();
}