大家好我在使用Windows窗体中的列表框发送批量邮件时遇到问题....当我尝试发送批量邮件时,它显示发送失败..我的代码是什么我不知道我的代码有什么问题检查并检查我不知道有什么问题
void SendMail1(string from,string to,string subject,string message,string host,int port,string un,string pw)
{
MailMessage tm = new MailMessage();
tm.From = new MailAddress(from);
tm.To.Add(new MailAddress(to));
tm.Subject = subject;
tm.Body = message;
tm.IsBodyHtml = true;
tm.Priority = MailPriority.Normal;
tm.SubjectEncoding = Encoding.UTF8;
var smtp = new SmtpClient();
{
smtp.Host = host;
smtp.Port = port;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//default credentials
smtp.Credentials = new NetworkCredential(un,pw);
}
smtp.Send(tm);
MetroFramework.MetroMessageBox.Show(this, "Bulk Mail Successfully Sent.....", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnSendEmail2_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(txtSubject2.Text) || string.IsNullOrEmpty(txtMessage2.Text) || string.IsNullOrEmpty(txtFrom2.Text) || string.IsNullOrEmpty(txtEmailAddress2.Text) || string.IsNullOrEmpty(txtPassword2.Text) || string.IsNullOrEmpty(txtAttachment2.Text))
{
MetroFramework.MetroMessageBox.Show(this, "One Or More Fields Empty..Please Check To See Which Fields Are Empty,Before Sending Mails...", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
//send mails here
foreach(string i in lstEmails.Items)
{
if (i.Trim() != string.Empty)
{
//declare all fields
DataTable dt = new DataTable("tblEmails");
dt.Columns.Add("Email", typeof(string));
dt.Rows.Add(i.ToString());
string _from = txtFrom2.Text.Trim();
string _subject = txtSubject2.Text.Trim();
string _message = txtMessage2.Text.Trim();
string _host = Convert.ToString(ddlProvider2.SelectedItem);
int _port = 587;
string _un = txtEmailAddress2.Text.Trim();
string _pw = txtPassword2.Text.Trim();
Parallel.ForEach(dt.AsEnumerable(), x =>
{
SendMail1(_from, x["Email"].ToString(), _subject,_message
, _host, _port, _un, _pw);
});
// MetroFramework.MetroMessageBox.Show(this, i + "\n", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MetroFramework.MetroMessageBox.Show(this, "Please Load Some Emails To Send Mail Message...", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
}
}
答案 0 :(得分:0)
您正在尝试批量发送电子邮件。如果这有效"有时",那么如果您的发送速率高于管理员设置的阈值,则SMTP服务器很可能拒绝您的邮件。除非管理员更改其政策以适应您的申请,否则在这种情况下您无能为力。或者,您需要一个不同的SMTP服务(亚马逊有一个,仅适用于像您这样的批量邮件应用程序)。