Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
AnswerRe: Sending E mail using C# Pin
Marco Bertschi13-Nov-14 23:01
protectorMarco Bertschi13-Nov-14 23:01 
QuestionHow To Create Data Recovery in C#.net Pin
Srikanth5913-Nov-14 1:51
Srikanth5913-Nov-14 1:51 
AnswerRe: How To Create Data Recovery in C#.net Pin
Dave Kreskowiak13-Nov-14 2:08
mveDave Kreskowiak13-Nov-14 2:08 
QuestionPDFSHARP - table page break Pin
Mol4ok12-Nov-14 22:19
Mol4ok12-Nov-14 22:19 
AnswerRe: PDFSHARP - table page break Pin
Mol4ok16-Nov-14 15:57
Mol4ok16-Nov-14 15:57 
GeneralRe: PDFSHARP - table page break Pin
Mycroft Holmes16-Nov-14 18:32
professionalMycroft Holmes16-Nov-14 18:32 
GeneralRe: PDFSHARP - table page break Pin
Mol4ok16-Nov-14 21:03
Mol4ok16-Nov-14 21:03 
QuestionCannot access a disposed object. Object name : System.Net.Mail.MailMessage Pin
Jassim Rahma12-Nov-14 20:35
Jassim Rahma12-Nov-14 20:35 
Hi,

I am trying to send email message in C# using this MSDN article:

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx

but I am getting this error:

Cannot access a disposed object. Object name : System.Net.Mail.MailMessage

can anyone help please...

C#
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
    // Get the unique identifier for this asynchronous operation.
    String token = (string)e.UserState;

    if (e.Cancelled)
    {
        Console.WriteLine("[{0}] Send canceled.", token);
    }
    if (e.Error != null)
    {
        MessageBox.Show(String.Format("[{0}] {1}", token, e.Error.ToString()));
    }
    else
    {
        Console.WriteLine("Message sent.");
    }

    mailSent = true;
}
        
private static void send_message()
{
    // Command line argument must the the SMTP host.
    SmtpClient client = new SmtpClient("smtp.gmail.com");
    client.Port = 587;
    client.Credentials = new System.Net.NetworkCredential("it@mydomain.com", "my_password");
    client.EnableSsl = true;

    // Specify the e-mail sender. 
    // Create a mailing address that includes a UTF8 character 
    // in the display name.
    MailAddress from = new MailAddress("it@mydomain.com", "My" + (char)0xD8 + " Company", System.Text.Encoding.UTF8);
    // Set destinations for the e-mail message.
    MailAddress to = new MailAddress("jassim@mydomain.com");
    // Specify the message content.
    MailMessage message = new MailMessage(from, to);
    message.Body = "This is a test e-mail message sent by an application. ";
    // Include some non-ASCII characters in body and subject. 
    string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
    message.Body += Environment.NewLine + someArrows;
    message.BodyEncoding = System.Text.Encoding.UTF8;
    message.Subject = "test message 1" + someArrows;
    message.SubjectEncoding = System.Text.Encoding.UTF8;
    // Set the method that is called back when the send operation ends.
    client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
    // The userState can be any object that allows your callback  
    // method to identify this send operation. 
    // For this example, the userToken is a string constant. 
    string userState = "test message1";
    client.SendAsync(message, userState);
    Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
    string answer = Console.ReadLine();
    // If the user canceled the send, and mail hasn't been sent yet, 
    // then cancel the pending operation.

    // if (answer.StartsWith("c") && mailSent == false)
    // {
        // client.SendAsyncCancel();
    // }

    // Clean up.
    message.Dispose();
    Console.WriteLine("Goodbye.");
}

private void btnSend_Click(object sender, EventArgs e)
{
    // XtraMessageBox.Show("You are about to send this message to all contacts!", "Send", MessageBoxButtons.OK, MessageBoxIcon.Warning);

    if (XtraMessageBox.Show("Are you sure you want to send this message to all contacts now?", "Send", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
    {
        send_message();
    }
}



Technology News @ www.JassimRahma.com

AnswerRe: Cannot access a disposed object. Object name : System.Net.Mail.MailMessage Pin
BillWoodruff12-Nov-14 21:04
professionalBillWoodruff12-Nov-14 21:04 
AnswerRe: Cannot access a disposed object. Object name : System.Net.Mail.MailMessage PinPopular
Bernhard Hiller12-Nov-14 21:10
Bernhard Hiller12-Nov-14 21:10 
AnswerRe: Cannot access a disposed object. Object name : System.Net.Mail.MailMessage Pin
Marco Bertschi13-Nov-14 23:05
protectorMarco Bertschi13-Nov-14 23:05 
QuestionAsk Pin
kavnico12-Nov-14 16:50
kavnico12-Nov-14 16:50 
AnswerRe: Ask Pin
BillWoodruff12-Nov-14 20:30
professionalBillWoodruff12-Nov-14 20:30 
AnswerRe: Ask Pin
Richard MacCutchan12-Nov-14 21:22
mveRichard MacCutchan12-Nov-14 21:22 
GeneralRe: Ask Pin
ZurdoDev13-Nov-14 3:07
professionalZurdoDev13-Nov-14 3:07 
GeneralRe: Ask Pin
Richard MacCutchan13-Nov-14 3:10
mveRichard MacCutchan13-Nov-14 3:10 
GeneralRe: Ask Pin
ZurdoDev13-Nov-14 3:19
professionalZurdoDev13-Nov-14 3:19 
QuestionC# FormatCurrency(VB) to ToString Pin
Member 876492812-Nov-14 10:23
Member 876492812-Nov-14 10:23 
AnswerRe: C# FormatCurrency(VB) to ToString Pin
Eddy Vluggen12-Nov-14 10:47
professionalEddy Vluggen12-Nov-14 10:47 
AnswerRe: C# FormatCurrency(VB) to ToString Pin
Pete O'Hanlon12-Nov-14 10:58
mvePete O'Hanlon12-Nov-14 10:58 
GeneralRe: C# FormatCurrency(VB) to ToString Pin
Dominic Burford13-Nov-14 4:25
professionalDominic Burford13-Nov-14 4:25 
GeneralRe: C# FormatCurrency(VB) to ToString Pin
Pete O'Hanlon13-Nov-14 4:49
mvePete O'Hanlon13-Nov-14 4:49 
GeneralRe: C# FormatCurrency(VB) to ToString Pin
Dominic Burford13-Nov-14 5:34
professionalDominic Burford13-Nov-14 5:34 
AnswerRe: C# FormatCurrency(VB) to ToString PinPopular
Dave Kreskowiak12-Nov-14 11:13
mveDave Kreskowiak12-Nov-14 11:13 
GeneralRe: C# FormatCurrency(VB) to ToString Pin
Bernhard Hiller12-Nov-14 21:15
Bernhard Hiller12-Nov-14 21:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.