sending email to registered users in ASP.NET - asp.net

I'm working on a ASP.NET web site (VS2008,C#), I'm going to send email to my users, what are my options? what do I need in terms of host or server? can I use a shared host? what services should my server provide in order to be able to send email? is there any sample code for sending email?
thanks

You can use google as thesmtp server (smtp.gmail.com)
Int32 port = 465;
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
MailMessage message = new MailMessage("fromemailaddress", "toemailaddress");
message.Body = "Hello!";
message.Subject = "Hello!";
client.Send(message);
Also, you can put the credentials in the web.config file so you don't have to specify them wherever you send mail in your code.
Don't forget to include the following:
using System.Net.Mail;

All you really need is access to an SMTP server of some kind. This may be available from your host but they could be anywhere - all you need is an IP address and credentials.
Sending an e-mail is trivial, take a look here.

In order to send an email you need to provide following details :
Host Name
Port
If it's not SMTP server that do not need to pass NetworkCredential.
You can you Google or Hotmail host server.
For Google host server
host name "smtp.gmail.com"
port 587
SSL connection Yes
For Hotmail host server
host name "smtp.live.com"
port 587/25
SSL connection False
You can use following classes :
MailMessage
SmtpClient
Furthermore all the settings above in Asp.net can be set in webconfig
<configuration>
<system.net>
<mailSettings>
<smtp from="example#domain.com" deliveryMethod="Network">
<network host="smtp.gmail.com" port="587"
userName="example#domain.com" password="password"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
Than see sample code
MailMessagemessage = new MailMessage("greg#gmail.com");
message.From = new MailAddress("example#domain.com", "Greg");
SmtpClientclient = new SmtpClient();
client.EnableSsl = true;
client.Send(message);

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

sending mails with SmtpClient

This is my first time to write a program that sends mails. I don't know what to put in the SMTP client constructor :
SmtpClient client = new SmtpClient(????);
Can anybody help?
Always consult the documentation. The SmptClient has three constructors.
SmtpClient()
SmtpClient(String)
SmtpClient(String, Int32)
If you choose the last one, then your code would look like the following, for gmail:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
You could also use the first constructor and set properties instead.
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("<email_from>", "password");
smtp.Host = "smtp.gmail.com";
This is your host and optional port.
For example:
SmtpClient client = new SmtpClient("mail.domain.com", 123);
For more information, you should read the MSDN documentation for this class:
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
You can specify the SMTP host and port in code as others have suggested.
But if you're always using the same host and port, it's probably easier and more flexible to use the default SmtpClient constructor, and specify the host and port in the <smtp> element of your application configuration file:
using(var smtpClient = new SmtpClient())
{
...
}
<system.net>
<mailSettings>
<smtp deliveryMethod="network" from="ben#contoso.com">
<network
host="localhost"
port="25"
defaultCredentials="true"
/>
</smtp>
</mailSettings>
<system.net>
One advantage of this is that you can use a different configuration in your development/test environment, such as the one below, which will avoid sending unwanted mails to your system's mail recipients without any code changes.
<smtp deliveryMethod="SpecifiedPickupDirectory" from="ben#contoso.com">
<network host="localhost"/>
<specifiedPickupDirectory pickupDirectoryLocation="C:\temp\mail\"/>
</smtp>
public SmtpClient(
string host,
int port
)
Basically you need to pass host-name and port-number
Please read more about SmtpClient Constructor
This is slightly off-topic, but supports a best practices approach relating to modern email delivery over SMTP...
I would recommend always using a service that supports TLS over SSL as it provides more secure transmissions.
If you're unfamiliar with TLS, #gideon provided an example of using TLS with GMail and here is an article which supports this approach: C# ASP.NET Send Email via TLS

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();

Unable to send to all recipients localhost asp.net

Trying to send email via ASP.NET (classic ASP on the server works fine) and getting the "Unable to send to all recipients." error. Mail server is setup on localhost, Windows 2003 server 64-bit.
Web Config is as follows:
<mailSettings>
<smtp from="rob.hudson#ttu.edu">
<network host="127.0.0.1" port="25" defaultCredentials="true" />
</smtp>
</mailSettings>
Code that generates:
MailMessage mm = new MailMessage();
mm.From = new MailAddress("rob.hudson#ttu.edu");
mm.To.Add(email);
mm.Bcc.Add("rob#iteachwriting.com");
mm.CC.Add("susan.lang#ttu.edu");
mm.Subject = "Your ENGL" + course + "-" + section+ " RaiderWriter account";
mm.Body = sb.ToString();
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Send(mm);
If you have more than one invalid/non-existent email address in your recipients, this error message might happen on some email server, and on our email server the case applies for those email addresses are no longer valid because the employees left the company. Please let me know if you find any other possible cause.
Check if the sending mailbox is full - over the allocated quota.
that can be if:
1)the user or pass are wrong
2)not enable SSL
3)less secure app is not enable
4)you have not log in in server with this mail
In our case the reason was that the SMTP-Server refused messages sent by a machine with a specific IP-Address.
SMTP-Server was part of IIS and there was a list of IPs configured which were allowed to use this SMTP-Server. The client machine in question was not part of the list so we got "Unable to send to all recipients." (or "Senden an alle Empfänger nicht möglich." in german).
Sending from a "valid" client worked.
The error message from the SMTP-Server in this case was pretty misleading.
i struggle a lot and found that there were issue with my Application Pool. I set defaultAPPPool and my code worked.

Is it possible to send an email via google apps without an ssl cert?

I'm trying to send email via google apps from my asp.net app and am getting the following exception:
{"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at "}
My Web.config has the following values (sorry I couldn't get the XML to display):
<system.net>
<mailSettings>
<smtp from="user#domain.com"
deliveryMethod="Network">
<network host="smtp.gmail.com"
port="587"
userName="user#domain.com"
password="password"
enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
I'm thinking that this is due to my not having an SSL cert but I'm not sure. Any thoughts?
I have it working this way with one of our gmail account (code is simplified):
SmtpClient client = new SmtpClient("smtp.gmail.com", "smtp.gmail.com");
client.EnableSsl = true;
client.Credentials = new NetworkCredential(userName, userPassword);
client.Send(mail); // mail is of type System.Net.Mail.MailMessage
You could try to send an email with this code and your configuration just to see if you receive the same error message.
Your code should work fine; I use the exact same configuration successfully.
You may have a firewall issue.

Resources