Setting up SMTP loally without username, password - c#

I am trying to setup SMTP relay to send mails from the web application without username and password. I read that you can setup locally on your IIS and use "No Authentication"
In web.config, these are my settings:
<appSettings>
<add key="SmtpServerAddress" value="localhost" />
<add key="SmtpServerPort" value="25" />
<add key="SmtpServerTimeout" value="30" />
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="">
<network host="localhost" port="25" />
</smtp>
</mailSettings>
And my code-behind to send email is :
SmtpClient sc = new SmtpClient();
sc.Host = "localhost";
sc.Port = 25;
sc.UseDefaultCredentials = true;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
sc.Send(mm);
}
catch (Exception ex)
{
throw ex;
}
When I submit the click event, to send mail, I am getting "remote server not found".
Could you shed some light on this ?

You cannot add localhost and start sending emails (unless there actually IS an SMPT server running). You still need a "real" SMTP server. Those settings a nothing more then adding a default SMTP server in the Web.Config.
If you don't set those settings in IIS, you send mail like this. There are may examples of this on SO.
using (SmtpClient client = new SmtpClient())
{
client.Host = "mail.fakedomain.nl";
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("fake#fakedomain.nl", "abcd123");
//send mail
}
But if you set the default settings in IIS, you can do this
using (SmtpClient client = new SmtpClient())
{
//send mail
}
It saves a few lines of code.
Are you trying this on your development computer? If so you could install hMailServer

Related

"System.Net.Mail.SmtpException" Asp.NET

I was building a project that sends e-mails from my website to an account using SMTP with the Asp.NET Framework with MVC, but, when I try to send the e-mail it catches the next exception
The SMTP server requires a secure connection, or the client did not authenticate. The server response was: 5.7.0 Authentication Required
This is the code inside the try
MailMessage message = new MailMessage();
message.From = new MailAddress("myemailfortesting#gmail.com");
message.To.Add(new MailAddress(contact.Email));
message.Subject = "Test";
message.IsBodyHtml = true;
message.Body = "Name:" + contact.Name+ "Message:" + contact.Text;
using (SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.Credentials = new NetworkCredential("myemailfortesting#gmail.com", "p455w0rD");
smtpClient.EnableSsl = true;
smtpClient.Send(message);
}
The connection string in Views/Share/Web.config
<system.net>
<mailSettings>
<smtp from="myemailfortesting#gmail.com">
<network host="smtp.gmail.com" port="587" userName="myemailfortesting#gmail.com" password="p455w0rD" defaultCredentials="true" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
I've already turn on the Less secure apps in my destination Google account.
What could be wrong here?

C# Email with Button_Click Event

I'm trying to create a Button_Click event that sends an email to a gmail account. This is the error I'm getting:
Unable to read data from the transport connection: net_io_connectionclosed.
It's pointing out Line 63 which is:
client.Send(mail);
Here is the code:
protected void Button2_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
SmtpClient client = new SmtpClient();
client.Port = 465;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress("yourclassroomconnection#gmail.com"));
mail.From = new MailAddress("yourclassroomconnection#gmail.com");
mail.Subject = "New Order";
string bodyTemplate = Label2.Text;
mail.Body = bodyTemplate;
client.Send(mail);
}
Any idea where I'm going wrong?
You can use below code as a small test. Try sending email with minimal option. Then add other options like html support. So you can narrow down the problem when you're experimenting a new thing.
try {
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
} catch (Exception ex)
{
}
You need to generate app specific password and use it here instead of your gmail password.
Please read this tutorial also.
http://csharp.net-informations.com/communications/csharp-smtp-mail.htm
Hard coding the username and password (i.e. the credentials) may be sometimes frustrating.
What you can do is, you can add these credentials in web.config file only once. And you are good to go. Here is the better solution.
web.config file code goes as follows:
<configuration>
<appSettings>
<add key="receiverEmail" value ="ReceiverEmailAddressHere"/>
</appSettings>
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="yourclassroomconnection#gmail.com">
<network host="smtp.gmail.com" port="587" enableSsl="true"
userName="YourActualUsername" password="YourActualPassword"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
Please note that you have to change the host according to your gmail account. I am not sure whether the host is correct. I am using outlook to send emails so the host would be smtp-mail.outlook.com
This is how your web.config file would have all the necessary connection credentials that you define at one place at a time. You don't have to use it everytime you use the Email functionality in your application.
protected void btnSendMail_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
// get the receiver email address from web.config
msg.To.Add(ConfigurationManager.AppSettings["receiverEmail"]);
// get sender email address path from web.config file
var address = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string emailAddress = address.Network.UserName;
string password = address.Network.Password;
NetworkCredential credential = new NetworkCredential(emailAddress, password);
msg.Subject = " Subject text here "
}
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Send(msg); // send the message
The key point here is to access the sender's email address and receiver's email address. Note that I have used (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp"); This will navigate your web.config file and search through the hierarchy available in it - Grabs the email address, fails if it doesn't get email address.
Hope this helps. Happy coding!

not able to send email

My website is hosted on a Godaddy server. I am trying to send an email but i am getting "The operation has timed out." error. I have tried following links but its not working.
GoDaddy email sending failed
and
Not Able To Send Email Via C#
I have tried so far below code.
In web.config
<system.net>
<mailSettings>
<smtp>
<network host="smtpout.secureserver.net" userName="username" password="secret" />
</smtp>
</mailSettings>
</system.net>
and in code behind
var SmtpClient = new SmtpClient();
SmtpClient.Send(frommail,tomail,subject,body);
I have also tried below code
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(txtForgotEmail.Text));
msg.From = new MailAddress("email");
msg.Subject = "subject";
msg.Body = "msg body"
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("toemail", "secret");
client.Port = 25;
client.UseDefaultCredentials = false;
client.Send(msg);
P.S. i cant even send an email from localhost and also tried
client.EnableSsl = false;
client.UseDefaultCredentials = false;
Try adding port to settings
<network host="smtpout.secureserver.net" userName="username" password="secret" port=25/>
For what ports to use refer,
https://sg.godaddy.com/help/what-do-i-do-if-i-have-trouble-connecting-to-my-email-account-319
try all 25, 80, 3535 and 465 with SSL

