sending mails with SmtpClient - asp.net

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

Related

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

sending email to registered users in 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);

How do I use TLS email with web.config

Here is my web.config file:
<system.net>
<mailSettings>
<smtp from="xxx#gmail.com" >
<network host="smtp.gmail.com" port="587" userName="xxx#gmail.com" password="yyy" />
</smtp>
</mailSettings>
</system.net>
I need to enable TLS, a requirement of my email server. However I only see SSL.
Its actually equivalent - TLS is kinda of broader than SSL. So use enableSsl= "true" for enabling TLS. As per MSDN documentation, that will force SMTPClient to use RFC 3207
(and RFC uses both terms TLS/SSL).
<network enableSsl="true" host="smtp.gmail.com" port="587" ...
TLS (Transport Level Security) is the slightly broader term that has replaced SSL (Secure Sockets Layer) in securing HTTP communications. So what you are being asked to do is enable SSL.
There is no setting in Web.Config for System.Net.Mail (.net 2.0) that maps to EnableSSL property of System.Net.Mail.SmtpClient.
Resolution
1) In the code behind, We need to consume PasswordRecovery1_SendingMail event of the web control
2) This event provide us access to Email Message being sent and also give us option to cancel the send operation
3) We will make a copy of this email message, and create a new instance of System.Net.Mail.SmtpClient
4) This time we have complete access to its properties and we can turn On/Off the EnableSSL setting
5) Lets set EnableSSL to true and send the email message to desired SMTP server
The below code snippet can do the job.
protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{
System.Net.Mail.SmtpClient smtpSender = new System.Net.Mail.SmtpClient("mail.google.com", 587);
smtpSender.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpSender.Credentials = new System.Net.NetworkCredential("username#gmail.com", "password");
smtpSender.EnableSsl = true;
smtpSender.Send(e.Message);
e.Cancel = true;
}
Repro Steps
1) Configure a PasswordRecovery control
2) Provide all the settings in Web.Config, including username/password, server name, email sender and others
3) Try to send a recovery email when SMTP server requires SSL
Check below link:
http://blogs.msdn.com/b/vikas/archive/2008/04/29/bug-asp-net-2-0-passwordrecovery-web-control-cannot-send-emails-to-ssl-enabled-smtp-servers.aspx

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.

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