How to use http proxy while sending email via SmtpClient [duplicate]

旧巷老猫 提交于 2019-11-30 09:55:49

问题


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;
}

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

That question specifically asks how to use proxy

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

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

thank you


回答1:


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

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

If you use it you will get a warning:

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

Use this instead:

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


来源:https://stackoverflow.com/questions/17049828/how-to-use-http-proxy-while-sending-email-via-smtpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!