Shima
Shima

Reputation: 19

send mail and show delivery message in asp.net

how i can show delivery message in asp.net? I use this code for my job but this code no reply me. because i want show message delivery

 public string  sendemail(String strFrom, string strTo, string strSubject, string strBody)
 {
        string delivery;
        Array arrToArray;
        char[] splitter = { ';' };
        arrToArray = strTo.Split(splitter);
        MailMessage mm = new MailMessage();

            mm.From = new MailAddress(strFrom);

        mm.Subject = strSubject;
        mm.Body = strBody;
        //mm.IsBodyHtml = IsBodyHTML;

            mm.ReplyTo = new MailAddress("[email protected]");


        foreach (string s in arrToArray)
        {
            mm.To.Add(new MailAddress(s));
        }

        SmtpClient smtp = new SmtpClient();
        try
        {
            smtp.Host = "smtp.mail.yahoo.com";
            smtp.EnableSsl = true; //Depending on server SSL Settings true/false
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = "[email protected]";
            NetworkCred.Password = "pass";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;//Specify your port No;
            smtp.Send(mm);
            mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
            delivery  = mm.DeliveryNotificationOptions.ToString();



        }
        catch
        {
            mm.Dispose(); 
            smtp = null;
            delivery = mm.DeliveryNotificationOptions.ToString();

        }
        return delivery;

    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        Thread threadSendMails;
        threadSendMails = new Thread(delegate()
        {
          Label1.Text  =  sendemail("[email protected]", "[email protected]", "Hello", "<p>Body</p>");

        });
        threadSendMails.IsBackground = false ;
        threadSendMails.Start();

    }

Upvotes: 1

Views: 848

Answers (2)

ilyabreev
ilyabreev

Reputation: 634

I think there is no need to send e-mail in the separate thread. Instead you can just call sendemail or another method that implements email sending in the btnSend_click event handler.

Also there is no synchronously way to show delivery message of email you sent.

If you want just to show the success message to your website user then you need to declare some asp:Label control in markup file (.aspx), make it invisible by default, and show it in your btnSend_click just like that:

protected void btnSend_Click(object sender, EventArgs e)
{
    sendemail("[email protected]", "[email protected]", "Hello", "<p>Body</p>");
    Label1.Visible = true;
    // assuming you have asp:Label control with ID "Label1" 
    // and "Visible" property set to false
}

Upvotes: 0

Sascha
Sascha

Reputation: 10347

Because of the design of the SMTP protocol you cannot be sure that a message has actually arrived at the recepient. You can only be sure that the server has accepted the message. The delivery notification is only implemented in some target mail servers and read notifications are mostly a client thing. If they are written they will be sent via mail to the sender's address. To catch those, you need to monitor a POP3 or IMAP box, or whatever you have.

Additionally: You should think of not using a thread - this might easily lead to hard to to debug weird behaviour.

Upvotes: 2

Related Questions