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

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

问题描述

我可以通过Yahoo电子邮件通过以下方式发送电子邮件.但是我的问题是,我还可以使计算机在连接​​Yahoo服务器时使用代理吗?我的意思是使用代理连接来连接yahoo 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

该问题专门询问如何使用代理

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

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

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而是访问并设置全局默认代理.使用'null'代替GetEmptyWebProxy. http://go.microsoft.com/fwlink/?linkid=14202

改为使用此:

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

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

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