Friday, September 11, 2009

URI Format Not Supported.

I am Sending a Mail with attachemts for this I have Made A WCF Which works fine when I give the Network path of the file. but I need to host It And when I try to give a URL path It Says. URI format not supported please Help My Code in WCF

public List funSendMail(Mailcls m)
        {
            List result = new List();
            try
            {
                /* Create a new blank MailMessage with the from and to adreesses*/
                System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(m.Sender, m.Receiver);
                /*Checking the condition that the cc is empty or not if not then
                 * include them
                 */
                if (m.Cc != null && m.Cc != string.Empty)
                {
                    mailMessage.CC.Add(m.Cc);
                }
                /*Checking the condition that the Bcc is empty or not if not then
                 * include them
                 */
                if (m.Bcc != null && m.Bcc != string.Empty)
                {
                    mailMessage.Bcc.Add(m.Bcc);
                }
                //Ading Subject to the Mail
                mailMessage.Subject = m.Subject;
                //Adding the Mail Body
                mailMessage.Body = m.Body;

                /* Set the properties of the MailMessage to the
                   values on the form as per the mail is HTML formatted */

                mailMessage.IsBodyHtml = true;


                /* Bigining of Attachment1 process   & 
                   Check the all file for a attachment */

                if ((m.AttachfilesPath != null) && (m.AttachfilesPath.Count > 0))
                {
                    foreach (string s in m.AttachfilesPath)
                    {
                        result.Add("Attaching File : " + s);
                       
                        System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(s);                        
                        /* Attach the newly created email attachment */
                        mailMessage.Attachments.Add(attach);
                    }

                }

                /* Set the SMTP server and send the email with attachment */
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
                // smtpClient.Host = emailServerInfo.MailServerIP;
                //this will be the host in case of gamil and it varies from the service provider
                smtpClient.Host = m.Host;
                //smtpClient.Port = Convert.ToInt32(emailServerInfo.MailServerPortNumber);
                //this will be the port in case of gamil for dotnet and it varies from the service provider
                smtpClient.Port = m.Port;
                smtpClient.UseDefaultCredentials = true;
                //smtpClient.Credentials = new System.Net.NetworkCredential(emailServerInfo.MailServerUserName, emailServerInfo.MailServerPassword);
                smtpClient.Credentials = new System.Net.NetworkCredential(m.UserName, m.Password);
                //this will be the true in case of gamil and it varies from the service provider
                smtpClient.EnableSsl = m.SSL;
                smtpClient.Send(mailMessage);

                try
                {

                    /* Delete the attachements if any */
                    foreach (string s in m.AttachfilesPath)
                    {
                        File.Delete(s);
                    }
                }
                catch { }
                result.Add("1");

            }
            catch (Exception ex)
            {
                result.Add("-2 " + ex.Message);
            }
            return result;

        }
and In Calling Appication (Which is a Silverlight Application)


ObservableCollection cv = new ObservableCollection();
            //cv.Add(@"\\C6\Shared\Repo\a\Drafts\go.png");//This Works
            cv.Add(@"www.msdotnetmentor.com/wp-content/uploads/2009/08/5.JPG");//This Doesnot Work
            Mail.Bcc = txtBCC.Text;
            Mail.Body = txtMailBody.Text;
            Mail.Cc = txtCC.Text;
            //Mail.AttachfilesPath = SendDraftAttachment;
            Mail.AttachfilesPath = cv;
            Mail.Host = EditDeleteEmailConfig.SMTPHost;
            Mail.Password = EditDeleteEmailConfig.SMTPPassword;
            Mail.Port = EditDeleteEmailConfig.SMTPPort;
            Mail.Receiver = txtTo.Text;
            Mail.Sender = txtFrom.Text;
            Mail.SSL = EditDeleteEmailConfig.SMTPSSL;
            Mail.Subject = txtSubject.Text;
            Mail.UserName = EditDeleteEmailConfig.SMTPUserName;
            SuccessMessage = "Mail Sent Successfully";
            ErrorMessage = "Error In Sending Mail" + Environment.NewLine + "Try Sending Later";
            if (EmailSettings)
            {
                SendClient.funSendMailAsync(Mail);
                SendClient.funSendMailCompleted += new EventHandler(SendClient_funSendMailCompleted);
            }
            else
            {
                EditDeleteEmailConfig = new EmailConfig();
                IsSendMail = true;
                EmailSettingsClient.GetEamilConfigByClientIdAsync(userID);
            }



Now The Solution IS


Fine I got The Answer

Points:
1. URI is not a physical path so it can't be used for File Handling as we need the Physical path for that.
2. There is nothing in WCf like server.mappath()
3. we have to do some workaround to achive so and that work around is

add the following Namespaces :
using System.ServiceModel.Activation;
using System.Web;


now the class implementing the intervfce or the WCF class Use This Line of Code
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]


Eg:
// NOTE: If you change the class name "SendMailWCF" here, you must also update the reference to "SendMailWCF" in App.config.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SendMailWCF : ISendMailWCF
{


and now you can access the file like
string RD = HttpContext.Current.Server.MapPath("Repo") + @"\" + filename;



And below is an excerpt from the web.config.



<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <behaviors> </behaviors> <services> </services>

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com