อีเมล C # ไม่ได้รับการส่ง SMTP

ฉันกำลังพยายามส่งอีเมลโดยทางโปรแกรมใน C# แต่ฉันไม่พบสิ่งที่ฉันขาดหายไป นี่คือรหัสสำหรับสิ่งเดียวกัน

string SendEmail(string toAddress, string subject, string body, string senderID)
        {
            string result = "Message Sent Successfully..!!";
            const string senderPassword = "mypassword";
            try
            {
                SmtpClient smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
                    Timeout = 30000,
                };
                MailMessage message = new MailMessage(senderID, toAddress, subject, body);
                smtp.Send(message);
            }
            catch (SmtpException e)
            {
                result = "Smtp status code " + e.StatusCode;
            }
            catch (Exception ex)
            {
                result ="Error sending email " +ex.ToString();
            }
            return result;
        } 

ฉันได้ลองดูค่าภายในตัวแปรผลลัพธ์แล้ว มันมีสองค่า

  1. รหัสสถานะ Smtp ล้มเหลวทั่วไป
  2. การดำเนินการหมดเวลา // หากเราแสดงความคิดเห็นเกี่ยวกับโค้ด catch (SmtpException e)

หากฉันดูในเว็บไซต์ msdn เพื่อหารหัสสถานะ SMTP "ความล้มเหลวทั่วไป" การตีความรหัสสถานะ smtp เราจะพบว่ารหัสสถานะหมายความว่าไม่พบโฮสต์ และฉันลองทำ nslookup และโฮสต์ "smtp.gmail.com" พร้อมใช้งานและเข้าถึงได้ จากเทลเน็ต

สิ่งที่ฉันขาดหายไปในโปรแกรม?


person Prashant Bhanarkar    schedule 26.08.2016    source แหล่งที่มา
comment
ส่งอีเมลผ่าน SMTP โดยใช้ C# ที่เกี่ยวข้อง   -  person Mehdi Dehghani    schedule 26.08.2016


คำตอบ (2)


นี่คือโค้ดสำหรับส่งอีเมลโดยทางโปรแกรมโดยใช้ c# มันทำงานได้ดีในเครื่องของฉัน

    try
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

        mail.From = new MailAddress("email");
        mail.To.Add("email");
        mail.Subject = "Subject";
        mail.Body = "Message";
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("email", "password");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);
        return true;
    }
    catch (Exception ex)
    {
        return "false";
    }
person JeetDaloneboy    schedule 26.08.2016
comment
หากเราตรวจพบข้อยกเว้น เราได้รับความพยายามในการเชื่อมต่อล้มเหลวเนื่องจากบุคคลที่เชื่อมต่อไม่ตอบสนองอย่างถูกต้องหลังจากช่วงระยะเวลาหนึ่ง หรือการเชื่อมต่อที่สร้างไว้ล้มเหลวเนื่องจากโฮสต์ที่เชื่อมต่อไม่สามารถตอบสนอง - person Prashant Bhanarkar; 26.08.2016
comment
ฉันเดาว่าคุณต้องเปลี่ยนการตั้งค่าในบัญชี Gmail ของคุณ - person JeetDaloneboy; 26.08.2016
comment
ฉันเปิดการตั้งค่าแอปที่มีความปลอดภัยน้อยกว่าใน Gmail ของฉันแล้ว แต่ก็ยังไม่ได้ช่วยอะไร - person Prashant Bhanarkar; 26.08.2016
comment
รหัสของคุณทำงานได้ดีอย่างสมบูรณ์บนเครื่องของฉัน - person JeetDaloneboy; 26.08.2016
comment
@PrasantBhanarkar ลบรหัสยืนยันที่สองในบัญชี Gmail ของคุณ และตรวจสอบว่าชื่อผู้ใช้และรหัสผ่านที่ให้มานั้นถูกต้องหรือไม่ การอ้างอิง : c-sharpcorner.com/blogs/ - person Rajeesh Menoth; 30.08.2016

นี่คือรหัสของฉัน คุณสามารถลองได้

public static bool SendEmail(string subject, MailAddress from, List<string> to,List<string> cc, string body, Attachment attachment)
{
    try
    {

        MailMessage mail = new MailMessage();
        mail.Subject = subject;
        mail.From = from;
        foreach (var item in to)
            mail.To.Add(item);
        foreach (var item in cc)
            mail.CC.Add(item);
        mail.IsBodyHtml = true;
        mail.Body = body;
        if (attachment != null)
            mail.Attachments.Add(attachment);

        SmtpClient mailClient = new SmtpClient();
        mailClient.Port = 587;//maybe 25
        mailClient.Host = "smtp.gmail.com";
        mailClient.EnableSsl = true;
        mailClient.Credentials = new NetworkCredential("EmailAddress", "EmailPassword");
        //mailClient.Credentials = new NetworkCredential("[email protected]", "1234");


        try
        {
            mailClient.Send(mail);
        }
        catch (Exception ex)
        {
            return false;
        }

        return true;
    }
    catch { return false; }
}
person Ömer Bodur    schedule 26.08.2016