We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How to set mail credentials

Dovberman
Dovberman asked
on
Medium Priority
966 Views
Last Modified: 2012-05-11
I am trying to send mail from my site host. This worked until I changed to  a different site host.

 //send the message
        SmtpClient smtp = new SmtpClient("smtp.stockpickermax.com", 8889);
             
        if (strConnection.Contains("StockProSQL")) // Local server
        {
            // do not send mail;
        }
        else // Remote server
        {
            smtp.UseDefaultCredentials = true;
            smtp.Send(mail);
        }

Error:

No connection could be made because the target machine actively refused it 64.79.170.142:8889

Line 184:        {
Line 185:            smtp.UseDefaultCredentials = true;
Line 186:            smtp.Send(mail);
Line 187:        }
Line 188:        conStockSelect.Close();

Source File: e:\web\stockpicker\htdocs\ErrorHandling\ShowError.aspx.cs    Line: 186

I know the credentials. How can I enter them/

Thanks,

Comment
Watch Question

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
√Čric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:

Author

Commented:
This is close. Here is the most recent error:

Compiler Error Message: CS0246: The type or namespace name 'NetworkCredential' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

 

Line 184:        else // Remote server
Line 185:        {
Line 186:            smtp.Credentials = new NetworkCredential("stockpicker", "1088Delb");
Line 187:            smtp.Send(mail);
Line 188:        }

How do I define the NetworkCredential class. This is probably in a using statement.

Thanks,
CERTIFIED EXPERT
Top Expert 2015

Commented:
Add System.Net In a Using directive, or try it with the full namespace:

smtp.Credentials = new System.Net.NetworkCredential("stockpicker", "1088Delb");

Also, set default credentials to false:

smtp.UseDefaultCredentials = False;

Author

Commented:
using System.Net.Mail;
using System.Net.NetworkInformation;

No connection could be made because the target machine actively refused it 64.79.170.142:8889
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 64.79.170.142:8889

Source Error:


Line 184:            smtp.UseDefaultCredentials = false;
Line 185:            smtp.Credentials = new System.Net.NetworkCredential("stockpicker", "1088Delb");
Line 186:            smtp.Send(mail);
Line 187:        }
Line 188:        conStockSelect.Close();

I will contact my site provider.

Thanks,

Source File: e:\web\stockpicker\htdocs\ErrorHandling\ShowError.aspx.cs    Line: 186

CERTIFIED EXPERT
Top Expert 2015

Commented:
Maybe the server requires SSL?

smtp.EnableSsl = true;  
smtp.Port = "587";

Set port number as required.  

Author

Commented:
This is the answer from my site host:

I'm afraid Network Credentials are not supported in our environment. You will either have to use "localhost" with no authentication to send out email, or use your email servers address with a valid email address and password for authentication.

How would I implement this?

I am using Outlook 2010.

Thanks,

Dovberman
CERTIFIED EXPERT
Top Expert 2015

Commented:
If you are not concerned about who the sent emails are coming from, you could send through a free email account like gmail. The server would then be smtp.gmail.com, ssl enabled, port 587. Supply the credentials the same way.

Author

Commented:
Thanks,

I will try it tomorrow.
As per answer of your site host. You can use following code to generate email using your hosted server.

 using System.Web.Mail;
.
.
.
MailMessage xMailMessage = new MailMessage();
xMailMessage.From = "test@test.com";
xMailMessage.To = "to@test.com";
xMailMessage.Subject = "This is a test";
xMailMessage.BodyFormat = MailFormat.Html;
xMailMessage.Body = "<html><head><title>Test emial</title></head><br /><br /><hr>JUST A TEST<hr><br /><br /></body></html>";

SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(xMailMessage); 

Open in new window

You can also use SmtpMail.SmtpServer = "127.0.0.1";
instead of "localhost".
I am sure it will work in your environment.

Author

Commented:
This is what finally worked:

               SmtpClient smtp = new SmtpClient("smtp.stockpickermax.com", 25);
                NetworkCredential Credentials = new NetworkCredential("support@stockpickermax.com", "mypassword");
                smtp.Credentials = Credentials;
                 smtp.Send(mail);

Author

Commented:
Thanks,
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.