简体   繁体   English

使用asp.net和谷歌SMTP服务器发送电子邮件

[英]Send email using asp.net and google smtp server

I have written following code in codebehind aspx page to send email.I want to use google smtp server. 我在aspx页面的代码背后写了以下代码来发送电子邮件。我想使用谷歌SMTP服务器。 But some how I am not receiving the mails 但是有些我没有收到邮件

                protected void btnSubmit_Click(object sender, EventArgs e)
                {              
                // Sender e-mail address.
                MailAddress From = new MailAddress(txtFrom.Text);
                // Recipient e-mail address.
                MailAddress To = new MailAddress(txtTo.Text);
                MailMessage Msg = new MailMessage(From,To);
                Msg.Subject = txtSubject.Text;
                Msg.Body = txtBody.Text;
                // your remote SMTP server IP.
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.Credentials = new System.Net.NetworkCredential
                 ("*******@gmail.com", "**********");
                client.EnableSsl = true;
                client.Port = 465;
                client.Send(Msg);
                client.Dispose();
                }

What wrong am I doing?Please help 我在做什么错?请帮忙

Here is what you need to do with the SmtpClient object: 这是您需要对SmtpClient对象执行的操作:

     SmtpClient client = new SmtpClient();
     client.DeliveryMethod = SmtpDeliveryMethod.Network;
     client.EnableSsl = true;
     client.Host = "smtp.gmail.com";
     client.Port = 587;
     client.Credentials = new System.Net.NetworkCredential("xxxx@gmail.com", "xxxx");

Gmail uses OAuth 2.0 you may well have to supply some sort of API key to access their mail functions. Gmail使用OAuth 2.0,您可能必须提供某种API密钥才能访问其邮件功能。

Accessing mail using IMAP and sending mail using SMTP is often done using existing IMAP and SMTP libraries for convenience. 为了方便起见,通常使用现有的IMAP和SMTP库完成使用IMAP访问邮件和使用SMTP发送邮件的过程。 As long as these libraries support the Simple Authentication and Security Layer (SASL), they should be compatible with the SASL XOAUTH2 mechanism supported by Gmail. 只要这些库支持简单身份验证和安全层(SASL),它们就应该与Gmail支持的SASL XOAUTH2机制兼容。

In addition to the SASL XOAUTH2 protocol documentation, you may also want to read Using OAuth 2.0 to Access Google APIs for further information on implementing an OAuth 2.0 client. 除了SASL XOAUTH2协议文档之外,您可能还需要阅读使用OAuth 2.0访问Google API,以获取有关实现OAuth 2.0客户端的更多信息。

The Libraries and Samples page provides code samples in a variety of popular languages using the SASL XOAUTH2 mechanism with either IMAP or SMTP. “库和样本”页面使用SASL XOAUTH2机制和IMAP或SMTP提供了各种流行语言的代码样本。 ( source ) 来源

Here is a generic email program. 这是一个通用的电子邮件程序。

It works on Port=25. 它适用于端口= 25。

Remember gmail is an IMAP server. 记住gmail是IMAP服务器。

  try 
  { 
           MailMessage msg = new MailMessage ();
           MailAddress fromAdd = new MailAddress("fromemail@email.com");
           msg.[To].Add("toemail@email.com");
           msg.Subject = "Choose Session Members";
           msg.From = fromAdd;
           msg .IsBodyHtml = true;
           msg.Priority = MailPriority.Normal;
           msg .BodyEncoding = Encoding.Default;
           msg.Body = "<center><table><tr><td><h1>Your Message</h1><br/><br/></td></tr>";
           msg.Body = msg.Body + "</table></center>";
           SmtpClient smtpClient = new SmtpClient ("smtp.yourserver.com", "25");
           smtpClient.EnableSsl = true;
           smtpClient.UseDefaultCredentials = false;
           smtpClient.Credentials = new System.Net.NetworkCredential("yourname@yourserver.com", "password");
           smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
           smtpClient.Send(msg);
           smtpClient.Dispose();
        }

First of all, have you checked the webmail? 首先,您是否检查过网络邮件? In the sent folder? 在发送的文件夹中? Many years ago I had the same problem, but I realized that my firewall was blocking me. 很多年前,我遇到了同样的问题,但是我意识到我的防火墙阻止了我。

Other thing, 另一件事

"SmtpClient class does not support Implicit SSL. It does support Explicit SSL, which requires an insecure connection to the SMTP server over port 25 in order to negotiate the transport level security (TLS)." “ SmtpClient类不支持隐式SSL。它支持显式SSL,后者需要通过端口25与SMTP服务器建立不安全的连接才能协商传输级安全性(TLS)。”

http://blog.ramsoftsolutions.com/2015/04/sending-mail-via-smtp-over-implicit-ssl.html http://blog.ramsoftsolutions.com/2015/04/sending-mail-via-smtp-over-implicit-ssl.html

Source: 资源:

How can I send emails through SSL SMTP with the .NET Framework? 如何使用.NET Framework通过SSL SMTP发送电子邮件?

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM