0
votes

I am trying to send an email using the C# SmtpClient class. The "catch" block gets executed after it attempts to "send" the message. When I go to look at the exception, the InnerException says "Unable to connect to the remote server" and the message says "Failure sending mail". The StatusCode is "General failure".

Am I missing certain credentials? Do I need to set some more fields to the instance of the SmtpClient or MailMessage?

Here is the code:

 private void sendEmail(string recepientEmailAddress) {
        string subjectEmail = "Subject Email";
        MailAddress to = new MailAddress("[email protected]");
        MailAddress from = new MailAddress("[email protected]");

        MailMessage message = new MailMessage(from, to);
        message.Subject = subjectEmail;
        message.IsBodyHtml = true;
        message.Body = "Body example";

        try
        {
            SmtpClient sc = new SmtpClient(ConfigurationManager.AppSettings["MailServer"].ToString());
            sc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            sc.DeliveryMethod = SmtpDeliveryMethod.Network;
            sc.EnableSsl = true;
            sc.UseDefaultCredentials = true;
            sc.Send(message);
        }
        catch (Exception e)
        {
            Helpers.ErrorLogger.ProcessError(e.Message, e.StackTrace, "RegistrationController", "SendEmail");
        }
    }
2
Seems that the MailServer specified in the AppSettings is not valid. - user700390
missing username and password dude on which email you try to send from - Asaf Shazar
@AsafShazar what's a way to attach the user and password to the email I am sending from? Thanks. - tonyleMill

2 Answers

2
votes
private void sendEmail(string recepientEmailAddress) {
        string subjectEmail = "Subject Email";
        MailAddress to = new MailAddress("[email protected]");
        MailAddress from = new MailAddress("[email protected]");

        MailMessage message = new MailMessage(from, to);
        message.Subject = subjectEmail;
        message.IsBodyHtml = true;
        message.Body = "Body example";

        try
        {
            SmtpClient sc = new SmtpClient(ConfigurationManager.AppSettings["MailServer"].ToString());
            sc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            sc.DeliveryMethod = SmtpDeliveryMethod.Network;
            sc.EnableSsl = true;
            sc.Host="smtp.gmail.com"
            sc.port = 587;
            sc.UseDefaultCredentials = true;
            sc.Send(message);
        }
        catch (Exception e)
        {
            Helpers.ErrorLogger.ProcessError(e.Message, e.StackTrace, "RegistrationController", "SendEmail");
        }
    }

adding port(gmail:587)

adding host(smtp.gmail.com

if that not working try add User name and password change from

sc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

TO

sc.Credentials = new System.Net.NetworkCredential("username","password")
0
votes
public static bool Send()
    {
        try
        {
            var fromAddress = new MailAddress("[email protected]");
            var toAddress = new MailAddress("[email protected]");
            const string subject = "subjects";
            const string userName = "serverCredentail";
            const string Password = "serverpassword";
            const string body = "Body";
            var smtp = new SmtpClient
            {
                Host = "servernameexample.com",
                Port = 25,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(userName, Password)
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            })
            {
                smtp.Send(message);
            }
        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }

This Code should work I just tested it.