民间,
我想要异步发送一些不同主题和正文的电子邮件。这是我的代码
Email.cs
public string To;
public string CC;
public string Subject;
public string Host;
public string Port;
public string Body;
public MailMessage mail;
public SmtpClient smtp;
public void send()
{
smtp = new SmtpClient();
mail = new MailMessage();
mail.To.Add(To);
if (this.CC !="" && this.CC !=null) mail.CC.Add(CC);
mail.CC.Add(CCIDBizzMail);
mail.Subject = this.Subject;
mail.From = new MailAddress(From);
mail.IsBodyHtml = true;
smtp.Host = this.SMTPAddress;
mail.Body = this.Body;
smtp.Credentials = new System.Net.NetworkCredential
(this.From, this.Password);
smtp.EnableSsl = false;
smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
smtp.SendAsync(mail, null);
}
private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
String token = (string)e.UserState;
if (e.Cancelled)
{
}
if (e.Error != null)
{
}
else
{
mail.Dispose();
smtp.Dispose();
}
}
这是我发送电子邮件的代码:
Email objEmail = new Email();
objEmail.Subject = "Thank You for Your Order!";
objEmail.Body = "first email";
objEmail.To: "ssss@mail.com"
objEmail.Send();
objEmail.Subject = "Thank You for Your Order!";
objEmail.Body = "second email";
objEmail.To: "tttt@mail.com"
objEmail.Send();
但是,tttt@mail.com从未收到过电子邮件。我的网站总是发送到ssss@mail.com
你可以帮我解决这个问题吗?答案 0 :(得分:0)
在这里,您尝试发送一封具有异步类型的电子邮件,而不是制作新的对象电子邮件。
我建议尝试两个想法。
在每封电子邮件发送中重新制作
{
Email objEmail = new Email();
objEmail.Subject = "Thank You for Your Order!";
objEmail.Body = "first email";
objEmail.To: "ssss@mail.com"
objEmail.Send();
}
{
Email objEmail = new Email();
objEmail.Subject = "Thank You for Your Order!";
objEmail.Body = "second email";
objEmail.To: "tttt@mail.com"
objEmail.Send();
}
或者将电子邮件例程更改为
public string To;
public string CC;
public string Subject;
public string Host;
public string Port;
public string Body;
public void send()
{
using(var smtp = new SmtpClient())
{
using(mail = new MailMessage())
{
mail.To.Add(To);
if (this.CC !="" && this.CC !=null) mail.CC.Add(CC);
mail.CC.Add(CCIDBizzMail);
mail.Subject = this.Subject;
mail.From = new MailAddress(From);
mail.IsBodyHtml = true;
smtp.Host = this.SMTPAddress;
mail.Body = this.Body;
smtp.Credentials = new System.Net.NetworkCredential
(this.From, this.Password);
smtp.EnableSsl = false;
// maybe here you place extra code for the errors
// http://msdn.microsoft.com/en-us/library/swas0fwc.aspx
smtp.Send(mail);
}
}
}
如果使用localhost发送电子邮件,并且您希望发送许多电子邮件,最好以正确方式发送,而不是异步发送。