Click here to Skip to main content
14,364,290 members
Rate this:
Please Sign up or sign in to vote.
See more:
i use this code to send email in asp.net.this methos is in class.i want to save email info when send completed.how can return a value from this method to page that if send return true,if error occured return false.then code in page save email info?
thank you in advance.

public void SendAsyncMail()
{
    MailMessage mail = new MailMessage();

    mail.From = new MailAddress("Enter from mail address");
    mail.To.Add(new MailAddress("Enter to address #1"));
    mail.To.Add(new MailAddress("Enter to address #2"));
    mail.Subject = "Enter mail subject";
    mail.Body = "Enter mail body";

    SmtpClient smtpClient = new SmtpClient();
    Object state = mail;

    //event handler for asynchronous call
    smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
    try
    {
        smtpClient.SendAsync(mail, state);
    }
    catch (Exception ex) { /* exception handling code here */ }
}

void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    MailMessage mail = e.UserState as MailMessage;

    if (!e.Cancelled && e.Error!=null)
    {
        message.Text = "Mail sent successfully";
    }
}
Posted
Comments
Afzaal Ahmad Zeeshan 12-Nov-15 5:05am
   
void cannot return anything, but you can use it to fill in other fields. So fill in a value in another field and check it later in the code.
NewWebDesigner 12-Nov-15 5:22am
   
can you explaine more?i dont know how do it?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

One approach to achieve this is to override AsyncCompletedEventArgs and implement your own return methods.
For e.g.
public class CalculatePrimeCompletedEventArgs :
    AsyncCompletedEventArgs
{
public CalculatePrimeCompletedEventArgs(
     ...)
    {
        //Do stuff
    }
}


For the complete example, try How to: Implement a Component That Supports the Event-based Asynchronous Pattern[^].
   
Comments
NewWebDesigner 12-Nov-15 5:59am
   
ok.but it is a complete example.but i am new in web design.can you give me a simple example?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100