如何在通过 SmtpClient 发送电子邮件时使用 http 代理 [英] How to use http proxy while sending email via SmtpClient

查看:129
本文介绍了如何在通过 SmtpClient 发送电子邮件时使用 http 代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过以下方式使用雅虎电子邮件发送电子邮件.但我的问题是我也可以让计算机在连接​​雅虎服务器时使用代理吗?我的意思是使用代理连接来连接雅虎 smpt 服务器.这可能吗 ?谢谢

I am able to send emails via below way with Yahoo emails. But my question is can i also make it a way that computer will use proxy while connecting yahoo servers ? I mean use proxy connection to connect yahoo smpt server. Is this possible ? thank you

public static bool func_SendEmail(string srFrom, string srSenderEmail, string srSenderEmailPw, 
        string srHtmlBody, string srTextBody, string srTitle, string srProxy)
{
    try
    {
        using (MailMessage message = new MailMessage(new MailAddress(srSenderEmail, srFrom), new MailAddress(srSenderEmail)))
        {
            message.ReplyTo = new MailAddress(srSenderEmail, srFrom);
            message.IsBodyHtml = false;
            message.Subject = srTitle;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            AlternateView textPart = AlternateView.CreateAlternateViewFromString(srTextBody, Encoding.UTF8, "text/plain");
            textPart.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
            message.AlternateViews.Add(textPart);
            AlternateView htmlPart = AlternateView.CreateAlternateViewFromString(srHtmlBody, Encoding.UTF8, "text/html");
            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
    {
        return false;
    }
    return true;
}

好吧,这个问题和这个问题不一样:通过http代理发送邮件

Alright this question is not same as this one : Sending mail through http proxy

那个问题专门问了如何使用proxy

That question specifically asks how to use proxy

另一方面我的问题是如何使用http代理连接另一个邮件服务器来发送电子邮件

My question on the other hand asks how to use http proxy to connect another mail server to send email

在这种情况下,我想使用线程,每个线程的代理,由此每个线程连接到雅虎 smtp 服务器,并使用 http 代理发送电子邮件

In this case i want to use threads, proxies for each thread and from this each thread connect to yahoo smtp server with using http proxy to send email

谢谢

推荐答案

System.Net.GlobalProxySelection.Select = new WebProxy(address,port);

<小时>

更新:System.Net.GlobalProxySelection.Select 已被弃用

如果你使用它,你会得到一个警告:


Update: System.Net.GlobalProxySelection.Select has been deprecated

If you use it you will get a warning:

该类已被弃用.请使用 WebRequest.DefaultWebProxy而是访问和设置全局默认代理.改用空"GetEmptyWebProxy 的.http://go.microsoft.com/fwlink/?linkid=14202>

This class has been deprecated. Please use WebRequest.DefaultWebProxy instead to access and set the global default proxy. Use 'null' instead of GetEmptyWebProxy. http://go.microsoft.com/fwlink/?linkid=14202

改用这个:

WebRequest.DefaultWebProxy = new WebProxy(address,port);

这篇关于如何在通过 SmtpClient 发送电子邮件时使用 http 代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