使用c#和Godaddy服务器发送电子邮件错误

时间:2016-12-20 06:47:56

标签: c# asp.net .net email

您好我正在使用来自goodaddy的主机,mi aplication是在c#中制作的,问题是发送电子邮件时,这是我的代码

 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
        };

    }

发送电子邮件的电子邮件是gmail,错误是下一个

  

System.Net.Mail.SmtpException:发送邮件失败。 ---> System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:尝试在System.Net上的System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)中以其访问权限74.125.199.109:587禁止的方式访问套接字.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,Socket& socket,IPAddress& address,ConnectSocketState state,IAsyncResult asyncResult,Exception& exception)---内部异常堆栈跟踪结束---在System.Net.ServicePoint System.Net.PooledStream.Activate上的System.Net.PooledStream.Activate(Object owningObject,Boolean async,GeneralAsyncDelegate asyncCallback)中的.GetConnection(PooledStream PooledStream,Object owner,Boolean async,IPAddress& address,Socket& abortSocket,Socket& abortSocket6) (System owningObject,GeneralAsyncDelegate asyncCallback)在System的System.Net.ConnectionPool.GetConnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)。位于System.Net.Mail.SmtpClient.Send(MailMessage消息)的System.Net.Mail.SmtpClient.GetConnection()上System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)的Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) )---内部异常堆栈跟踪的结束---在Core.EmailManager.sendEmail(EmailDto emailDto)的System.Net.Mail.SmtpClient.Send(MailMessage消息)

可能是什么问题?

2 个答案:

答案 0 :(得分:4)

我解决了,我希望这个aswer帮助某人,这个代码在这里:

 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
        };

    }

问题是其他中继/ smtp服务器无法在我们的托管中运行。而且我不需要指定密码。我改变了端口和主机,这是帮助我的链接

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

答案 1 :(得分:0)

这是我的方法有效。我将在网络配置中设置用户名和密码。

 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>