1. <small id='2xYQK'></small><noframes id='2xYQK'>

        <bdo id='2xYQK'></bdo><ul id='2xYQK'></ul>
      1. <legend id='2xYQK'><style id='2xYQK'><dir id='2xYQK'><q id='2xYQK'></q></dir></style></legend>
        <tfoot id='2xYQK'></tfoot>
      2. <i id='2xYQK'><tr id='2xYQK'><dt id='2xYQK'><q id='2xYQK'><span id='2xYQK'><b id='2xYQK'><form id='2xYQK'><ins id='2xYQK'></ins><ul id='2xYQK'></ul><sub id='2xYQK'></sub></form><legend id='2xYQK'></legend><bdo id='2xYQK'><pre id='2xYQK'><center id='2xYQK'></center></pre></bdo></b><th id='2xYQK'></th></span></q></dt></tr></i><div id='2xYQK'><tfoot id='2xYQK'></tfoot><dl id='2xYQK'><fieldset id='2xYQK'></fieldset></dl></div>
      3. 当我的电脑通过代理连接或没有代理连接到 Internet 时,从 Gmail、live、aol 或 yahoo 帐户发送

        时间:2023-10-06

          <small id='hG3I5'></small><noframes id='hG3I5'>

          <legend id='hG3I5'><style id='hG3I5'><dir id='hG3I5'><q id='hG3I5'></q></dir></style></legend>
        1. <tfoot id='hG3I5'></tfoot>

          <i id='hG3I5'><tr id='hG3I5'><dt id='hG3I5'><q id='hG3I5'><span id='hG3I5'><b id='hG3I5'><form id='hG3I5'><ins id='hG3I5'></ins><ul id='hG3I5'></ul><sub id='hG3I5'></sub></form><legend id='hG3I5'></legend><bdo id='hG3I5'><pre id='hG3I5'><center id='hG3I5'></center></pre></bdo></b><th id='hG3I5'></th></span></q></dt></tr></i><div id='hG3I5'><tfoot id='hG3I5'></tfoot><dl id='hG3I5'><fieldset id='hG3I5'></fieldset></dl></div>
                • <bdo id='hG3I5'></bdo><ul id='hG3I5'></ul>

                    <tbody id='hG3I5'></tbody>
                  本文介绍了当我的电脑通过代理连接或没有代理连接到 Internet 时,从 Gmail、live、aol 或 yahoo 帐户发送 SMTP 邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想通过 SMTP 将 C# 中的电子邮件发送到不同的邮件提供商,例如 Gmail、Yahoo、AOL、Msn、Live 等,以便如果我的计算机通过代理连接到互联网或直接连接到互联网,我的代码可以正常工作.(代理是一个转发代理,从内部网络接收请求并将它们转发到互联网,我在 IE 中配置代理)
                  ..................................

                  I want to send email in C# via SMTP to different mail providers example Gmail, Yahoo, AOL, Msn, Live etc so that my code works fine if my computer is connected to internet via proxy or connected directly to internet. (Proxy is a forward proxy taking requests from an internal network and forwarding them to the Internet and I configure proxy in I.E. as)
                  .................................

                  如果 PC 没有通过代理连接,我有代码可以发送 SMTP 邮件

                  I have code by which I can send SMTP mail if PC is not connected via proxy

                      public void SendMail(string senderId, string password, List<string> To, List<string> CC, List<string> BCC, string Subject, string Body, List<Attachment> Attachment)
                      {
                          SmtpClient SmtpServer = null;
                          string[] ss = senderId.Split('@');
                  
                          string ServerName = ss[1].Substring(0, ss[1].IndexOf("."));
                  
                          switch (ServerName.ToLower())
                          {
                              case "gmail":
                                  SmtpServer = new SmtpClient("smtp.gmail.com");
                                  SmtpServer.Port = 587;
                                  SmtpServer.Credentials = new System.Net.NetworkCredential(senderId, password);
                                  SmtpServer.EnableSsl = true;
                                  break;
                              case "msn":
                              case "live":
                              case "hotmail":
                              case "outlook":
                                  SmtpServer = new SmtpClient("smtp.live.com");
                                  SmtpServer.Port = 25;
                                  SmtpServer.Credentials = new System.Net.NetworkCredential(senderId, password);
                                  SmtpServer.EnableSsl = true;
                                  break;
                  
                              case "aol":
                                  SmtpServer = new SmtpClient("smtp.aol.com");
                                  SmtpServer.Port = 25;
                                  SmtpServer.Credentials = new System.Net.NetworkCredential(senderId, password);
                                  SmtpServer.EnableSsl = true;
                                  break;
                              case "yahoo":
                              case "ymail":
                              case "rocketmail":
                              case "yahoomail":
                                  SmtpServer = new SmtpClient("smtp.mail.yahoo.com");
                                  SmtpServer.Port = 25;
                                  SmtpServer.Credentials = new System.Net.NetworkCredential(senderId, password);
                                  SmtpServer.EnableSsl = false;
                                  break;
                              default:
                  
                                  break;
                  
                  
                          }
                          SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
                  
                          MailMessage mail = new MailMessage();
                          mail.From = new MailAddress(senderId);
                  
                          foreach (string item in To)
                          {
                              mail.To.Add(item);
                          }
                          foreach (string item in CC)
                          {
                              mail.CC.Add(item);
                          }
                          foreach (string item in BCC)
                          {
                              mail.Bcc.Add(item);
                          }
                  
                          mail.Subject = Subject;
                          mail.Body = Body;
                  
                          foreach (Attachment item in Attachment)
                          {
                              mail.Attachments.Add(item);
                          }
                  
                          SmtpServer.Send(mail);
                  
                      }
                  

                  这一切正常,但我想在通过代理连接时发送电子邮件.

                  This is working perfectly fine but I want to send email when I am connected via proxy.

                  我已经阅读了很多帖子,例如,通过代理服务器发送邮件
                  通过代理的 ASP.net SMTP 邮件
                  是否有用于通过代理发送电子邮件的 .NET 库?
                  他们都说这是不可能的,但是在搜索 我发现了 chilkat 库 limilabs 示例,允许用户通过配置代理通过代理发送邮件.
                  我进行了很多研究,我阅读了 SOCKS 代理,学习了如何使用原始套接字发送 SMTP 邮件,但我无法找到缺少的解决方案.
                  如果有人遇到同样的问题或任何想法我能做些什么,我将不胜感激以前做过的一些工作的任何想法?

                  I have read so many posts like, Sending Mail over proxy server
                  ASP.net SMTP Mail though Proxy
                  Is there .NET library for email sending via PROXY?
                  They all state it is not possible but while searching I found chilkat library, limilabs sample which allows user to send mail via proxies by configuring the proxy.
                  I have researched a lot, I have read SOCKS proxy, learnt how to send SMTP mail using raw sockets but I am unable to find solution something missing.
                  I will appreciate any ideas for some work that has been done before, if any one who faced the same problem or any ideas what I can do?

                  -正如我已经提到的那样,我找到了使用 Chilkat 和 limilabs 的样本,这意味着 我不想使用这些> 并且我不允许使用任何第三方 dll

                  - As I have already mentioned that, I have found samples using Chilkat and limilabs it means I do not want to use those and I am not allowed to use any third party dll

                  推荐答案

                  通过代理服务器发送邮件有点棘手,但最终通过使用代码通过套接字发送邮件解决了问题.

                  Sending mails via proxy server was a little tricky but at the end solved the problem by using the code to send mails via socket.

                  迟到了,但我们假设它可能对其他人有帮助.

                  从 链接

                  附上以下代码,SmtpServer 对象与问题描述的相同,

                  Attaching the code below, the SmtpServer object is same as described in question,

                     #region ..... SOCKET MAIL .....
                  
                      private enum SMTPResponse : int
                      {
                          /// <summary>
                          /// 220
                          /// </summary>
                          CONNECT_SUCCESS = 220,
                          /// <summary>
                          /// 235
                          /// </summary>
                          CRED_SUCCESS = 235,
                          /// <summary>
                          /// 250
                          /// </summary>
                          GENERIC_SUCCESS = 250,
                          /// <summary>
                          /// 334
                          /// </summary>
                          AUTH_SUCCESS = 334,
                          /// <summary>
                          /// 354
                          /// </summary>
                          DATA_SUCCESS = 354,
                          /// <summary>
                          /// 221
                          /// </summary>
                          QUIT_SUCCESS = 221
                      }
                  
                      public static string EncodeTo64(string toEncode)
                      {
                  
                          byte[] toEncodeAsBytes = System.Text.Encoding.ASCII.GetBytes(toEncode);
                  
                          string returnValue = System.Convert.ToBase64String(toEncodeAsBytes, Base64FormattingOptions.None);
                  
                          return returnValue;
                  
                      }
                  
                      public bool SendFromSocket(MailMessage message)
                      {
                          var requestUri = new System.Uri("smtps://" +SmtpServer.Host +":"+SmtpServer.Port);
                  
                          Uri proxy = null;
                          //This is magic code which does your job for the proxy 
                          using (var web = new System.Net.WebClient())
                          {                
                              proxy = web.Proxy.GetProxy(requestUri);                
                          }
                  
                          using (var s = new TcpClient(proxy.DnsSafeHost, proxy.Port  ))
                          {               
                              using (var stream = s.GetStream())
                              using (var clearTextReader = new StreamReader(stream))
                              using (var clearTextWriter = new StreamWriter(stream) { AutoFlush = true })
                              using (var sslStream = new SslStream(stream))
                              {
                                  if (!Check_Response(clearTextReader, SMTPResponse.CONNECT_SUCCESS))
                                  {
                                      s.Close();
                                      return false;
                                  }
                  
                                  clearTextWriter.WriteLine("HELO");
                                  if (!Check_Response(clearTextReader, SMTPResponse.GENERIC_SUCCESS))
                                  {
                                      s.Close();
                                      return false;
                                  }
                  
                                  clearTextWriter.WriteLine("STARTTLS");
                                  if (!Check_Response(clearTextReader, SMTPResponse.CONNECT_SUCCESS))
                                  {
                                      s.Close();
                                      return false;
                                  }
                  
                  
                                  sslStream.AuthenticateAsClient(SmtpServer.Host);
                                  bool flag = sslStream.IsAuthenticated;
                  
                                  using (var reader = new StreamReader(sslStream))
                                  using (var writer = new StreamWriter(sslStream) { AutoFlush = true })
                                  {                       
                                      writer.WriteLine(string.Format("EHLO {0}", SmtpServer.Host));
                                      if (!Check_Response(reader, SMTPResponse.GENERIC_SUCCESS))
                                      {
                                          s.Close();
                                          return false;
                                      }
                  
                                      writer.WriteLine("AUTH LOGIN");
                                      if (!Check_Response(reader, SMTPResponse.AUTH_SUCCESS))
                                      {
                                          s.Close();
                                          return false;
                                      }
                  
                                      writer.WriteLine(EncodeTo64(SenderId));
                                      if (!Check_Response(reader, SMTPResponse.AUTH_SUCCESS))
                                      {
                                          s.Close();
                                          return false;
                                      }
                  
                                      writer.WriteLine(EncodeTo64(Password));
                                      if (!Check_Response(reader, SMTPResponse.CRED_SUCCESS))
                                      {
                                          s.Close();
                                          return false;
                                      }
                  
                                      writer.WriteLine("MAIL FROM: <{0}>", message.From);
                                      if (!Check_Response(reader, SMTPResponse.GENERIC_SUCCESS))
                                      {
                  
                                          s.Close();
                                          return false;
                                      }
                  
                                      foreach (MailAddress To in message.To)
                                      {
                                          writer.WriteLine("RCPT TO: <{0}>", To.Address);
                                          if (!Check_Response(reader, SMTPResponse.GENERIC_SUCCESS))
                                          {
                                              s.Close();
                                              return false;
                                          }
                                      }
                  
                                      if (message.CC != null)
                                      {
                                          foreach (MailAddress cc in message.CC)
                                          {
                                              writer.WriteLine(string.Format("RCPT TO: <{0}>", cc.Address));
                                              if (!Check_Response(reader, SMTPResponse.GENERIC_SUCCESS))
                                              {
                                                  s.Close();
                                                  return false;
                                              }
                                          }
                                      }
                  
                                      StringBuilder Header = new StringBuilder();
                                      Header.Append("From: " + message.From + "
                  ");
                                      Header.Append("To: ");
                                      for (int i = 0; i < message.To.Count; i++)
                                      {
                                          Header.Append(i > 0 ? "," : "");
                                          Header.Append(message.To[i].Address);
                                      }
                                      Header.Append("
                  ");
                                      if (message.CC != null)
                                      {
                                          Header.Append("Cc: ");
                                          for (int i = 0; i < message.CC.Count; i++)
                                          {
                                              Header.Append(i > 0 ? "," : "");
                                              Header.Append(message.CC[i].Address);
                                          }
                                          Header.Append("
                  ");
                                      }
                                      Header.Append("Date: ");
                                      Header.Append(DateTime.Now.ToString("ddd, d M y H:m:s z"));
                                      Header.Append("
                  ");
                                      Header.Append("Subject: " + message.Subject + "
                  ");
                                      Header.Append("X-Mailer: SMTPDirect v1
                  ");
                                      string MsgBody = message.Body;
                                      if (!MsgBody.EndsWith("
                  "))
                                          MsgBody += "
                  ";
                                      if (message.Attachments.Count > 0)
                                      {
                                          Header.Append("MIME-Version: 1.0
                  ");
                                          Header.Append("Content-Type: multipart/mixed; boundary=unique-boundary-1
                  ");
                                          Header.Append("
                  ");
                                          Header.Append("This is a multi-part message in MIME format.
                  ");
                                          StringBuilder sb = new StringBuilder();
                                          sb.Append("--unique-boundary-1
                  ");
                                          sb.Append("Content-Type: text/plain
                  ");
                                          sb.Append("Content-Transfer-Encoding: 7Bit
                  ");
                                          sb.Append("
                  ");
                                          sb.Append(MsgBody + "
                  ");
                                          sb.Append("
                  ");
                  
                                          foreach (object o in message.Attachments)
                                          {
                                              Attachment a = o as Attachment;
                                              byte[] binaryData;
                                              if (a != null)
                                              {
                                                  //FileInfo f = new FileInfo(a.);
                                                  sb.Append("--unique-boundary-1
                  ");
                                                  sb.Append("Content-Type: application/octet-stream; file=" + a.Name + "
                  ");
                                                  sb.Append("Content-Transfer-Encoding: base64
                  ");
                                                  sb.Append("Content-Disposition: attachment; filename=" + a.Name + "
                  ");
                                                  sb.Append("
                  ");
                                                  Stream fs = a.ContentStream;
                                                  binaryData = new Byte[fs.Length];
                                                  long bytesRead = fs.Read(binaryData, 0, (int)fs.Length);
                                                  fs.Close();
                                                  string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
                  
                                                  for (int i = 0; i < base64String.Length; )
                                                  {
                                                      int nextchunk = 100;
                                                      if (base64String.Length - (i + nextchunk) < 0)
                                                          nextchunk = base64String.Length - i;
                                                      sb.Append(base64String.Substring(i, nextchunk));
                                                      sb.Append("
                  ");
                                                      i += nextchunk;
                                                  }
                                                  sb.Append("
                  ");
                                              }
                                          }
                                          MsgBody = sb.ToString();
                                      }
                  
                                      writer.WriteLine("DATA
                  ");
                                      if (!Check_Response(reader, SMTPResponse.DATA_SUCCESS))
                                      {
                                          s.Close();
                                          return false;
                                      }
                                      Header.Append("
                  ");
                                      Header.Append(MsgBody);
                                      Header.Append(".
                  ");
                                      Header.Append("
                  ");
                                      Header.Append("
                  ");
                                      writer.WriteLine(Header.ToString());
                                      if (!Check_Response(reader, SMTPResponse.GENERIC_SUCCESS))
                                      {
                                          s.Close();
                                          return false;
                                      }
                  
                                      writer.WriteLine("QUIT
                  ");
                                      Check_Response(reader, SMTPResponse.QUIT_SUCCESS);
                                      s.Close();
                                      return true;
                                  }
                              }
                  
                          }
                      }
                  
                      private static bool Check_Response(StreamReader netStream, SMTPResponse response_expected)
                      {
                  
                          int response;
                  
                          int read = 0;
                  
                          StringBuilder sResponse = new StringBuilder();
                          do
                          {
                              char[] buffer = new char[1024];                
                              read = netStream.Read(buffer, 0, buffer.Length);
                              sResponse.Append(buffer);                
                          }
                          while (read == 1024);
                  
                          response = Convert.ToInt32(sResponse.ToString().Substring(0, 3));
                  
                          if (response != (int)response_expected)
                              return false;
                          return true;
                      }
                  
                      #endregion ..... SOCKET MAIL .....
                  

                  这篇关于当我的电脑通过代理连接或没有代理连接到 Internet 时,从 Gmail、live、aol 或 yahoo 帐户发送 SMTP 邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:无法从 Azure 云服务使用 Gmail smtp 下一篇:从 Web.Config 中的电子邮件友好显示名称存储 Smtp

                  相关文章

                    <i id='JcmFi'><tr id='JcmFi'><dt id='JcmFi'><q id='JcmFi'><span id='JcmFi'><b id='JcmFi'><form id='JcmFi'><ins id='JcmFi'></ins><ul id='JcmFi'></ul><sub id='JcmFi'></sub></form><legend id='JcmFi'></legend><bdo id='JcmFi'><pre id='JcmFi'><center id='JcmFi'></center></pre></bdo></b><th id='JcmFi'></th></span></q></dt></tr></i><div id='JcmFi'><tfoot id='JcmFi'></tfoot><dl id='JcmFi'><fieldset id='JcmFi'></fieldset></dl></div>

                  1. <small id='JcmFi'></small><noframes id='JcmFi'>

                      • <bdo id='JcmFi'></bdo><ul id='JcmFi'></ul>
                      <tfoot id='JcmFi'></tfoot>
                      <legend id='JcmFi'><style id='JcmFi'><dir id='JcmFi'><q id='JcmFi'></q></dir></style></legend>