Sending mail Using C# asp.net [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
send email asp.net c#
I have sent mail several times using the technique several times before but somehow it doesnt work i am providing the code in the following:
MailMessage myMailMessage = new MailMessage();
myMailMessage.Subject = "Response From Client";
myMailMessage.Body = "hello word";
myMailMessage.From = new MailAddress("mymail#gmail.com", "jub");
myMailMessage.To.Add(new MailAddress("mymail#gmail.com", "Softden"));
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMailMessage);
and my web.config is:
<mailSettings>
<smtp deliveryMethod = "Network" from="Jubair <mymail#gmail.com>">
<network defaultCredentials="false" enableSsl="true" userName="mymail#gmail.com" password="Mypassword" host="smtp.gmail.com" port="587"></network>
</smtp>
</mailSettings>
it says the smtp server requires a secure connection or the client was not authenticated.. Please help
Try adding something like this
Per Dominic's answer on Stackoverflow look at he following example
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
//UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
//----------------- A simple approach below ---------------
I just tested this below and it works
var mail = new MailMessage();
// Set the to and from addresses.
// The from address must be your GMail account
mail.From = new MailAddress("noreplyXYZ#gmail.com");
mail.To.Add(new MailAddress(to));
// Define the message
mail.Subject = subject;
mail.IsBodyHtml = isHtml;
mail.Body = message;
// Create a new Smpt Client using Google's servers
var mailclient = new SmtpClient();
mailclient.Host = "smtp.gmail.com";//ForGmail
mailclient.Port = 587; //ForGmail
// This is the critical part, you must enable SSL
mailclient.EnableSsl = true;//ForGmail
//mailclient.EnableSsl = false;
mailclient.UseDefaultCredentials = true;
// Specify your authentication details
mailclient.Credentials = new System.Net.NetworkCredential("fromAddress#gmail.com", "xxxx123");//ForGmail
mailclient.Send(mail);
mailclient.Dispose();
//The .config settings there are two options on how you could set this up I am suspecting that this is the issue you are having
<system.net>
<mailSettings>
<smtp from="from#gmail.com" deliveryMethod="Network">
<network userName="from#gmail.com" password="mypassword" host="smtp.gmail.com" port="587"/>
</smtp>
</mailSettings>
</system.net>
or option 2
<configuration>
<appSettings>
<add key="smtpClientHost" value="mail.localhost.com"/> //SMTP Client host name
<add key="portNumber" value="587"/>
<add key="fromAddress" value="yourEmailAddress#gmail.com"/>
</appSettings>
</configuration>
Send Email from Yahoo, Gmail, Hotmail (C#)
http://www.codeproject.com/Tips/520998/Send-Email-from-Yahoo-Gmail-Hotmail-Csharp
These are evry good tutorials with samples. make your demo email id. pass your id and its password as parameters. and send mail to anyone.
http://www.codeproject.com/Articles/15807/Easy-SMTP-Mail-Using-ASP-NET-2-0
http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp
http://www.codeproject.com/Tips/490604/Sending-mail-using-ASP-NET-with-optional-parameter
if you are still unable to send email make sure to change the port number. but 587 should work normally.
Make sure you contact the email server side to see what kind of authentication they accept to relay.

SmtpClient .send() error: "message rejected as spam by content filtering"

Here is the code i wrote to send email,
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
sc.UseDefaultCredentials = false;
try
{
m.From = new MailAddress(Sender);
m.To.Add(new MailAddress(Receiver));
m.Subject = Subject;
m.IsBodyHtml = true;
m.Body = Body;
sc.Send(m);
}
catch (Exception ex) { _Exceptions.ManageExceptions(ex); }
And the config file settings:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="xxx#xxx.com">
<network host="192.168.0.170" userName="setsdom01\user1" password="xxx" port="25" />
</smtp>
</mailSettings>
</system.net>
It executes when i run it from my machine, i try on different PC and it is giving me the following message: ...message rejected as spam by content filtering..
What could be the problem?
I think you need to add NetworkCredential backend code.
var AuthenticationDetails = new NetworkCredential("xxx#", "xxxx");
sc.Credentials = AuthenticationDetails;
This might work
Also look this How to Enable and Configure the Spam Confidence Level Thresholds may be helpful

Categories