ASP.net Override smtp FROM with different FROM addresses in codebehind - asp.net

I have a web site where the from mail settings are configured in web.config with
<smtp from="xxx#ourdomain.com">
One of the pages on the website sends e-mails. This page can only be access by certain members who each have their own e-mail address under the domain setup in web.config.
What I would like to be able to do is change the from address based upon the logged in users e-mail address. The code is setup and works fine with 1 exception, when the to recipient receives the e-mail it is listed as from xxx#ourdomain.com on behalf of someuser#ourdomain.com. What I would like to see is for the recipient to see the from as someruser#domain.com.
Is this specific to our email server (Arvixe) or can this be fixed via code? As an alternate is there a way to programmatically change the web.config smtp FROM setting for the individual user? I have to believe that there is another solution out there than purchasing a third party solution such as aspNetEmail.
Web.config settings:
<mailSettings>
<smtp from="xxx#ourdomain.com">
<network host="mail.somehost.com" port="26" userName="xxx#ourdomain.com" password="*****" />
</smtp>
</mailSettings>
Portion of code behind used to send e-mail:
Dim EmailTo As String = item(1).ToString
Dim objMail As New System.Net.Mail.MailMessage
objMail.Attachments.Add(imgAtt)
objMail.Subject = ASPxTextBox1.Text.ToString
objMail.From = New System.Net.Mail.MailAddress("someuser#ourdomain.com")
objMail.Body = message
objMail.IsBodyHtml = True
objMail.To.Clear()
objMail.To.Add(New System.Net.Mail.MailAddress(EmailTo))
smtp.Send(objMail)

Related

System.Net.Mail.SmtpClient - Client does not have permission to send as this sender

I have a need to send an email via a web application but the address that the email goes to requires SMTP authentication. The code works for mailboxes that don't need authentication.
I would like to pass across Windows user credentials (the site uses Windows authentication) and thought this would be achieved with my code, but unfortunately I get errors about the client not having permission to send. I'm assuming it is using the credentials of the application pool user rather than the logged in user.
Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
mailMessage.IsBodyHtml = True
Dim strBody As String = "<font size='2' font face='Tahoma'>" & _
"<br><br><b>Date: </b>" & Me.txtDate.Text
mailMessage.From = New System.Net.Mail.MailAddress(fromAddress) 'Logged in user's email address
' More code here to build the email body etc...
' Then attempt to send it:
Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient
smtpClient.UseDefaultCredentials = True
smtpClient.Send(mailMessage)
Web.config contains only the SMTP server name under system.net (mailsettings->smtp)
How do I get the application to use the user's credentials?
Did you specify the UseDefaultCredentials in the Web.config? If not you must do that to use them. This will allow the currently logged in user to send email trough the specified SMTP server. Note that websites usually run under a separate user credential, not the currently logged in user on the machine.
<system.net>
<mailSettings>
<smtp deliveryMethod="network">
<network host="localhost" port="25" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
Or specify the credentials in code.
smtpClient.Credentials = new NetworkCredential("fake#false.nl", "abcd1234");
If both do not work you simply are not authorized to send mail on that server.
See for more info
https://msdn.microsoft.com/nl-nl/library/system.net.mail.smtpclient.usedefaultcredentials(v=vs.110).aspx
https://msdn.microsoft.com/nl-nl/library/w355a94k(v=vs.110).aspx

GoDaddy Send Email

