问题
I have been trying to send an email by C#. I have Googled for various examples and have taken bits and pieces from each and from the standard code which everyone would most probably be using.
string to = "receiver@domain.com";
string from = "sender@domain.com";
string subject = "Hello World!";
string body = "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
client.Credentials = new NetworkCredential("test@domain.com", "password");
client.Send(message);
However, I keep getting an error stating
System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: Access denied - Invalid HELO name (See RFC2821 4.1.1.1)
So, what do I do now? Is SmtpClient supposed to be special and only work on specific SMTP servers?
回答1:
It seems your username/password pair is not authenticating successfully with your SMTP server.
EDIT
I think, I found what's wrong here. I have corrected your version below.
string to = "receiver@domain.com";
//It seems, your mail server demands to use the same email-id in SENDER as with which you're authenticating.
//string from = "sender@domain.com";
string from = "test@domain.com";
string subject = "Hello World!";
string body = "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("test@domain.com", "password");
client.Send(message);
回答2:
Have you tried setting your auth credentials in the web.Config?
<system.net>
<mailSettings>
<smtp from="test@foo.com">
<network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
and your code behind
MailMessage message = new MailMessage();
message.From = new MailAddress("sender@foo.bar.com");
message.To.Add(new MailAddress("recipient1@foo.bar.com"));
message.To.Add(new MailAddress("recipient2@foo.bar.com"));
message.To.Add(new MailAddress("recipient3@foo.bar.com"));
message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);
回答3:
Try this:
string to = "receiver@domain.com";
string from = "sender@domain.com";
string subject = "Hello World!";
string body = "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
// explicitly declare that you will be providing the credentials:
client.UseDefaultCredentials = false;
// drop the @domain stuff from your user name: (The API already knows the domain
// from the construction of the SmtpClient instance
client.Credentials = new NetworkCredential("test", "password");
client.Send(message);
回答4:
In my case, it was a wrong port. The configuration provided by the hosting didn't worked both SSL (465) and no SSL (25). I used MS Outlook to "crack" the configuration, and then copied to my application. It was 587 SSL.
回答5:
encontré la solución después de mucho pelear..
SEND EMAIL WITH SMTP AUTHENTICATION USING ASP.NET
Send email with smtp authentication using ASP.NET Following Codes demonstrates how to send an email with SMTP Authentication using ASP.NET:
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("postmaster@yourdomain.com"); //IMPORTANT: This must be same as your smtp authentication address.
mail.To.Add("postmaster@yourdomain.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "This is from system.net.mail using C sharp with smtp authentication.";
//send the message
SmtpClient smtp = new SmtpClient("mail.yourdomain.com");
//IMPORANT: Your smtp login email MUST be same as your FROM address.
NetworkCredential Credentials = new NetworkCredential("postmaster@yourdomain.com", "password");
smtp.Credentials = Credentials;
smtp.Send(mail);
lblMessage.Text = "Mail Sent";
NOTE: Please note that you cannot use System.Net.Mail.SmtpClient to send an e-mail message with Implicit SSL.
来源:https://stackoverflow.com/questions/3155242/troubleshooting-mailbox-unavailable-the-server-response-was-access-denied-i