开发者

create linked resource for embedding Google map in mail

开发者 https://www.devze.com 2023-01-14 18:47 出处:网络
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 crea

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 email id'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;
 }


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).


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");
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号