<small id='0RkS3'></small><noframes id='0RkS3'>

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

      <bdo id='0RkS3'></bdo><ul id='0RkS3'></ul>

  1. <tfoot id='0RkS3'></tfoot>
    1. 通过 Google Apps 帐户通过 C# 发送电子邮件

      Send Email via C# through Google Apps account(通过 Google Apps 帐户通过 C# 发送电子邮件)

      <tfoot id='t5OVI'></tfoot>

          1. <legend id='t5OVI'><style id='t5OVI'><dir id='t5OVI'><q id='t5OVI'></q></dir></style></legend>
              <tbody id='t5OVI'></tbody>

              <bdo id='t5OVI'></bdo><ul id='t5OVI'></ul>

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

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

              • 本文介绍了通过 Google Apps 帐户通过 C# 发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个标准的 Google Apps 帐户.我已经通过 Google Apps 设置了一个自定义域.使用 Gmail 界面时,我可以通过 Google Apps 成功发送和接收电子邮件.但是,我想通过代码发送电子邮件.为了尝试这个,我一直在尝试以下代码:

                I have a standard Google Apps account. I have setup a custom domain through Google Apps. I am able to send and receive emails successfully through Google Apps when I use the Gmail interface. However, I want to send an email via code. In order to attempt this, I have been trying the following code:

                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add("someone@somewhere.com");
                mailMessage.Subject = "Test";
                mailMessage.Body = "<html><body>This is a test</body></html>";
                mailMessage.IsBodyHtml = true;
                
                // Create the credentials to login to the gmail account associated with my custom domain
                string sendEmailsFrom = "emailAddress@mydomain.com";             
                string sendEmailsFromPassword = "password";
                NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);
                
                SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
                mailClient.EnableSsl = true;
                mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                mailClient.UseDefaultCredentials = false;
                mailClient.Timeout = 20000;
                mailClient.Credentials = cred;
                mailClient.Send(mailMessage); 
                

                当到达 Send 方法时,抛出一个异常状态:

                When the Send method is reached, an Exception is thrown that states:

                "SMTP 服务器需要一个安全的连接或客户端没有认证.服务器响应是:5.5.1 需要身份验证."

                "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."

                如何通过 Google 通过我的自定义域发送电子邮件?

                How do I send emails through my custom domain via Google?

                谢谢!

                推荐答案

                无需在代码中硬编码所有 smtp 设置.将它们放在 web.config 中.这样,您可以在需要时加密这些设置并即时更改它们,而无需重新编译您的应用程序.

                There is no need to hardcode all smtp settings in your code. Put them in web.config instead. This way you can encrypt these settings if needed and change them on the fly without recompiling your application.

                <configuration>
                  <system.net>
                    <mailSettings>
                      <smtp from="example@domain.com" deliveryMethod="Network">
                          <network host="smtp.gmail.com" port="587"
                              userName="example@domain.com" password="password"/>
                      </smtp>
                    </mailSettings>
                  </system.net>
                </configuration>
                

                在您发送电子邮件时结束,只需在您的 SmtpClient 上启用 SSL:

                End when you send email just enable SSL on your SmtpClient:

                var message = new MailMessage("navin@php.net");
                // here is an important part:
                message.From = new MailAddress("example@domain.com", "Mailer");
                // it's superfluous part here since from address is defined in .config file
                // in my example. But since you don't use .config file, you will need it.
                
                var client = new SmtpClient();
                client.EnableSsl = true;
                client.Send(message);
                

                确保您发送的电子邮件地址与您在 Gmail 中尝试进行身份验证时使用的电子邮件地址相同.

                Make sure that you're sending email from the same email address with which you're trying to authenticate at Gmail.

                注意:从 .NET 4.0 开始,您可以将 enableSsl="true" 插入 web.config,而不是在代码中进行设置.

                Note: Starting with .NET 4.0 you can insert enableSsl="true" into web.config as opposed to setting it in code.

                这篇关于通过 Google Apps 帐户通过 C# 发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                  <legend id='ELxJw'><style id='ELxJw'><dir id='ELxJw'><q id='ELxJw'></q></dir></style></legend>

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

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

                        <tbody id='ELxJw'></tbody>
                      <tfoot id='ELxJw'></tfoot>

                        • <bdo id='ELxJw'></bdo><ul id='ELxJw'></ul>