SMTPClient Half Working \ Half Not - asp.net

I am using Microsoft's membership framework on a website. I am able to send password retrieval e-mails, but am unable to send e-mails myself using SMTPClient.
When you configure the SMTP settings in the Web Site Administration Tool, what are the settings that this application give it's SMTPclient?
If I know this, I can duplicate it and hopefully send e-mails.
A few items I've ruled out.
- I'm using port 25, (which is the only one allowed under medium trust).
- This code works on my local system.

I was attempting to pass the values for SMTPClient in with the constructor. What I failed to realize is that SMTPClient apparently automatically pulls the values from web.config and uses those.
By trying to pass my own values (even though they where identical); I inadvertently violated trust levels causing a security exception.
Don't pass in smtp info in the constructor, use web.config to set it up, and there should be no problem in medium trust.

It could be lots of things, from credential problems, DNS issues, or...who knows. Is it unable to send as in you get an error or as in they appear to go but never arrive?
Are you sure the WSA tool is going through SMTPClient, or are you just assuming it (I don't know myself)?
-- MarkusQ
I am getting a Security Exception on
my mail
That sounds like a credentials problem then, or a trust issue. Are you sure you're running at medium (or higher) trust? What's the rest of the error?
-- MarkusQ

Try checking your web.config file: is the WSA tool updating these settings?
This element is under the configuration element
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="you#email.com">
<network
host="your.gateway.com"
userName="your#email.com"
password="your_password"
port="25" />
</smtp>
</mailSettings>
</system.net>

Related

Send Email when Error in ASP.Net MVC 5 using ELMAH

I am trying to send email whenever there is an exception in my code using Elmah.
Here is the configuration in web.config file.
<elmah>
<errorMail from="MyEmailId#gmail.com" to="MyEmailId#gmail.com"
async="true" smtpPort="0" useSsl="true" />
</elmah>
<system.net>
<mailSettings>
<smtp deliveryMethod ="Network">
<network host="smtp.gmail.com" port="587" userName="MyEmailId#gmail.com" password="MyEmailPassword" />
</smtp>
</mailSettings>
</system.net>
Now above configuration works fine. I tested in localhost every time I get an exception I am getting an Email Successfully.
Question 1:
Does that mean if I deploy my application with above settings, no
matter who ever access my website, if they get an exception I will
receive an email.
Question 2:
I have added password in above config file. But now everyone can see
my password. Is this the right way or I can still send the mail w/o
putting my password in config file.
Answer to question 1 :
Exceptions will be caught irrespective of the type of error module you
use. So you will definitely receive an email in those exception cases.
Answer to question 2 :
Why are you using your personal mail id? Create a new ID and use it
for the purpose, so that even if others see, it doesn't have any
effect. Others means only the developers will be able to see, not the
public. You can encrypt the web.config file by checking this MSDN
article.
Also consider encrypting your password and storing it in the
web,config file.

System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable.

I get this error when trying to send an email using Asp.net application using SMTP on IIS 7.5
The error is "System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: No such user here at System.Net.Mail.SmtpTransport.SendMail(... "
What i have in web.config is
<system.net>
<mailSettings>
<smtp from="support#mysite.com" deliveryMethod="Network">
<network host="hostname" port="25" password="password" userName="you#yoursite.com" />
</smtp>
</mailSettings>
</system.net>
I really don't understand what should be the username and password here. I get the same error when i put defaultcredentials = "true" instead of username and password. Is there something to do with toaddress. The toaddress is myid#mydomain.com, which works for all other emails, etc.
Please advice me where i am doing wrong!! Thank you in advance!
The userName and password settings refer to the credentials for connecting to the SMTP server. Make sure you don't confuse this with the username and password for the from or to address, which is not relevant to the SMTP server.
You may want to try a tool like SMTPDiag to help you figure out any SMTP connectivity issues you have. However, your error seems to indicate that connectivity is fine but that mail cannot be delivered.
Ram if you are using a web host and you have SMTP support in your package... Then check their control panel.
Also as Jacob I want to remind you that FREE SMTP user accounts that web hosts offer are different from credentials to connect to the SMTP server itself. Usually the former is that's free with web hosts and the latter is not included as long as you don't have a dedicated server I think.
If you don't find it in their control panel then call support... They will be able to give you those details if applicable.

ASP PasswordRecovery Email Failure, DateTime precision

I have an asp.net 4.0 website and I'm using the PasswordRecovery control for a forgot password form. When I run the site locally it works fine, emails are sent. However, when I run the site from my vps, I get an error message when trying to send the email. There's nothing in the server's event log.
My PasswordRecovery aspx code is as follows:
<asp:PasswordRecovery ID="PasswordRecovery1" runat="server"
CssClass="mediumText">
<MailDefinition From="noreply#x.com" BodyFileName="~/EmailTemplates/PasswordRecovery.txt" />
My web.config mail settings are as follows:
<system.net>
<mailSettings>
<smtp from="noreply#x.com">
<network host="smtp.123-reg.co.uk" password="x" userName="x" />
</smtp>
</mailSettings>
</system.net>
I've now run SQL Profiler against the SQL Server Express Instance, and it turns out that an exception is being thrown from the SQL Server on the call to dbo.aspnet_Membership_GetUserByName. There's a type conversion issue because PasswordRecovery is passing a DateTime parameter with 7 decimal places for the seconds. If I manually execute the stored procedure with 3 decimal place,s then it works. Does anyone know why the precision of the DateTime parameter is different on my server than on my laptop?
As you will see from reading this issue and the subsequent responses, the DateTime precision issue you are seeing is just a result of SQL Profiler displaying the date with the wrong format. That issue is unrelated to what is preventing your email from being sent.
What is the error message you receive when you try to send the email? The issue is more than likely related to something preventing your email from making it to the SMTP server. Are you sending to the same SMTP using the same credentials from your machine?
I think it is some connectivity issue from your VPS to SMTP server.
You can test this outside of your code by trying to connect to SMTP server using telnet.
Follow the steps mentioned in the link below to
http://support.microsoft.com/kb/323350
The datetime conversion error is a red herring. That is only occuring when you copy it from the profiler and execute it in management studio. This is a known issue.
You might want to read this:
http://forums.asp.net/t/1398826.aspx/1
Sounds like it might simply be that the membership provider settings are different with regards to the passwords.
There are two possibilities that I see.
You don't have a machineKey defined in your web.config AND your local and VPS instance are both pointing to the same database. If this is true, put a machineKey in your config and regen your passwords.
Your membership provider config on the VPS used to be configured for hashed passwords and somewhere along the way was changed to encrypted passwords. (or vice versa). This would also cause issues.

ELMAH: Can you set it up to email errors only remotely?

While debugging my solution locally, I would like to not receive ELMAH error emails. I'd only like to receive them when the application is hosted on a remote machine. Is this possible?
I faced the same problem and rejected the idea of having different web.configs for local and remote as that just doesn't play nicely with source control (but see comments below).
Instead, I added this to a Global.asax file in each site which uses Elmah:
void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
{
e.OnErrorMailing(); // Prevent local machine errors from being mailed to the rest of the team
}
OnErrorMailing() is an extension method declared in our web library as follows:
/// <summary>
/// Prevent local machine errors from being mailed to the rest of the team. Feel free to add your own machine names/case blocks here
/// </summary>
public static void OnErrorMailing(this ErrorMailEventArgs e)
{
switch (e.Error.HostName.ToLower())
{
case "mymachinename":
e.Mail.To.Clear();
e.Mail.To.Add("MYEMAILADDRESS");
break;
}
}
Of course if you have only one site which uses Elmah or you don't mind code duplication you can put that code straight into global.asax too.
I've no doubt there are more elegant solutions but this works for me. Only I receive the exceptions from my local machine and production exceptions go to the email address configured in web.config.
I'm pretty sure you can do this. Just filter outgoing error emails. Of course use some setting to indicate that it's hosted remotely.
Or just have two different web.configs and make sure "dev one" has emaling turned off.
You can have emails go to a pickup folder on your local machine instead of the SMTP server by modifying the web.config file
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\temp\" />
</smtp>
</mailSettings>
</system.net>
Or you can put this in your Machine.config settings on your dev box so you don't have to keep touching the web.config file.
I found a solution which is to use config transforms with VS 2010. I removed the <errorMail /> tag from my base web.config then added it to my Web.Qa.Config and Web.Release.Config with transform tags. When I publish, it allows the remote machines to send out the emails while they are suppressed locally.

Mailbox unavailable error

When trying to send out an email in a .NET site, the following error is being encountered:
Mailbox unavailable. The server response was: No such user here
Does this error appear if the code is trying to send to an email address which doesn't exist?
Thanks.
I now have more information about this error. The emails are sent from 'noreply#[domain]'. When the emails are sent to an email address of the same domain, the emails are sent without a problem. This error only appears when the email addresses being sent to are not from the same domain. I don't know if that's any use?
This happens when you specify a domain with your NetworkCredentials. If you specify a domain (third argument) then you can only send to valid mailboxes within that domain. Leave it out to be able to send to any address outside the domain.
var client = new SmtpClient("smtp.server.com");
client.UseDefaultCredentials = false;
// The following will be able to send to anyone outside the domain.
client.Credentials = new NetworkCredential("user", "password");
// The following will only work when sending to users on server.com
client.Credentials = new NetworkCredential("user", "password", "server.com");
Could be that your password is incorrect. I had to reset the password on the mail server, then the error went away.
This may happen when you switch from 2.0 platform to 4.0. As it was explained here you need to tell IIS explicitly that you are not using default credentials and domain. Use the following syntax in web.config:
<network host="mail.younameit.com" port="25"
userName="account#younameit.com" password="youchoose"
defaultCredentials="false" clientDomain=""/>
The last two parameters are most important to fix this problem.
This sounds like an smtp issue
Try setting your smtp server info in the web.config file like this :
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network defaultCredentials="false" host="mail.blah.com" password="xxxx" port="25" userName="ex#blah.com"/>
</smtp>
</mailSettings>
</system.net>
This is a decent article detailing this section of the web.config and how to access it with code behind :
http://dotnetslackers.com/Community/blogs/kaushalparik/archive/2008/09/06/accessing-web-config-file-smtp-mail-settings-programmatically.aspx
This Q/A was useful to me in a similar situation. For us, the key fact was that the error only occurred for email addresses on a different domain. I learned that our new webhost/mail server setup is intentionally configured this way. (A previous one with the same hosting co. was not.) Some combination of app code or Web.config settings might have solved our problem, but the most direct way forward was to create a no-reply account on our domain, so that now no-reply#ourdomain.com IS valid, and IS allowed to send to external addresses.
No modifications to code or the Web.config were needed. The latter calls out only "from" and "host" and the credentials are not needed in our hosting environment. (When we override the nominal "from", we need to override it to be some other address that's valid on our domain.

Resources