How do I use TLS email with web.config - asp.net

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

Related

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

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)

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

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.

Error on Sending mail asp.net mvc

Hi whenever i am trying to send the mail from my application on account creation i get the following error.
User not local; please try a different path. The server response was: Bad Recipient at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
Smtp settings provided by client is all right.When i replace my smtp setting with gmail smtp settings on web.config mail is going smoothly.but when my smtp setting is set to the smtp setting provided by client above error occurs.
I have the folloeing in my web.config.
<mailSettings >
<smtp deliveryMethod="Network" from="from addressm" >
<network defaultCredentials="true" host="hostname" port="25" userName="username" password="password" />
</smtp>
</mailSettings>
The email address you are using for the To address is does not exist on the SMTP server and was therefore refused.
Make sure you use an email address that exists.
See this forum post for more detail.
Bad Recipient error means that address you want to send some mail does not exist. Check it carefully

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