3

Hi i am using the hosting from goodaddy, mi aplication was made in c#, the problem is when send an email, this is my code

 public ResponseDto sendEmail(EmailDto emailDto) {
        try
        {
            MailMessage message = new MailMessage();
            SmtpClient client = new SmtpClient();
            message.From = new MailAddress("mail@gmail.com");
            message.Subject = "have a new mail";
            message.Body = "info: \n "
                + "name: " + emailDto.Name 
                + "\mail: "+ emailDto.Email + "\nMessage: " +
                emailDto.Message;
            message.To.Add("mail@hotmail.com");
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;

            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.Credentials = new System.Net.NetworkCredential("mail@gmail.com", "(password");
            client.Send(message);
        }
        catch (Exception e) {
            return new ResponseDto
            {
                message = e.ToString()
              ,
                Success = false
            };
        }

        return new ResponseDto
        {
            message = "success",
            Success = true
        };

    }

my email that send the email is gmail, the error is the next

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 74.125.199.109:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at Core.EmailManager.sendEmail(EmailDto emailDto)

What could be the problem?

cristian franco
  • 266
  • 2
  • 13
  • 1
    Apparently, [you cannot do that on GoDaddy](https://uk.godaddy.com/community/Developer-Cloud-Portal/An-attempt-was-made-to-access-a-socket-in-a-way-forbidden-by-its/td-p/14708) with a shared hosting account. – Ken Y-N Dec 20 '16 at 06:55
  • 1
    Alternatively you may try [using GMail API](http://stackoverflow.com/questions/35763607/how-to-send-email-using-gmail-api-having-html-body-attachment-in-asp-net) instead of SMTP – Martheen Dec 20 '16 at 06:59
  • @KenY-N y saw this post, maybe i need to set an email from godaddy – cristian franco Dec 20 '16 at 07:20
  • I solved, in a few minutes i pist the answer thanks guys! – cristian franco Dec 20 '16 at 07:30

2 Answers2

7

I solved, I hope that this aswer helps somebody, this the code is here:

 public ResponseDto sendEmail(EmailDto emailDto) {
        try
        {
            MailMessage message = new MailMessage();
            SmtpClient client = new SmtpClient();
            message.From = new MailAddress("mail@gmail.com");
           message.Subject = "have a new mail";
        message.Body = "info: \n "
            + "name: " + emailDto.Name 
            + "\mail: "+ emailDto.Email + "\nMessage: " +
            emailDto.Message;
            message.To.Add("mail@hotmail.com");
             client.EnableSsl = false;
             client.UseDefaultCredentials = false;
             client.Port = 25;
             client.Host = "relay-hosting.secureserver.net";
             client.Send(message);

        }
        catch (Exception e) {
            return new ResponseDto
            {
                message = e.ToString()
              ,
                Success = false
            };
        }

        return new ResponseDto
        {
            message = "success",
            Success = true
        };

    }

the problem was that other relay/smtp servers will not work from our hosting. and i don't need specify the password. and i change the port and the host, this is the link that help me

https://au.godaddy.com/community/Developer-Cloud-Portal/Unable-to-send-email-from-C-net-application-from-website/td-p/2394

cristian franco
  • 266
  • 2
  • 13
0

This has been my approach that worked. I will set up username and password in the web config.

 MailMessage mailMessage = new MailMessage();
 mailMessage.From = new MailAddress("mail@gmail.com", "");
 mailMessage.To.Add(userEmailAddress);
 mailMessage.Subject = subject;
 mailMessage.Body = "";
 mailMessage.IsBodyHtml = true;
 SmtpClient mailSender = new SmtpClient(ConfigurationManager.AppSettings["smtpconn"]);
await mailSender.SendMailAsync(mailMessage);

web config

  <appSetting>
   <add key="smtpconn" value="smtp.gmail.com" />
 </appSettings>
 <system.net>
<mailSettings>
  <smtp from="mail@gmail.com">
    <network host="smtp.gmail.com" port="587" userName="un" password="pw" defaultCredentials="false"/>
  </smtp>
</mailSettings>
</system.net>
Arianule
  • 8,811
  • 45
  • 116
  • 174