【问题标题】:SendAsync Smtp Mail ErrorSendAsync SMTP 邮件错误
【发布时间】:2014-01-18 20:15:15
【问题描述】:

应用程序要求使用 SendAsync 而不仅仅是 Send。所以我做了一个类 CEmailServer 并设置了一切。到目前为止,Send 工作正常,但是当将其更改为与 SendAsync 一起使用时,它就不行了。我创建了一个在发送邮件并且 userToken 就位但它一直失败时调用的方法。我找不到我的错误。这是我的代码:

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("someadress.kimberley@gmail.com", "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("someadress.kimberley@gmail.com", "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;
}

非常感谢!

【问题讨论】:

  • 我真诚地希望这不是您的实际密码。
  • 不,旧帐户已被删除
  • 但以防万一使用不同的凭据:)

标签: c# smtp sendasync


【解决方案1】:

您将在发送完成之前处置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);

【讨论】:

  • @Mordacai1000 您是否也将messageusing 语句中删除了?此外,如果您能提供异常的详细信息,将会很有帮助。
  • 我没有删除消息。这是错误:System.Net.Mail.SmtpException:发送邮件失败。 ---> System.ObjectDisposedException:无法访问已处置的对象。对象名称:“System.Net.Mail.MailMessage”。在 System.Net.Mail.MailMessage.get_AlternateViews() 在 System.Net.Mail.MailMessage.SetContent(Boolean allowUnicode) 在 System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult 结果) --- 内部异常堆栈跟踪结束 - --
  • 非常感谢,早上测试一下
  • 这里的任何人都可以告诉我应该在 SendCompleted 事件处理程序中首先调用谁的 dispose() 方法,即 msg.dispose( )smtpclient.dispose() ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 2014-06-07
  • 2017-01-19
  • 1970-01-01
  • 2014-11-15
  • 2012-01-04
相关资源
最近更新 更多