user3788648
user3788648

Reputation: 31

Send Email With C# (Cpanel Hosting)

I have bought a cPanel host and the SMTP server information is:

info

This is my code:

string smtpAddress = "mandane.hostcream.com";
int portNumber = 465;
bool enableSSL = true;
string emailFrom = "[email protected]";
string password = Authenitication.PassWord;
string emailTo = To.Text;
string subject = Subject.Text;
string body = Body.Text;

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;

    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
    {
        smtp.Credentials = new NetworkCredential(emailFrom, password);
        smtp.EnableSsl = enableSSL;
        smtp.Send(mail);
    }
}

When I run my code and click on the send button after 1 or 2 minutes this appears:

Additional information: Failure sending mail.

What am I doing wrong?

Upvotes: 1

Views: 3753

Answers (2)

G.Nader
G.Nader

Reputation: 857

It seems that you cannot reach Yahoo address on port 465, please check if this address reachable first because it appears to be a network issue.

Upvotes: 0

Sowiarz
Sowiarz

Reputation: 1081

I think you missed something, try this:

SmtpClient smtpClient = new SmtpClient();
NetworkCredential smtpCredentials = new NetworkCredential("email from","password");

MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("email from");
MailAddress toAddress = new MailAddress("email to");

smtpClient.Host = "smpt host address";
smtpClient.Port = your_port;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = smtpCredentials;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Timeout = 20000;

message.From = fromAddress;
message.To.Add(toAddress);
message.IsBodyHtml = false;
message.Subject = "example";
message.Body = "example";

smtpClient.Send(message);

Upvotes: 4

Related Questions