Steam
Steam

Reputation: 9856

Send email fails with exception?

I tried to send mail with a class (whose source code, I do not have) and a C# script (at end).

I can send mail with the class, but not with C# script. I get the 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: No connection could be made 
because the target machine actively refused it YourIPAddress

I know that this class uses the exact same information in the C# code.So, I am guessing that this is because of some default/windows credentials or something like that, but I am not sure. How do I figure out why this is happening and how do I fix it ?

Code -

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net.Mail;        

    public void Main()
    {
        String SendMailFrom = "[email protected]";
        String SendMailTo = "[email protected]";
        String SendMailSubject = "Hello !";
        String SendMailBody = "Hello Email!";

        String SmtpServer = "smtp.apple.com";

        MailMessage myHtmlFormattedMail = new MailMessage(SendMailFrom, 
        SendMailTo, SendMailSubject, SendMailBody);
        myHtmlFormattedMail.IsBodyHtml = true;

        SmtpClient mySmtpClient = new SmtpClient(SmtpServer);
        mySmtpClient.Port = 2525; 
        mySmtpClient.Send(myHtmlFormattedMail);

      }

    }

}

Upvotes: 0

Views: 1705

Answers (2)

Logvinov Vladimir
Logvinov Vladimir

Reputation: 94

using System;
using System.Net;
using System.Net.Mail;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            SendMail("smtp.gmail.com", "[email protected]", "myPassword", "[email protected]", "e-mail subject", "body of the email", "data.xls");
        }

        public static void SendMail(string smtpServer, string from, string password, string mailto, 
            string caption, string message, string attachFile = null)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(from);
                mail.To.Add(new MailAddress(mailto));
                mail.Subject = caption;
                mail.Body = message;

                if (!string.IsNullOrEmpty(attachFile))
                    mail.Attachments.Add(new Attachment(attachFile));
                SmtpClient client = new SmtpClient();
                client.Host = smtpServer;
                client.Port = 587;
                client.EnableSsl = true;
                client.Credentials = new NetworkCredential(from.Split('@')[0], password);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(mail);
                mail.Dispose();
            }
            catch (Exception e)
            {
                throw new Exception("Mail.Send: " + e.Message);
            }
        }
    }
}

Upvotes: 1

Jakub Konecki
Jakub Konecki

Reputation: 46008

Looks like you're not passing any credentials and Apple doesn't allow unauthorized sending of emails.

Try:

SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");

Also, are you sure there's a SMTP server running @ smtp.apple.com:2525?

Upvotes: 0

Related Questions