I am using ASP.Net 4.0 with MVC 3 and C# to try and send an email from my site. This code works on other hosts but for some reason GoDaddy is erroring out. Here is the code I am using.
var fAddress = new MailAddress("customers#email.com");
var tAddress = new MailAddress("mygodaddyaddress#email.com");
var message = new MailMessage(fAddress, tAddress)
{
Subject = subject,
Body = body
};
var client = new SmtpClient("relay-hosting.secureserver.net");
client.Send(message);
Here is the error I am receiving
Mailbox name not allowed. The server response was: sorry, your mail was administratively denied. (#5.7.1)
Any other GoDaddy users here that can shed some light?
It might be something to do with the from address:
Problem seem to be the FROM email address. The FROM email address must be an email address with the hosted domain to avoid this error. For example if you have a hosted domain yourdomain.com, your FROM email address should be something like username#yourdomain.com.
Source:
http://www.cosmocentral.com/2009/01/553-sorry-your-mail-was-administratively-denied/
It could be because neither email address is native to the mail server used by GoDaddy, and thus you'd need to allow relaying. This is just one possibility, I think the best approach would be to contact them directly.
Your From address does not need to be a GoDaddy email. It is the user/password for the domain that must be valid. This is what the Web.config should have:
<system.net>
<mailSettings>
<smtp from="me#test.com">
<network host="smtpout.secureserver.net" port="80" userName="foo#bar.com" password="yourpassword" defaultCredentials="false" />
</smtp>
</mailSettings>
</system.net>
Then you can initialize it in code-behind with this:
var mailclient = new SmtpClient();

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.

ASP.NET ChangePassword and PasswordRecovery not sending emails

I am using the ASP.NET membership provider controls (ChangePassword and PasswordRecovery) and they generally work just fine.
Unfortunately they don't send any emails once the process is finished.
I set up the SMTP server using the ASP.NET Configuration Website and it added the following lines to the web.config:
<system.net>
<mailSettings>
<smtp from="maildaemon#pagetailors.de">
<network host="pagetailors.de" password="XXXX" userName="XXXX" />
</smtp>
</mailSettings>
</system.net>
I also tried using the IP adress of the server instead of the domain.
In addition to that I set the maildefinition properties on the used web controls:
<MailDefinition From="maildaemon#pagetailors.de"
Subject="Your new password">
</MailDefinition>
Once I finish changing or resetting the password, the controls shows the success message. It does not show any errors but the mails are not sent.
I also checked the SMTP log files on the server (it's a MailEnable server) but couldn't even find an attempt to send these messages.
I am using a custom email helper class which manually sends an email using the following settings:
public static void SendEmail(string from, string to, string subject, string body)
{
SmtpClient mailClient = new SmtpClient(ConfigReader.GetStringValue("MailServer"));
mailClient.Credentials = new NetworkCredential(ConfigReader.GetStringValue("MailUsername"),
ConfigReader.GetStringValue("MailPassword"));
MailMessage mailMessage = new MailMessage(from, to, subject, body);
mailClient.Send(mailMessage);
}
The values for mailserver, username and password read from the config file are the same as defined in the ASP.NET configuration tool. These mails are delivered successfully.
Any ideas?
Thanks in advance!
Alright, i found a workaround:
I can call my custom EmailHelper class in the passwordRecovery.OnSendingMail event.
Strangely the message isn't sent if I set e.Cancel to true AFTER calling my helper (who says it was sent successfully). If I leave e.Cancel = false, everything works.

can an asp.net page send an email to the user

My aspx page is hosted by Discountasp.net. I can use System.Net.Mail.MailMessage to send an email but it seems it must be TO my Discountasp.net acct. (They let you set email accts for your site.)
I want a form that does a calc and sends the info directly to the user who has typed in their email addr.
Here is a link to the DiscountASP.NET "How to send email in ASP.NET 2.0" FAQ: https://support.discountasp.net/KB/a364/how-to-send-email-in-aspnet-20.aspx . It looks like you use "localhost" as your SMTP server, try the demo and see if it works for you. Good luck!
First, you need to check with your ISP what smtp settings they use (and potentially how many emails you get to send before being blacklisted as spammer, depending on what you're going to use this for...)
Secondly, when you have the correct setting in web.config, you should be able to send to anyone.
EDIT, in response to comment:
To be able to use System.Net.Mail properly, you should add the smtp settings (which you need to get from the ISP/Hosting service) to web.config as follows:
<configuration>
<system.net>
<mailSettings>
<smtp from="test#foo.com">
<network host="smtpserver1" port="25" userName="username"
password="secret" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
</configuration>
See this tutorial for more information.
I don't think that they would limit you to sending mail out only to your discountasp.net account. I would imagine that you might be doing something wrong before I would imagine this is a limitation to discountasp.net.
If it does turn out to be a limitation, you should get on the horn with customer service there and have them either clear up the confusion for you.
Using System.Net.Mail.MailMessage you should be able to set any SMTP address you want.
Dim message As New MailMessage("address#address.com", "address2#address.com")
message.Subject = "MessageSubject"
message.Body = "MessageBody"
Dim client As New SmtpClient(*EmailServerAddress*)
client.Send(message)
dim mailObj as new MailMessage
mailObj.From = {from address}
mailObj.To = {to address}
mailObj.Subject = {subject}
mailObj.BodyFormat = MailFormat.Html
mailObj.Body = {body of message}
SmtpMail.SmtpServer = {mailserver name or IP}
SmtpMail.Send(mailObj)

Resources