通过电子邮件访问网页

发布于 2024-08-21 12:35:10 字数 202 浏览 2 评论 0原文

我的数据提供商允许我在手机上访问邮件,但不能访问互联网。 我正在考虑编写一个实用程序来通过电子邮件获取网页,以便我在旅行时可以在手机上查看网页。这个想法是在我的系统上运行一个服务(运行 Outlook 并连接到交换服务器),该服务等待以网址作为主题的“查询邮件”。该服务应该获取网页并回复 html 内容。

请建议我如何有效地实施这一点。有没有可用的实用程序具有相同的功能?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奈何桥上唱咆哮 2024-08-28 12:35:10

使用系统 CDO dll 来实现相当简单。我已经有了一个工作原型,只需将邮件发送至 [email protected]您将收到带有嵌入式网页(如果主题中提供了 url)或附加网络存档(包含所有资源的 mht 文件)的答案(如果邮件正文中发送了 url)。
这是一个快速而肮脏的代码片段,您可以使用:

    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 是一个小方法,它发送 HEAD,然后在失败时发送 GET 请求来确定内容的类型和编码。

It's rather simple to do using system CDO dll. I already has a working prototype, just drop a mail to [email protected] 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.

谈情不如逗狗 2024-08-28 12:35:10

sourceforge 上有一个实用程序可以执行相同的操作: Web2Mail

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

命比纸薄 2024-08-28 12:35:10

Instapaper 做了类似的事情 - 它将网页转换为简单的 HTML 以便阅读。也许不完全是您所需要的,但可能是一个很好的起点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文