我想使用以下代码发送邮件:
public void Semail(string subject, string messageBody, string toAddress)
{
MailMessage mail = new MailMessage();
mail.To.Add(toAddress);
//mail.To.Add("amit_jain_online@yahoo.com");
mail.From = new MailAddress("noreplykaramaoozi@eesharif.edu");
mail.Subject = subject;
string Body = messageBody;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "sina.sharif.ir"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("noreplykaramaoozi@eesharif.edu", "******");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
但执行后我得到了这个错误:
Syntax error, command unrecognized. The server response was: Dovecot ready.
这是堆栈跟踪
[SmtpException: Syntax error, command unrecognized. The server response was: Dovecot ready.]
System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) +2176152
System.Net.Mail.SmtpClient.Send(MailMessage message) +2188821
Novitiate.fa.Register.btnLogin_Click(Object sender, EventArgs e) +2948
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707
答案 0 :(得分:7)
SMTP服务器可能存在问题。
尝试使用GMAIL设置,如果邮件正在使用GMAIL服务器,则很可能是您的邮件服务器存在问题。 看起来您使用的是POP3或IMAP服务器而非SMTP服务器,请检查服务器的配置
static void Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}