2014-01-18 51 views
1

應用程序需要使用SendAsync而不是僅發送。所以我做了一個類CEmailServer並設置了一切。到目前爲止發送工作正常,但改變它與SendAsync一起使用時,它不會。我創建了一個方法,當一個郵件被髮送並且userToken已經存在但它保持失敗時被調用。我找不到我的錯誤。這裏是我的代碼:SendAsync Smtp Mail Error

static bool mailSent = false; 

//Method for Sending with attachment. 
public void SendEmail(string Address, string Recipient, string Subject, string Body, string Dir) 
{ 
    var fromAddress = new MailAddress("[email protected]", "My Company"); 
    var toAddress = new MailAddress(Address, Recipient); 
    const string fromPassword = "password"; 
    string subject = Subject; 
    string body = Body; 

    var smtp = new SmtpClient 
    { 
     Host = "smtp.gmail.com", 
     Port = 587, 
     EnableSsl = true, 
     DeliveryMethod = SmtpDeliveryMethod.Network, 
     UseDefaultCredentials = false, 
     Credentials = new NetworkCredential(fromAddress.Address, fromPassword) 

    }; 
    using (var message = new MailMessage(fromAddress, toAddress) 
    { 
     Subject = subject, 
     Body = body, 
    }) 
    { 
     smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); 
     string userState = "Test"; 
     message.Attachments.Add(new Attachment(Dir)); 
     smtp.SendAsync(message,userState); 
     message.Dispose(); 
    } 
} 

//Method for Sending regular message without attachment. 
public void SendEmail(string Address, string Recipient, string Subject, string Body) 
{ 
    var fromAddress = new MailAddress("[email protected]", "My Company"); 
    var toAddress = new MailAddress(Address, Recipient); 
    const string fromPassword = "password"; 
    string subject = Subject; 
    string body = Body; 

    var smtp = new SmtpClient 
    { 
     Host = "smtp.gmail.com", 
     Port = 587, 
     EnableSsl = true, 
     DeliveryMethod = SmtpDeliveryMethod.Network, 
     UseDefaultCredentials = false, 
     Credentials = new NetworkCredential(fromAddress.Address, fromPassword) 

    }; 
    using (var message = new MailMessage(fromAddress, toAddress) 
    { 
     Subject = subject, 
     Body = body, 
    }) 
    { 
     smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); 
     string userState = "Message Sent"; 
     smtp.SendAsync(message, userState); 
     message.Dispose(); 
    } 
} 

//Method to be called when sending is complete 
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) 
{ 
    // Get the unique identifier for this asynchronous operation. 
    String token = (string)e.UserState; 

    if (e.Cancelled) 
    { 
     MessageBox.Show("Sending Canc"); 
    } 
    if (e.Error != null) 
    { 
     MessageBox.Show("Error Sending Mail"); 
    } 
    else 
    { 
     MessageBox.Show("Message sent."); 
    } 
    mailSent = true; 
} 

非常感謝!

+0

我衷心希望這不是你的實際密碼。 –

+0

不,舊的aacount被刪除了 – Mordacai1000

+0

但是,以防萬一使用不同的憑據:) – Mordacai1000

回答

6

在發送完成之前,您正在處置message。您應該將它們置於SendCompleted回調事件中。這是處理客戶端和消息的最佳方式的example

您的代碼應該是這個樣子:

var smtp = new SmtpClient 
{ 
    Host = "smtp.gmail.com", 
    Port = 587, 
    EnableSsl = true, 
    DeliveryMethod = SmtpDeliveryMethod.Network, 
    UseDefaultCredentials = false, 
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword) 
}; 

var message = new MailMessage(fromAddress, toAddress) 
{ 
    Subject = subject, 
    Body = body, 
}; 

smtp.SendCompleted += (s, e) => { 
    SendCompletedCallback(s, e); 
    smtp.Dispose(); 
    message.Dispose(); 
}; 
string userState = "Test"; 
message.Attachments.Add(new Attachment(Dir)); 
smtp.SendAsync(message, userState); 
+0

我刪除了Dispose方法調用,仍然無法正常工作... – Mordacai1000

+1

@ Mordacai1000你是否把'message' '聲明呢?另外,如果您可以提供例外的詳細信息,這將會很有幫助。 –

+0

我沒有刪除消息。這是錯誤:System.Net.Mail.SmtpException:發送郵件失敗。 ---> System.ObjectDisposedException:無法訪問處置的對象。 對象名稱:'System.Net.Mail.MailMessage'。 在System.Net.Mail.MailMessage.get_AlternateViews() 在System.Net.Mail.MailMessage.SetContent(布爾allowUnicode) 在System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult的結果) ---的內端異常堆棧跟蹤--- – Mordacai1000