是否可以使线程/任务使用http代理进行内部的所有连接

时间:2013-07-05 19:24:40

标签: c# proxy network-programming task smtpclient

好吧,让我尽量解释一下我的要求

当你开始如下任务时

    Task.Factory.StartNew(() =>
     {
         myFunction();
     });

我希望myFunction内的所有网络操作都使用http proxy。这可能吗?

可以使用httpwebrequest类的代理,但例如当我使用yahoo smtp发送邮件时,我无法使用任何代理。

所以想想就像在这里一样,无论这个代理用于所有网络操作是什么

C#4.5

好的,这是我用yahoo smtp发送电子邮件的方式。现在雅虎smtp本身就是一个代理。但是我想在这种情况下连接yahoo smtp时使用另一个代理(http web代理)。

这可能吗?

 public static bool func_SendEmail(string srFrom, string srSenderEmail, string srSenderEmailPw,
        string srHtmlBody, string srTextBody, string srTitle, string srProxy, string srSentToEmail)
    {
        try
        {
            using (MailMessage message = new MailMessage(new MailAddress(srSenderEmail, srFrom), new MailAddress(srSentToEmail)))
            using (AlternateView textPart = AlternateView.CreateAlternateViewFromString(srTextBody, Encoding.UTF8, "text/plain"))
            using (AlternateView htmlPart = AlternateView.CreateAlternateViewFromString(srHtmlBody, Encoding.UTF8, "text/html"))
            {
                message.ReplyTo = new MailAddress(srSenderEmail, srFrom);
                message.IsBodyHtml = false;
                message.Subject = srTitle;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                textPart.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
                message.AlternateViews.Add(textPart);
                htmlPart.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
                message.AlternateViews.Add(htmlPart);
                message.BodyEncoding = Encoding.UTF8;
                using (SmtpClient oSmtp = new SmtpClient())
                {

                    oSmtp.Host = "smtp.mail.yahoo.com";
                    oSmtp.Credentials = new NetworkCredential(srSenderEmail, srSenderEmailPw);
                    oSmtp.EnableSsl = false;
                    oSmtp.Port = 587;
                    oSmtp.Send(message);
                }
            }
        }
        catch (Exception E)
        {
            lock (swErrors)
            {
                swErrors.WriteLine(E.Message.ToString());
                if (E.InnerException != null)
                {
                    swErrors.WriteLine(E.InnerException.ToString());
                }
                swErrors.WriteLine("");
            }
            return false;
        }
        return true;
    }

0 个答案:

没有答案