2014-12-23 242 views
-5
namespace WindowsFormsAppEmailClient 
{ 
public partial class Form1 : Form 
{ 
    MailMessage MyMsg = new MailMessage(); 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void btnSend_Click(object sender, EventArgs e) 
    { 
     SmtpClient MyServer = new SmtpClient("smtp.gmail.com", 587); 

     //defining global MyMsg so we use it in attachment button 
     // MailMessage MyMsg = new MailMessage(); 

     MyMsg.From = new MailAddress( "[email protected]"); 

     MyMsg.To.Add(tbTo.Text); 

     if (tbcc.Text != "") 
     { 
      MyMsg.CC.Add(tbcc.Text); 
     } 

     MyMsg.Subject = tbSub.Text; 

     //message = body 

     MyMsg.Body = tbMsg.Text; 

     MyServer.EnableSsl = true; 

     //Credentials used for defining User Name + Password 

     MyServer.Credentials = new System.Net.NetworkCredential("[email protected]", "03456016286"); 
     MyServer.Send(MyMsg); 
    } 

    private void btnAtt_Click(object sender, EventArgs e) 
    { 
     openFileDialog1.ShowDialog(); 
     Attachment myFile = new Attachment(openFileDialog1.FileName); 

     MyMsg.Attachments.Add(myFile); 
    } 
} 
} 

問題是:我想在電子郵件發送成功時顯示成功消息框。請幫忙顯示消息框

+8

請告訴我你沒」不要在代碼中發佈真實的憑據。 – itsme86

+0

親愛的我只需要點擊發送電子郵件「成功」的「顯示消息」 –

+2

是的,它的真實性和備份郵件地址不是太難以解決,所以你可能想要現在更改密碼;) – SwDevMan81

回答

0

基於您的代碼和您張貼在公共論壇您的實際憑據的事實,我會認爲你只是想同步的解決方案,並希望您(阻塞)調用MyServer.Send();

後顯示一個消息框換你發送一個try/catch塊:

// The program will attempt to send a message 
try 
{ 
    MySever.Send(MyMsg); 
    MessageBox.Show("Message sent successfully"); 
} 
catch (Exception ex) 
{ 
    // Whoops there was an error sending the message, better tell the user what happened. 
    MessageBox.Show(String.Format("Message Failed To send because: {0}", ex.message); 
} 

如果不工作,那麼你將需要一些背景添加到您的問題,如任何錯誤,或者你看到異常。

「正確」的方式來做到這一點,雖然是做MyServer.SendAsync(MyMsg),然後有一個回調在聽取了SMTPClient類提供給您的SendCompleted事件:

MSDN Example of Async Mail Send

+0

謝謝親愛的。現在它的工作 –

+0

@AsharKashif沒問題親愛的;)請點擊上/下按鈕下方的綠色複選標記,將我的答案標記爲答案。 –