Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a image URL("http://xyz.com/photos/pic.png") that is uploaded on server and I am getting that image path at run time.
I want to sent mail with attachment(with that image file), I have a service that accept file path as string parameter along with other required information(like TO,CC,...).

I am getting error if I am passing URL("http://xyz.com/photos/pic.png") as file path to service.
Posted
Updated 11-Aug-15 23:54pm
v2

1 solution

You can save the image on your server and then pass the image path to your Email Service.

public static void SaveImageFromUrl(string imageUrl)
        {
            //Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create(imageUrl);
            // Set the Method property of the request to POST.
            request.Method = "GET";
            WebResponse response = null;
            // Get the response.
            try
            {
                response = request.GetResponse();
            }
            catch (Exception Ex)
            {
                //error
            }
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            //path to save image
            string ImagePath = HttpContext.Current.Server.MapPath("~/attachments/") + Guid.NewGuid() + ".jpg";//check your extension

            //Save the image in your server
            using (Stream s = File.Create(ImagePath))
            {
                dataStream.CopyTo(s);
            }

            //Now pass the ImagePath in your Email service
            
        }
 
Share this answer
 
Comments
Awadhendra Tripathi 12-Aug-15 6:15am    
after sending mail that image should delete from server
F-ES Sitecore 12-Aug-15 7:12am    
So add code to delete the file after sending. If you don't know how to delete a file google "c# file.delete"
Awadhendra Tripathi 12-Aug-15 8:48am    
Thanks Sanjay

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