After searching through the web and asking around people, I finally realised that, because the project was built few years ago and had the old SMTP server configuration passed into it, it was failing to connect to the server to send the emails. Changing the SMTP server sorted out the problem straight away in my case, but there may be other problems responsible for the same exception being thrown. Sender email address and its credentials must be correctly set (for example, if you are using gmail's SMTP server, you should set the username and password to send as the particular user), or use default credential (Property for SmtpClient).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Mail; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
SmtpClient client = new SmtpClient("smtpmail.example.com"); | |
client.Credentials = new NetworkCredential("username", "password"); | |
MailMessage message = new MailMessage("from@example.com", "to@example.com"); | |
message.Subject = "Subject: test!"; | |
message.Body = "This is a test email"; | |
client.Send(message); | |
} | |
} | |
} |
thank you dear, it works
ReplyDelete