3

I am trying to setup an email sending application, using a hotmail account.

The code looks like this:

MailMessage mail = new MailMessage(from, to);
        mail.Subject = "Proba email";
        mail.Attachments.Add(new Attachment("C:\\Documents and Settings\\Proba.txt"));
        SmtpClient client = new SmtpClient();
        client.UseDefaultCredentials = false;
        client.Port = 587; // 465 568
        client.Host = "smtp.live.com";
        client.EnableSsl = true;
        client.Credentials = new System.Net.NetworkCredential("smg@hotmail.com", "password");
        client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
        client.SendAsync(mail, "token");

Using, Async I actually get no errors, I even get the feedback saying message sent (Event triggers) but the message never arrives. If I use the simple client.Send void, I get the following error:

5.3.4 Requested action not taken; To continue sending messages, please sign in to your account.

So any ideas on what the problem can be? As I was trying to hand down the SMTP settings of hotmail I got various setups saying port 25, then 587 so maybe it's something there. Any help would be greatly appreciated thanks!

  • Okay so it's definitely working now, I would just like to ask if I will have to do regular "I'm not a robot checks" or was that a one-time thing?
peterh
  • 11,875
  • 18
  • 85
  • 108
ptr0x01
  • 627
  • 2
  • 10
  • 25
  • I relogged in to my account and it asked for a spam check, to see if I'm robot. Now the message got through and actually arrived, but would I need to do this frequently or was this a one time something? – ptr0x01 Mar 27 '11 at 19:52
  • If an account checks against robots then maybe robots aren't allowed? And you are writing a robot. – H H Mar 27 '11 at 19:55
  • I think the spam question has been asked before and there is nothing you can do. The recipient has to have your **from** address in their address book to ensure delivery to inbox. – Bala R Mar 27 '11 at 19:56
  • You won't get error messages if in async mode –  Mar 27 '11 at 19:58
  • So async can't bug out I see. Robots are not allowed if they are used to spam. As you can imagine, I am not writing a spam robot but a simple email client. So pretty much logging in to the account "solves" the problem. – ptr0x01 Mar 27 '11 at 20:04

3 Answers3

5

Here is my setup, BTW async will not return any errors.

<system.net>
<mailSettings>
  <!-- E-mail server settings -->
  <smtp from="do-not-reply@example.com">
    <network host="smtp.example.com" port="25" userName="" password="" defaultCredentials="true" />
  </smtp>
</mailSettings>

    void SendEmail(EmailEntity email)
    {
        var mailMessage = new MailMessage { From = new MailAddress(email.From) };
        mailMessage.To.Add(new MailAddress(email.To));
        mailMessage.Subject = email.Subject;
        mailMessage.Body = email.Body;
        mailMessage.IsBodyHtml = true;

        // Send the email
        var client = new SmtpClient();
        client.Send(mailMessage);
    }
  • Thanks, well it does work now, I suppose I should learn a bit about this spam protection to see if I can send messages without verifying my humanbeingness every X email. – ptr0x01 Mar 27 '11 at 20:08
2

I have a similar setup where I just use Send() that is working for me. The only thing extra that I have is

 client.DeliveryMethod = SmtpDeliveryMethod.Network;

just before client.Send();. Not sure if this alone will solve your problem. Do you have outlook setup with the same server and credentials ?

Bala R
  • 107,317
  • 23
  • 199
  • 210
0
SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        try
        {
           // System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            MailAddress fromAddress = new MailAddress(txt_name.Text, txt_to.Text);


            smtpClient.Host = "smtp.gmail.com";


            smtpClient.Port = 25;

           // msg.From = new System.Net.Mail.MailAddress("xyz@gmail.com");

          message.From = fromAddress;

            message.To.Add("xyz111@gmail.com");


            message.Body = txt_des.Text;
            smtpClient.EnableSsl = true;

            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();

            NetworkCred.UserName = "xyz@gmail.com";

            NetworkCred.Password = "xtz";

            smtpClient.UseDefaultCredentials = true;

            smtpClient.Credentials = NetworkCred; 


            smtpClient.Send(message);

            lblStatus.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            lblStatus.Text = "Send Email Failed." + ex.Message;
        }
r12
  • 59
  • 4