我有以下代码,用于在上传摘录时向大约60位用户发送电子邮件提醒。它将发送到所有帐户,如果我在本地运行它,但当我上传到服务器时,它不会发送任何电子邮件,除非它只发给一个人。我在托管和服务器方面没有太多经验,所以你能给我的任何帮助都会很棒。
if (Session["ExtractNo"].ToString() == "Extract 1")
{
//Connection String (SendEmail)
string SendEmail = ConfigurationManager.ConnectionStrings["Sendmail"].ConnectionString;
SqlDataReader reader;
String SendMessage = "SELECT Name, Position, Email FROM AuthorisedStaff Where Position = 'CM' or Position = 'DHOD' or Position = 'HOD'"; //<---- change position before launch
using (SqlConnection myConnection = new SqlConnection(SendEmail))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(SendMessage, myConnection);
ArrayList emailArray = new ArrayList();
reader = myCommand.ExecuteReader();
var emails = new List<EmailCode>();
while (reader.Read())
{
emails.Add(new EmailCode
{
Email = Convert.ToString(reader["Email"]),
Name = Convert.ToString(reader["Name"]),
Position = Convert.ToString(reader["Position"])
});
}
foreach (EmailCode email in emails)
{
//Email Config
const string username = "roll@test.ac.uk"; //account address
const string password = "######"; //account password
SmtpClient smtpclient = new SmtpClient();
MailMessage mail = new MailMessage();
MailAddress fromaddress = new MailAddress("roll@test.ac.uk", "PTLP"); //address and from name
smtpclient.Host = "omavex011"; //host name for particular email address
smtpclient.Port = 25; //port number for particular email address
mail.From = fromaddress;
mail.To.Add(email.Email);
mail.Subject = ("PTLP Check");
mail.IsBodyHtml = true;
//change context of message below as appropriate
mail.Body = HttpUtility.HtmlEncode(email.Name) + " <br /> <p>Part Time Payroll details are now available for checking.</p> ";
//smtpclient.EnableSsl = true;
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
smtpclient.Send(mail);
}
}
}
答案 0 :(得分:1)
正如我在评论中所提到的,这是你如何使用单个SmtpClient实例:
if (Session["ExtractNo"].ToString() == "Extract 1")
{
//Email Config
const string username = "roll@test.ac.uk"; //account address
const string password = "######"; //account password
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = "omavex011";
smtpclient.Port = 25;
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
string SendEmail = ConfigurationManager.ConnectionStrings["Sendmail"].ConnectionString;
SqlDataReader reader;
String SendMessage = "SELECT Name, Position, Email FROM AuthorisedStaff Where Position = 'CM' or Position = 'DHOD' or Position = 'HOD'"; //<---- change position before launch
using (SqlConnection myConnection = new SqlConnection(SendEmail))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(SendMessage, myConnection);
ArrayList emailArray = new ArrayList();
reader = myCommand.ExecuteReader();
var emails = new List<EmailCode>();
while (reader.Read())
{
emails.Add(new EmailCode { Email = Convert.ToString(reader["Email"]),
Name = Convert.ToString(reader["Name"]),
Position = Convert.ToString(reader["Position"])
});
}
foreach (EmailCode email in emails)
{
MailMessage mail = new MailMessage();
MailAddress fromaddress = new MailAddress("roll@test.ac.uk", "PTLP"); //address and from name
mail.From = fromaddress;
mail.To.Add(email.Email);
mail.Subject = ("PTLP Check");
mail.IsBodyHtml = true;
mail.Body = HttpUtility.HtmlEncode(email.Name) + " <br /> <p>Part Time Payroll details are now available for checking.</p> ";
smtpclient.Send(mail);
}
}
smtpclient.Dispose();
}