C#과 sendmail async에 이상한 문제가 있습니다. 정상적인 전송 방법을 사용하면 정상적으로 작동하며 전자 메일이 올바른 SMTP 서버로 전송됩니다. 하지만 SendAsync-Method를 사용하면 작동하지 않습니다.SendMail SendAsync
mailClient.SendCompleted += SendCompleted;
private static void SendCompleted (object sender, AsyncCompletedEventArgs e)
{
string token = (string) e.UserState;
if (e.Cancelled)
{
MessageBox.Show (string.Format ("{0}\n\"{1}\"", Application.Current.FindResource ("MailCannotSend"), token), LogFile.MSGBOX_ERROR, MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
MessageBox.Show (e.Error != null ? string.Format ("{0}\n\"{1}\"", e.Error, token) : "Complete!", LogFile.MSGBOX_ERROR, MessageBoxButton.OK, MessageBoxImage.Error);
}
SendAsync-Method는 예외 나 오류없이 e.Cancelled 항목에서 항상 실행됩니다. 나는 정말로 이유를 모른다.
팁이 있습니까, 실마리가 있습니까? 감사!
public void InitClient()
{
try
{
mailClient = new SmtpClient (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.SmtpServerName, SettingsHelper.TailSettings.AlertSettings.SmtpSettings.SmtpPort)
{
UseDefaultCredentials = false,
Timeout = 30000,
DeliveryMethod = SmtpDeliveryMethod.Network
};
string decryptPassword = StringEncryption.Decrypt (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.Password, LogFile.ENCRYPT_PASSPHRASE);
NetworkCredential authInfo = new NetworkCredential (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.LoginName, decryptPassword);
mailClient.Credentials = authInfo;
mailClient.SendCompleted += SendCompleted;
if (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.SSL)
mailClient.EnableSsl = true;
if (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.TLS)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
MailAddress from = new MailAddress (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.FromAddress);
MailAddress to = new MailAddress (SettingsHelper.TailSettings.AlertSettings.EMailAddress);
mailMessage = new MailMessage (from, to)
{
Subject = SettingsHelper.TailSettings.AlertSettings.SmtpSettings.Subject,
SubjectEncoding = System.Text.Encoding.UTF8,
BodyEncoding = System.Text.Encoding.UTF8,
IsBodyHtml = false
};
}
catch (Exception ex)
{
ErrorLog.WriteLog (ErrorFlags.Error, GetType ().Name, string.Format ("{1}, exception: {0}", ex, System.Reflection.MethodBase.GetCurrentMethod ().Name));
}
}
/// <summary>
/// Send E-Mail
/// </summary>
/// <param name="userToken">User token</param>
/// <param name="bodyMessage">Message to be send</param>
public void SendMail (string userToken, string bodyMessage = null)
{
try
{
string userState = userToken;
if (bodyMessage != null)
mailMessage.Body = bodyMessage;
if (String.Compare (userState, "testMessage", StringComparison.Ordinal) == 0)
mailMessage.Body = string.Format ("Testmail from {0}", LogFile.APPLICATION_CAPTION);
mailClient.SendAsync (mailMessage, userToken);
if (String.Compare (userState, "testMessage", StringComparison.Ordinal) == 0)
return;
}
catch (Exception ex)
{
ErrorLog.WriteLog (ErrorFlags.Error, GetType ().Name, string.Format ("{1}, exception: {0}", ex, System.Reflection.MethodBase.GetCurrentMethod ().Name));
}
}
이 당신의 전체 코드를 게시 :
더 이상의 제안은 응용 프로그램의 실행 환경이 무엇인지에 따라 달라집니다. –
질문 제목에 사용 된 언어에 대한 정보가 없으면 이해할 수없는 경우를 제외하고는 해당 정보를 포함하지 마십시오. 태그는 이러한 용도로 사용됩니다. –