Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

When i am trying to send a mail through webservice locally it gets send. But when i am using it as a reference in another project. I get this issue :

C#
{[{"Result" : "System.Security.SecurityException : System.Security.Permissions.SecurityPermission
   at EASendMAil.6y.2l(UInt32 6m, Byte[] 6m, Byte[] 6m)
   at EASendMail.4k.2N(string 6m, MailStyle 6m, Int64 6m, Int32 6m)
   at EASendMail.SmtpMail.3D(MailStyle 6m)
   at EASendMail.SmtpMail..ctor(String v)
   at EASendMail.SendMail.sendMail(String ToEmail, String password) at...
 
   The type of the first permission that failed was :
    System.Security.Permissions.SecurityPermission
    The Zone of the assembly that failed was : MyComputer"}]}

What should be done to solve this problem?

The code for SendEmail.cs : (The dll is included in the references)

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EASendMail;
using System.Net.Mail;

namespace EPWSBL
{
   public class SendEmail
    {
        public void sendMail(String ToEmail, String password)
        {
            SmtpMail oMail = new SmtpMail("TryIt");

            EASendMail.SmtpClient oSmtp = new EASendMail.SmtpClient();

            // Set sender email address, please change it to yours
            oMail.From = "abc@gmail.com"

            // Set recipient email address, please change it to yours
            oMail.To = ToEmail;

            // Set email subject
            oMail.Subject = " Password";

            // Set email body
            oMail.TextBody = " Hi, your user name is : " + ToEmail + " and password is : " + password;

            // Your SMTP server address
            SmtpServer oServer = new SmtpServer("smtp.gmail.com");

            // User and password for ESMTP authentication, if your server doesn't require
            // User authentication, please remove the following codes.            
            oServer.User = "abc@gmail.com";
            oServer.Password = "123456";

            // If your smtp server requires SSL connection, please add this line
            oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

            try
            {
                Console.WriteLine("Start to send email ...");
                oSmtp.SendMail(oServer, oMail);
                Console.WriteLine("Email was sent successfully!");
            }
            catch (Exception ep)
            {
                Console.WriteLine("Failed to send email with the following error:");
                Console.WriteLine(ep.Message);

            }
            Console.ReadLine();

        }
    }
}

The code in Business Logic is:

C#
public string wsForgotPasswordForCoordinator(string CoordinatorEmailID)
        {
            string emailid = CoordinatorEmailID;
            try
            {
                string password = coordinator.getCoordinatorPassword(emailid).ToString();
                if (password.Equals(null))
                {
                    //return "This EmailID Does Not Exist";
                    return "False";
                }
                else
                {
                    SendEmail send = new SendEmail();
                    send.sendMail(CoordinatorEmailID, password);
                    //return "Password Send Successfully To Your EmailID";
                    return "True";
                }
            }
            catch (Exception e)
            {
                return e.ToString();
                throw;
            }
        }


The webservice class contains :


C#
public string wsChangePasswordForCoordinator(string str)
        {
            String CoordinatorEmailID = string.Empty;
            String OldPassword = string.Empty;
            String NewPassword = string.Empty;

            String[] para = str.Split('{', '}', '[', ']', ':', '"');

            for (int i = 0; i < para.Length; i++)
            {
                if (para[i].Equals("CoordinatorEmailID"))
                    CoordinatorEmailID = para[i + 3];
                else if (para[i].Equals("OldPassword"))
                    OldPassword = para[i + 3];
                else if (para[i].Equals("NewPassword"))
                    NewPassword = para[i + 3];
            }

            EPWSBL.Coordinator CBL = new EPWSBL.Coordinator();
            string result = CBL.wsChangePasswordForCoordinator(CoordinatorEmailID, OldPassword, NewPassword);
            return result;
        }

The webservice contains :

[WebMethod]
        public string wsForgotPasswordForCoordinator(string strCoordinatorData)
        {
            ManageProfile MP = new ManageProfile();

            string result = MP.wsForgotPasswordForCoordinator(strCoordinatorData);

            string JSONresult = "{[{\"Result\":\"" + result + "\"}]}";

            return JSONresult;
        }

This error occur while at this statement:

C#
MessageBox.Show(abc.wsForgotPasswordForCoordinator("{{\"CoordinatorEmailID\":\"abc@gmail.com\"}}"));

The error is :
{[{"Result" : "System.Security.SecurityException : System.Security.Permissions.SecurityPermission
   at EASendMAil.6y.2l(UInt32 6m, Byte[] 6m, Byte[] 6m)
   at EASendMail.4k.2N(string 6m, MailStyle 6m, Int64 6m, Int32 6m)
   at EASendMail.SmtpMail.3D(MailStyle 6m)
   at EASendMail.SmtpMail..ctor(String v)
   at EASendMail.SendMail.sendMail(String ToEmail, String password) at...

   The type of the first permission that failed was :
    System.Security.Permissions.SecurityPermission
    The Zone of the assembly that failed was : MyComputer"}]}


This project is containing the service reference of my webservice.

Thanks in advance............
Posted
Updated 27-Jun-12 0:23am
v5
Comments
Sandeep Mewara 27-Jun-12 4:46am    
There would be more to this error telling you exactly on what/where the permission issue is. Look for it and solve. If you are unsure, share the full error details to get some more insight.
Herman<T>.Instance 27-Jun-12 5:50am    
please show the code the leads to this exception
Herman<T>.Instance 27-Jun-12 6:33am    
do you change that password in a database or in active directory? And has the service user enought rights to do so ?
rp786 27-Jun-12 6:37am    
The database is online. Yes user has the rights to make the changes in his password.

1) Check your code for SendEmail.cs in another simple application so you will confirm exactly where the problem is ,like following code is working fine in a normal form.add the missing parameters to your code.

C#
MailMessage mail = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
if ((mailSettings != null))
{
    mail.To.Add(new MailAddress(Properties.Settings.Default.SendExceptionLogFromMailId));
    mail.IsBodyHtml = true;
    mail.Subject = Constants.ERROR_LOG_MAIL_SUBJECT;
    mail.Body = Constants.ERROR_LOG_MAIL_BODY;
    mail.From = new System.Net.Mail.MailAddress(Properties.Settings.Default.SendExceptionLogFromMailId);
    Attachment objAttachment = new Attachment(logFileZipLocation);
    mail.Attachments.Add(objAttachment);
    smtpClient.UseDefaultCredentials = false;
    smtpClient.DeliveryMethod = mailSettings.Smtp.DeliveryMethod;
    smtpClient.Credentials = new NetworkCredential("@gmail_id", "Password");
    smtpClient.EnableSsl = true;
    smtpClient.Host = "smtp.gmail.com";
    smtpClient.Port = 587;
    smtpClient.Send(mail);
}


2) Check manually if the mail provider is block or not in your firm.
 
Share this answer
 
v2
You code must be running under company's intranet.
So, it will show this type of issue.
 
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