Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here I attached my coding...


MailMessage msg1 = new MailMessage();
string from = "Email address";
string ps = "Email Password";
string sub = TextBox1.Text + "Welcome to the world of Travel!";

msg1.From = new MailAddress(from, "Way2hotel");//sender
msg1.To.Add(new MailAddress(TextBox5.Text));//receiver
msg1.Subject = sub;
msg1.Body = "Congratulations,Thanks for Signing up.Celebrate increased growth in revenue.Enjoy your Bookings,Offers and promos with way2hotel.";
msg1.IsBodyHtml = true;
if (RadioButtonList3.Text == "Yes")
{
txt_mailpath.Text = "~/PDF/WebsiteQuotation.pdf";
System.Net.Mail.Attachment attachment;
//Response.Write(txt_mailpath);
attachment = new System.Net.Mail.Attachment(txt_mailpath.Text);
msg1.Attachments.Add(attachment);
}

SmtpClient smtp = new SmtpClient();
smtp.Host = "HOST NAME";
smtp.Port = PORT NUMBER;

smtp.Credentials = new NetworkCredential(from, ps);//sender mail and password
re.Visible = true;
smtp.EnableSsl = true;

But I got Error Like
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.


If any body know put on your answers....
Posted

Use File Upload Control

C#
MailMessage msg1 = new MailMessage();
string from = "Email address";
string ps = "Email Password";
string sub = TextBox1.Text + "Welcome to the world of Travel!";

msg1.From = new MailAddress(from, "Way2hotel");//sender
msg1.To.Add(new MailAddress(TextBox5.Text));//receiver
msg1.Subject = sub;
msg1.Body = "Congratulations,Thanks for Signing up.Celebrate increased growth in revenue.Enjoy your Bookings,Offers and promos with way2hotel.";
msg1.IsBodyHtml = true;
if (RadioButtonList3.Text == "Yes")
{
if (inpAttachment1.PostedFile != null)
{

HttpPostedFile attFile = inpAttachment1.PostedFile;
 int attachFileLength = attFile.ContentLength; 

 if (attachFileLength > 0)
 {

 strFileName = Path.GetFileName(inpAttachment1.PostedFile.FileName);

 inpAttachment1.PostedFile.SaveAs(Server.MapPath(strFileName));  

 MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
  
 msg1.Attachments.Add(attach);
 
 attach1 = strFileName;
 }
SmtpClient smtp = new SmtpClient();
smtp.Host = "HOST NAME";
smtp.Port = PORT NUMBER;

smtp.Credentials = new NetworkCredential(from, ps);//sender mail and password
re.Visible = true;
smtp.EnableSsl = true;
}
 
Share this answer
 
this is happen because your application are running under trust level medium ...In medium trust level does`t allow SMTP and it has lot of restriction.
Only one way you can move out from this situation is that you make it change trust level medium to full in your web.config file.
<system.web>
<trust level="Full">
 
Share this answer
 
I have the same problem I was using gmail account
for that I was using port no 587
i changed it to 25 the problem is solved
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900