开发者

Access webpages through email

My data provider allows me to access mails on my phone, but not internet. I am thinking of writing a utility to fetch a webpage through e-mail, so that I can get view开发者_如何学C webpages on my phone while I'm travelling. The idea is to have a service running on my system (running outlook and connected to exchange server)which waits for a 'query mail' which has the web address as subject. This service should fetch the webpage and reply with the html content.

Please suggest ways as to how I can implement this efficiently. Is there any utility available which does the same?


It's rather simple to do using system CDO dll. I already has a working prototype, just drop a mail to site2email@gmail.com and you will receive an answer with embedded webpage (in case of url was provided in subject) or attached web archive (mht file with all resources included) in case of url sent in the mail's body. Here is a quick and dirty code snippet you can play with:

    private void ConvertAndSend(string emailTo,Uri url)
    {
        try
        {
            string mimeType;
            string charset;
            if (NetworkUtils.UrlExists(url.AbsoluteUri,out mimeType,out charset))
            {
                if (mimeType.Equals("text/html",StringComparison.OrdinalIgnoreCase))
                    SendUrlToEmail(url.AbsoluteUri,emailTo,charset);
                else
                {
                    string fileName = Path.GetFileName(url.AbsoluteUri);
                    string extension = GetExtensionByMimeType(mimeType);
                    if (String.IsNullOrEmpty(fileName)
                        || fileName.Length > 200)
                        fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
                    if (!Path.GetExtension(fileName).Equals(extension
                        ,StringComparison.OrdinalIgnoreCase))
                        fileName += extension;
                    string tempFolder = Path.Combine(Server.MapPath("App_Data")
                        ,"TempFiles");
                    if (!Directory.Exists(tempFolder))
                        Directory.CreateDirectory(tempFolder);
                    fileName = Path.Combine(tempFolder,fileName);
                    new WebClient().DownloadFile(url.AbsoluteUri,fileName);
                    SendMessage(emailTo,url.AbsoluteUri,Path.GetFileName(fileName),fileName);
                }
            }
            else
            {
                throw new WebException("Requiested address is not available at the moment.");
            }
        }
        catch (Exception ex)
        {
            SendMessage(emailTo,url.AbsoluteUri,ex.Message,null);
        }
    }

    public void SendUrlToEmail(string url
        ,string emailTo
        ,string charset)
    {
        Encoding encFrom;
        try { encFrom = Encoding.GetEncoding(charset); }
        catch { encFrom = Encoding.UTF8; }
        CDO.Message msg = null;
        ADODB.Stream stm = null;
        try
        {
            msg = new CDO.MessageClass();
            msg.MimeFormatted = true;
            msg.CreateMHTMLBody(url
                ,CDO.CdoMHTMLFlags.cdoSuppressNone
                ,string.Empty
                ,string.Empty);
            //msg.HTMLBodyPart.Fields["urn:schemas:mailheader:Content-Language"].Value = "ru";
            msg.HTMLBodyPart.Fields["urn:schemas:mailheader:charset"].Value = "Cp" + encFrom.CodePage;
            msg.HTMLBodyPart.Fields["urn:schemas:mailheader:content-type"].Value = "text/html; charset=" + charset;
            msg.HTMLBodyPart.Fields.Update();
            msg.HTMLBody = documentBody;
            msg.Subject = url;
            msg.From = Params.Username;
            msg.To = emailTo;
            msg.Configuration.Fields[GmailMessage.SMTP_SERVER].Value = SmtpServer;
            msg.Configuration.Fields[GmailMessage.SEND_USING].Value = 2;
            msg.Configuration.Fields.Update();
            msg.Send();
        }
        finally
        {
            if (stm != null)
            {
                stm.Close();
                Marshal.ReleaseComObject(stm);
            }
            if (msg != null)
                Marshal.ReleaseComObject(msg);
        }
    }

NetworkUtils.UrlExists is a small method that send HEAD and then if it fail GET request to determine type and encoding of the content.


There is a utility that does the same on sourceforge: Web2Mail


Instapaper does something similar - it converts web pages into simple HTML for reading. Maybe not exactly what you need, but possibly a good starting point.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