【问题标题】:receive email through asp.net application通过 asp.net 应用程序接收电子邮件
【发布时间】:2012-07-01 22:34:19
【问题描述】:

我想通过我的 asp.net 应用程序向我的电子邮件地址接收电子邮件。就像通过表格发送查询的人一样。我为此使用了以下代码,似乎它什么也没做。我记得我做过我的一个网站,但不记得我是怎么做的。请在下面找到代码。

谢谢,

eMessage.To = "info@boilernetworkservices.co.uk"
        eMessage.From = txtEmail.Text
        eMessage.Subject = "Web Submission"
        eMessage.Body = "Web submission received from " & txtName.Text & ". Phone no: " & txtPhone.Text & "."
        eMessage.Priority = MailPriority.High
        SmtpMail.Send(eMessage)

我怎样才能使它工作?

【问题讨论】:

    标签: asp.net .net vb.net


    【解决方案1】:

    您的示例代码展示了如何使用SMTP 发送电子邮件,但您将无法使用此协议从远程服务器检索电子邮件。

    用于检索电子邮件的两个最常用的协议是POP3IMAP4,不幸的是,.NET 框架没有像使用 SMTP 那样提供它们的实现。

    电子邮件检索的一个选项是使用开源 POP3 客户端 OpenPop.NET,此 SO 问题中对此进行了讨论:retrieve email using c#?

    【讨论】:

      【解决方案2】:

      我为我的公司设置了一个 Google 帐户,例如 myCompanyName@gmail.com。 我用它作为中继。 您必须将您的 Google 帐户设置为“允许安全性较低的应用”。

      这是我的代码,可让潜在客户填写联系我们并将信息发送给我(即使在我发布到 Azure 时也有效:)):

      private void SendEmailToMyCompany(ContactInfo contactInfo)
          {
              string message = contactInfo.Message.Replace("\n", "<br />");
      
              MailAddress from = new MailAddress(contactInfo.Email);
              MailAddress to = new MailAddress("myhotmailaccount@hotmail.com");
              MailMessage mailMessage = new MailMessage(from, to);
      
              StringBuilder body = new StringBuilder();
              body.AppendFormat($"<b>First Name:</b> {contactInfo.FirstName}");
              body.Append("<br />");
              body.AppendFormat($"<b>Last Name:</b> {contactInfo.LastName}");
              body.Append("<br />");
              body.AppendFormat($"<b>Phone:</b> {contactInfo.Phone}");
              body.Append("<br />");
              body.AppendFormat($"<b>Email:</b> {contactInfo.Email}");
              body.Append("<br />");
              body.AppendFormat($"<b>Message:</b><br /><br /> {message}");
      
              mailMessage.Body = body.ToString();
              mailMessage.Subject = "MyCompany Customer Contact";
              mailMessage.IsBodyHtml = true;
      
              string smtpHost = _config["EmailSettings:SmtpHost"];
              string port = _config["EmailSettings:Port"];
              string userName = _config["EmailSettings:UserName"];
              string password = _config["EmailSettings:Password"];
      
              SmtpClient client = new SmtpClient(smtpHost)
              {
                  Port = int.Parse(port),
                  Credentials = new NetworkCredential(userName, password),
                  EnableSsl = true
              };
      
              client.Send(mailMessage);
          }
      

      然后这是我在 app.config 中的电子邮件设置:

      "EmailSettings": {
      "SmtpHost": "smtp.gmail.com",
      "Port": 587,
      "UserName": "myCompanyNameGmailAccount@gmail.com",
      "Password": "**********"
      

      }

      【讨论】:

        猜你喜欢
        • 2013-10-27
        • 1970-01-01
        • 1970-01-01
        • 2016-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多