-2

Here is my C# Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.EnableSsl = true;
        client.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "password");
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("xyz@gmail.com");
        msg.To.Add(new MailAddress(TextBox1.Text));
        msg.Subject = "Testing Email";
        String Body = "This is Testing Email.";
        msg.Body = Body;
        msg.IsBodyHtml = true;
        try
        {
            client.Send(msg);
            MsgBox("Send Successfully");
        }
        catch (Exception ex)
        {
            MsgBox("deleivery failed" + ex.ToString());
        }
    }
}

The problem is that when i enter Correct Email ID in TextBox1 then alert with Send Successfully is appear and no error message and when i enter a wrong email id like 12345xyz@gmail.com in TextBox1 then also alert with Send Successfully is appear and no error message and i recieve an email for email Delivery Failed in xyz@gmail.com inbox.

How i check email is send or delivery failed. I am using ASP.Net and C#.

user3834541
  • 161
  • 1
  • 7
  • 27
  • http://stackoverflow.com/questions/11406889/how-to-check-if-email-was-delivered-using-c-sharp-mailmessage – Edi G. Sep 01 '14 at 09:33
  • 2
    The `MsgBox` is a windows control, for desktop programming, its not working on web, you can not get any message from it. – Aristos Sep 01 '14 at 09:50

2 Answers2

0

change your catch block exception type as

 catch (SmtpFailedRecipientsException ex)
        {
           //Show error msg
        }

SmtpFailedRecipientsException is thrown when e-mail is sent using an SmtpClient and cannot be delivered to all recipients

Add Using System.Net.Mail

.Also look at SmtpException class

yogi970
  • 446
  • 5
  • 14
-1
   try
{
    client.Send(msg);
    Page.RegisterStartupScript("UserMsg", "<script>alert('Successfully Send...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
    Exception ex2 = ex;
    string errorMessage = string.Empty;
    while (ex2 != null)
    {
        errorMessage += ex2.ToString();
        ex2 = ex2.InnerException;
    }
    Page.RegisterStartupScript("UserMsg", "<script>alert('Sending Failed...');if(alert){ window.location='SendMail.aspx';}</script>");
}

Maybe this will help you?

GalaxMan
  • 58
  • 10