Asynchronously sending Emails in C#?

后端 未结 10 1434
礼貌的吻别
礼貌的吻别 2020-11-28 23:43

I\'m developing an application where a user clicks/presses enter on a certain button in a window, the application does some checks and determines whether to send out a coupl

10条回答
  •  盖世英雄少女心
    2020-11-29 00:29

    It's not too complicated to simply send the message on a separate thread:

    using System.Net.Mail;
    
    Smtp.SendAsync(message);
    

    Or if you want to construct the whole message on the separate thread instead of just sending it asynchronously:

    using System.Threading;
    using System.Net.Mail;
    
    var sendMailThread = new Thread(() => {
        var message=new MailMessage();
        message.From="from e-mail";
        message.To="to e-mail";
        message.Subject="Message Subject";
        message.Body="Message Body";
    
        SmtpMail.SmtpServer="SMTP Server Address";
        SmtpMail.Send(message);
    });
    
    sendMailThread.Start();
    

提交回复
热议问题