views:

42

answers:

2

I have used Google static map and created linked resource and embedded it in email and sending the mail to all users. I send a request to Google server and convert the response as stream and then created linked resource using that stream. The problem is I have used for loop and sent mail to all emailid's present in the Database table, i have created linked resource being created inside the for loop so each time the same image is requested from the Google server and mailed to user's. I want to prevent this, only for the first time the request has to be sent to Google and i have to store that response stream and use it to create linked resource. How to do this? i have tried storing the stream in local stream variable and created linked resource using that stream variable, i have also stored the stream in viewstate as well as in session but none of the methods worked out!

for (iCnt = 0; iCnt < dsEmailIds.Tables[0].Rows.Count; iCnt++) { //Linked resource to embed Google map in mail LinkedResource lnkResMain; if (imgPhotos.Src.Contains("maps.google.com")) lnkResMain = new LinkedResource(getStream(imgPhotos.Src));

//send mail mail.SendMail(fromAddress,dsEmailIds.Tables[0].Rows[0]["toAddress"].ToString(),lnkResMain);

}

//this converts string image url to stream since stream can be used to create linked resource public Stream getStream(string imgUrl) { System.Net.WebRequest req = System.Net.WebRequest.Create(imgUrl); System.Net.WebResponse response = req.GetResponse(); Stream stream = response.GetResponseStream(); return stream; }

+1  A: 

Using single stream in multiple emails will not work because for first email, the stream will read (and closed) and so will not be available for subsequent emails.

Why not save the map response stream into a temporary file and then create a linked resource using the file name (use this or this constructors - I will prefer later to specify the content type as well as).

VinayC
I have fixed this issue. I have sent request to Google server and got the response as stream. Then I have converted that stream to an image file and stored in temp files. Then I have used linked resource and embedded the Google map in temp files and finally after all mails are sent, I have checked if file exists and deleted that file from temp files location so that when mailing again it doesn't cause any issues.
banupriya
A: 

The code for this functionality is:

    //Sends request to Google and gets the response as stream
    //imgUrl - image url for the Google static map
    public Stream getStream(string imgUrl)
    {
        Stream stream = null;

        try
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(imgUrl);
            System.Net.WebResponse response = req.GetResponse();
            stream = response.GetResponseStream();
            return stream;
        }
        catch (Exception ex)
        {
            throw ex;
        }            
    }

    //Converts stream to image and stores the image in temp files location
    //strm - Stream containing Google map 
    //imageName - image Name to be saved in temp file
    public void SaveStreamAsImage(Stream strm,string imageName)
    {
       System.Drawing.Image img = null;

        try
        {
            img = System.Drawing.Image.FromStream(strm);
            img.Save(System.IO.Path.GetTempPath() + imageName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            strm.Dispose();
            strm.Close();

            img.Dispose();
        }            
    }

    //Deletes the temp image file after mailing to all users
    //filePath - file path of image which is to be deleted
    public void DeleteFile(string filePath)
    {
        if (File.Exists(filePath))
        {
            File.Delete(filePath);                
        }
    }


    public void SendEmail()
    {

    for (iCnt = 0; iCnt < dsEmailIds.Tables[0].Rows.Count; iCnt++)
    {
     //Linked resource to embed Google map in mail LinkedResource lnkResMain; 
     if (imgPhotos.Src.Contains("maps.google.com"))
     {
         Stream strm = getStream(imgPhotos.Src);
         SaveStreamAsImage(strm, "img1");
     }

     //send mail                              
     mail.SendMail(fromAddress,dsEmailIds.Tables[0].Rows[0]["toAddress"].ToString(),lnkResMain);

     }
     delDeleteFile(System.IO.Path.GetTempPath() + "img1");
    }
banupriya