首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SendAsync Smtp邮件错误

SendAsync Smtp邮件错误
EN

Stack Overflow用户
提问于 2014-01-18 20:15:15
回答 1查看 8.7K关注 0票数 1

应用程序需要使用SendAsync,而不仅仅是发送。所以我做了一个类CEmailServer并设置了所有的东西。到目前为止,Send工作得很好,但是当将它更改为与SendAsync一起工作时,它就不工作了。我创建了一个方法,以便在发送邮件时调用userToken,但它始终失败。我找不到我的错误。这是我的代码:

代码语言:javascript
复制
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;
}

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-18 20:26:22

在发送可以完成之前,您正在处理message。您应该在SendCompleted回调事件中处理它们。下面是处理客户端和消息的最佳方法的example

您的代码应该如下所示:

代码语言:javascript
复制
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);
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21209328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档