Failed to send email

string from = "myemail@gmail.com";
string to = "other.mail.123@gmail.com";

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new System.Net.Mail.MailAddress(from, "One Ghost", System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = System.Net.Mail.MailPriority.High;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

client.Credentials = new System.Net.NetworkCredential(from, "XXXXX");
client.Port = 587; 
client.Host = "smtp.gmail.com";
client.EnableSsl = true; 
try
{
    client.Send(mail);
}
catch (Exception ex)
{
    Exception ex2 = ex;
    string errorMessage = string.Empty;

    HttpContext.Current.Response.Write("errroorr " + ex.Message.ToString());
}

      

Throws an exception in the client.send () method. It says that sending the message failed. What could be the problem the inner exception is

the inner exception says System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 74.125.141.109:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, 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, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, 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(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)

+3


source to share


3 answers


Try adding these two settings

client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;

      



also check if you have an antivirus on your server that is blocking the sending of the email request

0


source


you can use this below

public static void SendMail(string Message, string Subject)
    {
        bool retVal;
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        MailAddress fromAddres = new MailAddress(FromMail, "Test");
        message.From = fromAddres;
        // To address collection of MailAddress
        message.To.Add(ToMail);
        message.Subject = Subject;
        smtpClient.Host = HotName;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential(SmtpUser, SmtpPassword);
        message.IsBodyHtml = true;
        // Message body content
        message.Body = Message;    

        smtpClient.Send(message);

        retVal = true;
        message.Dispose();
    }

      



I think this will help you ........

0


source


Sorry for posting it as a reply, I intended it to reply to the original post.

SMTP will require credentials, I could not find credentials. Try to telnet

connect to the server. If the port is available and if you have SMTP credentials, you will be able to send an email from the command line itself.

Try this and let me know if it works for you or not.

0


source







All Articles