1

I have recently purchased a new computer, and now my e-mails never get sent, and there are NEVER any exceptions thrown or anything.

Can somebody please provide some samples that work using the SmtpClient class? Any help at all will be greatly appreciated.

Thank you

Updates


Ok - I have added credentials now. And can SUCCESSFULLY SEND e-mail synchronously. But I can still not send them asynchronously.

Old: After trying to send e-mail synchronously, I receive the following exception:

Transaction failed. The server response was:

5.7.1 <myfriend@hotmails.com>: Relay access denied.

abatishchev
  • 98,240
  • 88
  • 296
  • 433

2 Answers2

1

You can send mail through Async(). How means you should follow the below code,

smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtpClient.SendAsync(mailMessage, mailMessage);

and, if you are using async, you need to also have the event handler like,

static void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        //to be implemented        
    }

By using this, you can send Mail.

Duk
  • 905
  • 5
  • 15
  • 34
0

You could first try the synchronous Send method to verify that everything is setup correctly with the SMTP server and that you don't get any exceptions:

var client = new SmtpClient("smtp.somehost.com");
var message = new MailMessage();
message.From = new MailAddress("from@somehost.com");
message.To.Add(new MailAddress("to@somehost.com"));
message.Subject = "test";
message.Body = "test body";
client.Send(message);

Remark: In .NET 4 SmtpClient implements IDisposable so make sure you wrap it in a using statement.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thank you very much; I am trying this now. I have already tried that but my code was abit different, so I'll try it your way. –  May 24 '10 at 08:11
  • Okay, I got an exception: Transaction failed. The server response was: 5.7.1 : Relay access denied - I will update my question with progress. I have no idea what this means –  May 24 '10 at 08:17
  • 1
    It means that the SMTP server you are using is not configured to relay mails to upstream servers. If you are using IIS you may take a look at this article: http://support.microsoft.com/kb/293800 – Darin Dimitrov May 24 '10 at 08:19
  • I'm using the same e-mail service (BlueBottle) to send e-mails. I've been doing this for years. I've also tried several other email accounts to send e-mail. Same story. –  May 24 '10 at 08:27
  • Well then you might have more luck asking your question on http://serverfault.com/ as this is no longer programming related. – Darin Dimitrov May 24 '10 at 08:36
  • After implementing your code I forgot to add credentials. I added credentials to "client", and now I CAN send email Synchronously. But still cannot send mail Asynchronously. –  May 24 '10 at 08:38
  • This IS a programming related question. –  May 25 '10 at 12:35