伙计们,我有一个类(class),我要向数据库中的所有学生发送邮件。当我在本地主机上发送邮件时,邮件很容易发送,但是当我将代码上传到服务器时。我收到此异常:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at HotelWeb.SMTPHelper.SendEmail(String From, String FromDisplayName, String To, String Cc, String Bcc, String Subject, String Body, Boolean SendMailInBackGround) in e:\hostingspaces\ubi9009\ihmsgr.taqwatechnologies.net\wwwroot\App_Code\SMTPHelper.cs:line 140 at HotelWeb.MAILnSMS.sendBulkMail() in e:\
hostingspaces\ubi9009\ihmsgr.taqwatechnologies.net\wwwroot\App_Code\MAILnSMS.cs:第 95 行
我的代码在本地机器上运行良好,但在服务器上无法正常运行是这样的:
public static void sendBulkMail() {
_ds = new DataSet();
SMTPBAL smtpbl = new SMTPBAL(0);
smtpbl.LoadAll(_ds);
string server = _ds.Tables[smtpbl.SqlEntityX].Rows[0]["SMTPX"].ToString();
int smtpPort = WebHelper.Cast(_ds.Tables[smtpbl.SqlEntityX].Rows[0]["smtpPort"].ToString(), 0);
string From = _ds.Tables[smtpbl.SqlEntityX].Rows[0]["Email"].ToString();
string User = _ds.Tables[smtpbl.SqlEntityX].Rows[0]["User"].ToString();
string Password = _ds.Tables[smtpbl.SqlEntityX].Rows[0]["Password"].ToString();
string sDisclaimer = _ds.Tables[smtpbl.SqlEntityX].Rows[0]["Disclaimer"].ToString();
bool EnableSSl = WebHelper.Cast(_ds.Tables[smtpbl.SqlEntityX].Rows[0]["EnableSll"], false);
string EmailAddress = string.Empty;
StudentBAL _stbl = new StudentBAL(0);
_stbl.LoadAll(_ds);
foreach (DataRow dr in _ds.Tables[_stbl.SqlEntityX].Rows)
{
try
{
if (dr["Email"] != DBNull.Value)
{
if (IsValidMailAddress(dr["Email"].ToString()))
{
string Message = "Dear parent,<br/><br/> <br/> The Attendance for current course/semester of your ward," + dr["Name"].ToString() + " is available on the given below link.Please check and contact HOD in case any enquiry.<br/><br/><br/>Copy below link to your browsers address bar. <br/><br/>http://ihmsgr.taqwatechnologies.net/login/login.aspx <br/>User Name :" + dr["LoginEmailAddress"].ToString() + " / " + dr["EmailAddress"].ToString() + "<br/>Password : " + dr["Password"].ToString() + "<br/><br/><br/>Regard's<br/><br/>Institute of hotel management.<br/>Srinagar.";
SMTPHelper SMTP1 = new SMTPHelper(server, smtpPort, From, Password, EnableSSl, true);
SMTP1.IsHTML = true;
SMTP1.SendEmail(From, "Institute of hotel management", dr["Email"].ToString(), "", "", "Attendance status of your ward.", Message, false);
}
}
}
catch (Exception err) {
}
}
}
我正在使用服务器上的托管空间 http://www.arvixe.com/请帮忙
最佳答案
该错误消息通常是由以下原因之一引起的:
- 不正确的连接设置,例如指定的错误端口 安全或非安全连接
- 凭据不正确。请验证用户名和密码 组合,以确保凭据正确无误。
另外,如果还是不行,尝试将SmtpClient的DeliveryMethod
设置为SmtpDeliveryMethod.Network
以下是相同的代码片段
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
关于c# - 无法在服务器上发送邮件。获取异常 SMTP 服务器需要安全连接或客户端未通过身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23519713/