Hi there,
I am writting an Console application. In the exception handling function, I want to send an email to administrator. I used SmtpClient as recommended in VS2005. But it failed when sending email. I could not figure where is wrong. Could you help
Code:
public
static void HandleException(string errorMessage){
string adminEmail = ConfigurationSettings.AppSettings["adminEmail"]; SmtpClient mail = new SmtpClient("127.0.0.1"); MailAddress from = new MailAddress("myemail@company.com"); MailAddress to = new MailAddress(adminEmail); MailMessage message = new MailMessage(from, to);message.Body =
"Hello Administrator,\n\n" "An exception occured when I tried to update local MySql database with data from live servers. " + "The exception message is: \n\n" + errorMessage + "\n\n" + "Please fix the problem and restart the service to invoke me.\n\n" + "Thank you.";message.Subject =
"An error occured from updating MySql database service on " + Environment.MachineName;mail.Credentials =
CredentialCache.DefaultNetworkCredentials;mail.Send(message); //sending email failed here
throw new Exception(errorMessage);}
}
The error message I got is:
System.Net.Mail.SmtpFailedRecipientException was unhandled
Message="Mailbox unavailable. The server response was: 5.7.1 Unable to relay for pengk@nov.com"
Source="System"
FailedRecipient="<pengk@nov.com>"
StackTrace:
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at WellDataAdapter.Utility.HandleException(String errorMessage) in C:\Projects\WellDataAdapter\Utility.cs:line 27
at WellDataAdapter.MySqlDB.GetLatestDate(SourceDB source) in C:\Projects\WellDataAdapter\MySqlDB.cs:line 47
at WellDataAdapter.WellDataAdapter.Execute() in C:\Projects\WellDataAdapter\WellDataAdapter.cs:line 30
at WellDataAdapter.Program.Main(String[] args) in C:\Projects\WellDataAdapter\Program.cs:line 12
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Any advice is appreciated,
Kai

Why SmtpClient failed in VS 2005?
Rafajna-Ujfalu
I also got the same problem. I fix it by configuring IIS smtp service.
System.Web.Mail is working correctly, and is attempting to send the email. However, your SmtpMail.SmtpServer is not allowing relaying. Try one of the following suggestions:
Mat007
Tom&#225;&#353; Pajonk
Sunkakangi
Hi Friends,
My Suggestion is that simpley replace your smtp server name or smtp ip address with the ip address "127.0.0.1" and it will work.
It worked with me,
I developed a solution which was giving the same error after deploying to the hosting server, when i changed the hosting smtp server with "127.0.0.1" since then its working without error.
KarthikG
I've had the same problem using 2.0 membership provider controls.
The mailmessage from is valid address.
My hosting supplier indicates that their relay settings on their smtp mail server allow for this.
Other than the membership controls, the only code/config that I've looked at is smtp settings in web.config:
<system.net>
<mailSettings>
<smtp from="David@davidconner.com">
<network host="https://mail.davidconner.com" password="" userName="" />
</smtp>
</mailSettings>
</system.net>
To make things more fun... the membership controls were emailing just fine from another project last week. I looked at settings there, and they appear to be indentical. I've read a half dozen posts on this, and having seen any new info. I'm running from local workstation, I used 127.0.0.1 as SMTP to no avail. What the heck
nkbfok
Maybe problem is your authenticue request or your trust to default behavior
Harold van de Kamp
Sorry, this is little help. The email address in error message quoted above is the "to" address for the recipient. The recipient address IS available in my case anyway... so the problem must be elsewhere, despite what "the exception said".
Abel Valadez
if the smtp is exchange you simply need to authenticate w/ user credentials.
put the following in your app.config or web.config
<
system.net><
mailSettings><
smtp from=genericusername@yourdomain.com><
network host="mail.yourdomain.com" port="25" userName="mailbot" password="pass" defaultCredentials="true" /></
smtp></
mailSettings></
system.net>Raymunco Chapa G
The 5.7.1 error - unable to relay - is the result of a security setting in your SMTP server. The response string your seeing is not coming from .NET but from the mail server itself.
So it looks like your code is fine but the mail server has a security setting that is rejecting the message based on the destination address. Most SMTP servers generate this error due to what's known as the relay control settings. These settings are essentially an anti spam mechanism and prevent spammers from using an SMTP server to send e-mail to a foreign domain.
In Exchange (I don't know the breed of SMTP server you're using) the server can be configured to accept messages for specific domains. Any messages bound for domains outside of those settings need to be specifically allowed - either by entering the IP address of the system trying to send the mail or by setting up specific address sets and routes.
So while I haven't solved your problem hopefully you'll be able to track down the cause. Contact your mail administrator or net admin and tell him you're trying to send e-mail from host w.x.y.z to domain Y and could you please be added to the list of allowed relay hosts.
aalexander
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
Regards,
Adeel
Georgia Nelson
Most helpful... I've been trying to enable the send mail function from the login PasswordRecover.aspx in the CSK from ASP.NET 2.0 As it turns out the localhost address is the one that has to be added. Simply adding the ip address of the NIC is not sufficient.
Interestingly enough, it seems unclear in this starter kit how it is that the email is actually generated and sent as to the content. I've stepped through the entire code for the PasswordRecover.aspx page and there is no line of code from what I can see that actually invokes the smptclient class.
Regards,
Carl