2009-06-24 16 views
0

私のWinFormアプリケーションでC#でコード化された動作があります。私の中c#ウィンドウからのメール送信フォームがトリガーされない

private void buttonSave_Click(object sender, EventArgs e)

Imは私の関数を呼び出す: functions.sendStatusEmail();

奇妙なことは、私はその後、保存ボタンを押すと、メールの送信がトリガされていない、ということです。しかし、私が私のアプリケーションを閉じると、メールは処理され、送信されます。

私は何かが欠けているか、送信イベントを手動で実行するためにsomアプリケーションイベントを手動で起動する必要がありますか?

事前に

おかげで(私はそれをクリックするだけでtriggerdが、メールが空だったclient.SendAsync(mail,null);を使用してみました)

- 編集:コードは

private void buttonSave_Click(object sender, EventArgs e) 
{ 
    // checks if a ticket is active 
    if (workingTicketId > 0) 
    { 
     // update ticket information 
     functions.updateTicketInfo(workingTicketId, comboBoxPriority.SelectedIndex, 
            comboBoxStatus.SelectedIndex, textBoxComment.Text); 

     // gives feedback 
     labelFeedback.Text = "Updated"; 

     // updates the active ticket list 
     populateActiveTicketList(); 

     // marks working ticket row in list 
     dataGridActiveTicketList.Rows[workingGridIndex].Selected = true; 

     // checks for change of ticket status 
     if (comboBoxStatus.SelectedIndex != workingTicketStatus) 
     { 
      // checks if contact person exists 
      if (labelContactPersonValue.Text.ToString() != "") 
      { 
       // sends email to contact person 
       functions.sendStatusEmail(labelContactPersonValue.Text, comboBoxStatus.SelectedIndex, workingTicketId, textBoxDescription.Text); 
      } 

      // updates working ticket status 
      workingTicketStatus = comboBoxStatus.SelectedIndex; 
     } 

    } 
} 

とセンドメール機能をexpamples :

// sends a status email to contact person 
// returns noting 
public void sendStatusEmail(string email, int newStatus, int ticketId, string ticketText) 
{ 
    // defines variables 
    string emailSubject; 
    string emailBody; 


    // some exkluded mailcontent handling 

    // sends mail 
    MailMessage mail = new MailMessage("[email protected]",email,emailSubject,emailBody); 
    SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["MailSMTP"]); 

    mail.IsBodyHtml = true; 
    client.Send(mail); 

    // dispose 
    mail.Dispose(); 
} 
+3

ポストはsendStatusEmailのコード() – Hemant

+0

...ともbuttonSave_Click –

答えて

1

can not unなぜそれがうまくいかないかを理解する。私は、関数の下に使用し、それが成功した電子メールを送信します。

public static bool SendEmail (string smtpServer, string fromAddress, string fromDisplayName, 
    string toAddress, string subject, string contents, bool important) { 

    MailAddress from = new MailAddress (fromAddress, fromDisplayName); 
    MailPriority priority = important ? MailPriority.High : MailPriority.Normal; 

    MailMessage m = new MailMessage { 
     From = from, 
     Subject = subject, 
     Body = contents, 
     Priority = priority, 
     IsBodyHtml = false 
    }; 

    MailAddress to = new MailAddress (toAddress); 
    m.To.Add (to); 

    SmtpClient c = new SmtpClient (smtpServer) { UseDefaultCredentials = false }; 

    c.Send (m); 
    return true; 
} 

悪気が、あなたはそれが送信される電子メールにつながるアプリケーションを終了していることを確信しています。ほとんどの場合、電子メールが送信されるまでに遅延があり、SMTPサーバー上のトラフィックのために受信されます。そのボタンを押してからしばらくお待ちください(3〜4分)、その間に受信トレイをリフレッシュしてみてください。

+0

のコード悪気が取られていない、.NET alltogheterに非常に新しいイム、と私結論は私には意味をなさないが、私は結果を待つことを試みた。私のアプリケーションを閉じると、デスクトップの見通しがメールを送信するシステムアイコンを表示するということがあります。それから私はそれを受け取ります。また、アプリケーションを終了しないと、Nortonから、タイムアウトなどの理由でメールを送信できないというエラーが表示されます。 – Andreas

+0

あなたのデスクトップの見通しに*電子メールの送信*とは何も関係ありません。おそらくあなたは*あなたが*電子メールを受信するので、あなたがアイコンを参照するため。確認するには、Outlookを終了し、メールを送信できるようにしてください。 Nortonについて:Norton Anti-virusからのメッセージですか? – Hemant

+0

それはantivirからですが、通常の種類ではありません。とにかく、私は別のボックスからそれを実行しようとしたと働いたので、それは私の箱にノートンで何かtodoを持っていると思います。あなたの努力に感謝し、時間の無駄を残して申し訳ありません。 – Andreas

1

ところで、SendAsyncは、非同期呼び出し後にmail.dispose()を呼び出したために機能しませんでした。非同期でそれを行うための正しい方法は次のようになり 、

private void button1_Click(object sender, EventArgs e) 
{ 
    MailMessage mail = new MailMessage("[email protected]", "[email protected]", "subj", "body"); 
    SmtpClient client = new SmtpClient("SMTPHOST"); 
    mail.IsBodyHtml = true; 
    client.SendCompleted += new 
    SendCompletedEventHandler(SendCompletedCallback); 

    client.SendAsync(mail,mail);//send the mail object itself as argument to callback 

} 

private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) 
{ 
    if (e.Cancelled) 
    { 
     //Mail sending is cancelled 
    } 
    if (e.Error != null) 
    { 
     //There is an error,e.Error will contain the exception 
    } 
    else 
    { 
     //Do any other success processing 
    } 

    ((MailMessage)e.UserState).Dispose();//Dispose 
} 
関連する問題