通过 gmail 发送 Asp.Net 电子邮件

发布于 2024-10-09 08:37:48 字数 1598 浏览 4 评论 0原文

我正在尝试使用下面的代码和配置从 ASP.Net 通过 GMail 发送电子邮件。不幸的是,它似乎不起作用,也没有抛出错误消息。服务器日志或邮件 IIS 邮件文件夹中没有任何内容,我什至检查了发件人地址的垃圾箱,看看邮件是否最终到达那里。任何帮助将不胜感激。

C# 部分

    public void SendFeedback()
    {
        string emailFrom = this.Email.Text;

        MailMessage message = new MailMessage();
        // here is an important part:
        message.From = new MailAddress(emailFrom, "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.
        message.Subject = "www.gumpshen.com - Website Query";
        message.IsBodyHtml = true;
        message.Body = string.Format(" Name = {0}, Phone = {1}", Name.Text, Phone.Text);
        message.Body += Environment.NewLine;
        message.Body += Environment.NewLine;
        message.Body += ProjectDetails.Text; ;

        var client = new SmtpClient();

        try
        {
            client.Send(message);

这是配置部分:

<system.net>
  <mailSettings>
    <smtp from="[email protected]" deliveryMethod="Network" >
      <network host="smtp.gmail.com" port="587" 
        userName="[email protected]" password="myPassword"/>
    </smtp>
  </mailSettings>
</system.net>

I am trying to send an email via GMail from ASP.Net using the code and config below. Unfortunatly it doesn't seem to be working and it also isn't throwing an error message. There is nothing in the server logs or the mail IIS mail folders, I even checked the trash of the from address to see if the mail ended up there. Any help would be really appreciated.

C# Section

    public void SendFeedback()
    {
        string emailFrom = this.Email.Text;

        MailMessage message = new MailMessage();
        // here is an important part:
        message.From = new MailAddress(emailFrom, "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.
        message.Subject = "www.gumpshen.com - Website Query";
        message.IsBodyHtml = true;
        message.Body = string.Format(" Name = {0}, Phone = {1}", Name.Text, Phone.Text);
        message.Body += Environment.NewLine;
        message.Body += Environment.NewLine;
        message.Body += ProjectDetails.Text; ;

        var client = new SmtpClient();

        try
        {
            client.Send(message);

This is the Config section:

<system.net>
  <mailSettings>
    <smtp from="[email protected]" deliveryMethod="Network" >
      <network host="smtp.gmail.com" port="587" 
        userName="[email protected]" password="myPassword"/>
    </smtp>
  </mailSettings>
</system.net>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素染倾城色 2024-10-16 08:37:48

您需要 client.EnableSsl=true;

检查此站点的代码:通过 Gmail 发送电子邮件

以下示例介绍了如何使用 Google 帐户从 ASP.NET 页面发送 HTML 电子邮件。
(此设置可轻松用于通过任何其他需要身份验证的 SMTP 服务器发送消息)。
注意:代码片段假设您在页面上有一个名为 lblMsg 的标签组件,该组件将显示
向正在发送电子邮件的用户发送成功/失败消息。
(但这可以很容易改变)。

 SmtpClient 客户端 = new SmtpClient();
   client.DeliveryMethod = SmtpDeliveryMethod.Network;
   client.EnableSsl = true;
   client.Host = "smtp.gmail.com";
   客户端.端口 = 587;

   // 设置Smtp认证
   System.Net.NetworkCredential 凭据 = 
       new System.Net.NetworkCredential("[电子邮件受保护]", "yourpassword ”);
   client.UseDefaultCredentials = false;
   client.Credentials = 凭据;                

   MailMessage 消息 = new MailMessage();
   msg.From = new MailAddress("[电子邮件受保护]");
   msg.To.Add(new MailAddress("[电子邮件受保护]”) );

   msg.Subject = "这是一个测试电子邮件主题";
   msg.IsBodyHtml = true;
   msg.Body = string.Format("测试 HTML 电子邮件");

   尝试
   {
       客户端.发送(消息);
       lblMsg.Text = "您的消息已成功发送。";
   }
   catch(异常前)
   {
       lblMsg.ForeColor = Color.Red;
       lblMsg.Text = "发送消息时发生错误。" + 例如消息;
   }


You need client.EnableSsl=true;

Check the code from this site: Email via Gmail

Here is an example on how to send HTML email from your ASP.NET page using your Google account.
(This setup can be easily used to send messages via any other SMTP server that requires authentication).
Note: the code snippet assumes you have a Label component on Page with named lblMsg that will show
success/failure message to the user that is sending email.
(But this can be easily changed).

   SmtpClient client = new SmtpClient();
   client.DeliveryMethod = SmtpDeliveryMethod.Network;
   client.EnableSsl = true;
   client.Host = "smtp.gmail.com";
   client.Port = 587;

   // setup Smtp authentication
   System.Net.NetworkCredential credentials = 
       new System.Net.NetworkCredential("[email protected]", "yourpassword");
   client.UseDefaultCredentials = false;
   client.Credentials = credentials;                

   MailMessage msg = new MailMessage();
   msg.From = new MailAddress("[email protected]");
   msg.To.Add(new MailAddress("[email protected]"));

   msg.Subject = "This is a test Email subject";
   msg.IsBodyHtml = true;
   msg.Body = string.Format("<html><head></head><body><b>Test HTML Email</b></body>");

   try
   {
       client.Send(msg);
       lblMsg.Text = "Your message has been successfully sent.";
   }
   catch (Exception ex)
   {
       lblMsg.ForeColor = Color.Red;
       lblMsg.Text = "Error occured while sending your message." + ex.Message;
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文