sending email using gmail account on local machine

喜欢而已 提交于 2019-12-11 20:38:59

问题


I am using gmail for sending email in my asp.net application. Email works fine if I send email on server but if I try to send emails on local machine it give error. I placed break poin in code and when send method is called it shows error box with heading "Smtp exception was unhandled by user code" and in detail it says "Faliur sending mail".

If I continued on browser it shows error page with these details:

An existing connection was forcibly closed by the remote host Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

Below is my code, kindly guide me.

Thanks.

  protected void btnConfirm_Click(object sender, EventArgs e)
{
    string _Message = GetAdminEmailMessage();        

    MailMessage message = new MailMessage();
    SmtpClient smtp = new SmtpClient();
    NetworkCredential mailAuthentication = new NetworkCredential("myaccount@gmail.com", "mypassword");
    message.To.Add(new MailAddress("mc5678@hotmail.com"));
    message.From = new MailAddress("myaccount@gmail.com");
    message.IsBodyHtml = true;
    message.Subject = "Local test email";
    message.Body = _Message;
    smtp.UseDefaultCredentials = false;
    smtp.EnableSsl = true;
    smtp.Port = 587;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Host = "smtp.gmail.com";
    smtp.Credentials = mailAuthentication;
    smtp.Send(message);

}

回答1:


Error code 0x800CCC0E indicates that the port has been blocked: http://support.microsoft.com/kb/191687

Try telnet smtp.gmail.com 587 - Can you connect?

If your port is unblocked and your credentials correct, then the code in the accepted answer for this question should work..

Sending email through Gmail SMTP server with C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
                EnableSsl = true
            };
            client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
            Console.WriteLine("Sent");
            Console.ReadLine();
        }
    }
}


来源:https://stackoverflow.com/questions/4690354/sending-email-using-gmail-account-on-local-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!