Resources » Code Snippets » Web Access

Solution to: URI Formats Not Supported


Posted Date: 14-Oct-2009  Last Updated:   Category: Web Access    
Author: Member Level: Gold    Points: 28



Solution to: URI Formats Not Supported



I am Sending a Mail with attachments 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(@"file://c6/Shared/Repo/a/Drafts/go.png%22)> Works
cv.Add(@http://www.msdotnetmentor.com/wp-content/uploads/2009/08/5.JPG%22);> 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);
}




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;






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.



<!-- Added only the one line below -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <!-- Everything else was left intact --> <behaviors> <!-- ... --> </behaviors> <services> <!-- ... --> </services>



now the class implementing the intervfce or the WCF class Use This Line of Code



[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]




Thanks and Regards
Meetu Choudhary


Did you like this resource? Share it with your friends and show your love!

Responses to "Solution to: URI Formats Not Supported"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India