我尝试遵循这个链接异步调用,但是一些类已经过时了。
因此,我想要一个确切的答案来完成我的项目。
public class RegisterInfo
{
public bool Register(UserInfo info)
{
try
{
using (mydatabase db = new mydatabase())
{
userinfotable uinfo = new userinfotable();
uinfo.Name = info.Name;
uinfo.Age = info.Age;
uinfo.Address = info.Address;
db.userinfotables.AddObject(uinfo);
db.SaveChanges();
// Should be called asynchronously
Utility.SendEmail(info); // this tooks 5 to 10 seconds or more.
return true;
}
}
catch { return false; }
}
}
public class UserInfo
{
public UserInfo() { }
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
public class Utility
{
public static bool SendEmail(UserInfo info)
{
MailMessage compose = SomeClassThatComposeMessage(info);
return SendEmail(compose);
}
private static bool SendEmail(MailMessage mail)
{
try
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.something.com";
client.Port = 123;
client.Credentials = new System.Net.NetworkCredential("username@domainserver.com", "password");
client.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
client.Send(mail);
return true;
}
catch { return false; }
}
}
请查看
Register
方法。
保存数据后,我不希望等待邮件的发送。如果可能的话,我想在其他线程上处理邮件的发送,以便用户不必等待更长的时间。我不需要知道邮件是否成功发送。
希望您能理解我的意思。对于我的糟糕英语表示抱歉。