Exception in Sending mail with C# task instead of SSIS mail task

  • The ssis send mail task is 99% useless. So, I was trying to use a C# task instead to send mail instead. I got an error. Please help me to fix it -

    Error: System.Reflection.TargetInvocationException: Exception has

    been thrown by the target of an invocation. ---> System.Net.Mail.SmtpException:

    Failure sending mail. ---> System.Net.WebException: Unable to connect to

    the remote server ---> System.Net.Sockets.SocketException: A connection

    attempt failed because the connected party did not properly respond after

    a period of time, or established connection failed because connected host

    has failed to respond 123.456.789: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, Int32 timeout, Exception& exception)

    This exception tells me the possible list of causes - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 123.456.789:587

    But it does not tell me exactly what is the cause - port number, wrong username, password or something else. How do I find out what is the exact cause.

    Here is the code I used - Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird

    using System.Net;

    using System.Net.Mail;

    public void email_send()

    {

    MailMessage mail = new MailMessage();

    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

    mail.From = new MailAddress("your mail@gmail.com");

    mail.To.Add("to_mail@gmail.com");

    mail.Subject = "Test Mail - 1";

    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;

    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");

    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;

    SmtpServer.Credentials = new System.Net.NetworkCredential("your

    mail@gmail.com", "your password");

    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);

    }

  • my working code for the same functionality is largely the same;

    i think you'll need to try-catch and get the inner exception and see what the server/connection issue is;

    I just tried my code here at work, and gmail is blocked, so my inner exception was

    No connection could be made because the target machine actively refused it. Connection closed.

    try

    {

    SmtpServer.Send(mail);

    }

    catch Exception ex

    {

    MessageBox.Show(ex.InnerException);

    MessageBox.Show(ex.Message);

    }

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell (12/30/2013)


    my working code for the same functionality is largely the same;

    i think you'll need to try-catch and get the inner exception and see what the server/connection issue is;

    I just tried my code here at work, and gmail is blocked, so my inner exception was

    No connection could be made because the target machine actively refused it. Connection closed.

    try

    {

    SmtpServer.Send(mail);

    }

    catch Exception ex

    {

    MessageBox.Show(ex.InnerException);

    MessageBox.Show(ex.Message);

    }

    Thanks. But, it does not work. It gives me the same error -

    System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 123.456.789.101: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, 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.SmtpClient.Send(MailMessage message)

    ----------------------

    Second error is - failure sending email.

    Btw, your code needs to be changed as follows -

    try

    {

    SmtpServer.Send(mail);

    }catch(Exception ex)

    {

    MessageBox.Show(ex.InnerException + "");

    MessageBox.Show(ex.Message);

    }

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply