简体   繁体   English

如何使用C#将asp.net中的电子邮件发送到使用gmail地址的任何电子邮件地址

[英]How to send email in asp.net using c# to any email address using gmail address

I am trying to send email through an application in asp.net using c#. 我正在尝试使用c#通过asp.net中的应用程序发送电子邮件。 I searched a lot and mostly found the following code to send email using c# in asp.net: 我进行了很多搜索,大多数情况下在asp.net中找到以下代码使用c#发送电子邮件:

        MailMessage objEmail = new MailMessage();

        objEmail.From = new MailAddress(txtFrom.Text);
        objEmail.To.Add(txtTo.Text);
        objEmail.CC.Add(txtCC.Text);
        objEmail.Bcc.Add(txtBCC.Text);
        objEmail.Subject = txtSubject.Text;

        try
        {

            SmtpClient mail = new SmtpClient();

            mail.EnableSsl = true;
            mail.DeliveryMethod = SmtpDeliveryMethod.Network;
            mail.Credentials = new NetworkCredential(txtFrom.Text, txtPassword.Text);
            mail.Timeout = 20000;

            mail.UseDefaultCredentials = false;

            mail.Host = "smtp.gmail.com";
            mail.Port = 587;

            mail.Send(objEmail);

            Response.Write("Your Email has been sent sucessfully - Thank You");

        }
        catch (Exception exc)
        {
            Response.Write("Send failure due to : <br />" + exc.ToString());
        }

But constantly I am receiving the following error: 但是我经常收到以下错误:

"System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& “” System.Net.Mail.SmtpException:发送邮件失败。---> System.IO.IOException:无法从传输连接中读取数据:现有连接被远程主机强行关闭。---> System.Net .Sockets.SocketException:System.Net.Sockets.NetworkStream.Read上的远程主机强制关闭了现有连接。 (字节[]缓冲区,Int32偏移量,整数32大小)---内部异常堆栈跟踪的结尾---在System.Net.Sockets.NetworkStream.Read(字节[]缓冲区,Int32偏移量,整数32大小)在System.Net .DelegatedStream.Read(Byte。]缓冲区,在System.Net.BufferedReadStream.Read(Byte []缓冲区,Int32偏移,Int32计数)在System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader调用者, System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader调用者)的System.Net.Mail.CheckCommand.Send(SmtpConnection conn,String& response) at System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)" System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn)在System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)在System.Net.Mail。 System.Net.Mail.SmtpClient.Send(MailMessage消息)处的.SmtpClient.GetConnection()”

You're trying to use Gmail's SMTP server with an email address that doesn't belong to Gmail (according to the post's title). 您正在尝试将Gmail的SMTP服务器用于不属于Gmail的电子邮件地址(根据帖子的标题)。 You need to update the host and port details to suit your email provider's SMTP details. 您需要更新主机和端口的详细信息,以适合电子邮件提供商的SMTP详细信息。

Your code looks reasonable, so you need to figure out what is preventing it from working. 您的代码看起来合理,因此您需要找出导致其无法正常工作的原因。

Can you eliminate the possibility of a firewall problem? 您能否消除防火墙问题的可能性? Most medium or large organizations tend to block ports 25, 465 and 587 - they simply don't want any unauthorized mail going out. 大多数中型或大型组织都倾向于阻塞端口25、465和587-他们只是不希望任何未经授权的邮件发送出去。 If you suspect this might be the case, you could try if from outside the network (ie your home machine) although some ISPs will also block ports. 如果您怀疑可能是这种情况,则可以尝试从网络外部(例如,您的家用计算机)进行尝试,尽管某些ISP也会阻塞端口。

If the firewall isn't blocking your connections, verify that your credentials work - this can be done by sending via Thunderbird, Outlook, or equivalent. 如果防火墙没有阻止您的连接,请验证您的凭据是否有效-可以通过Thunderbird,Outlook或类似的方式发送。 Try setting up a test using unencrypted mail - root thru your spam folder, paste a few into spamcop or equivalent, and look for an open relay. 尝试使用未加密的邮件设置测试-遍历您的垃圾邮件文件夹,将其中一些粘贴到spamcop或等效邮件中,然后寻找开放中继。 Alternatively, there are some commercial email providers that support unencrypted email. 或者,有些商业电子邮件提供商支持未加密的电子邮件。

You can check this out .. simple code and its works for gmail account ... 您可以检查出来..简单的代码及其适用于gmail帐户的功能...

    try
    {

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(TextFRM.Text);
        msg.To.Add(TextTO.Text);
        msg.Subject = TextSUB.Text;
        msg.Body = TextMSG.Text;


        SmtpClient sc = new SmtpClient("smtp.gmail.com");
        sc.Port = 25;
        sc.Credentials = new NetworkCredential(TextFRM.Text, TextPSD.Text);
        sc.EnableSsl = true;
        sc.Send(msg);
        Response.Write("Mail Send");
    }

    catch (Exception ex)
    {

        Response.Write(ex.Message);

    }

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

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