views:

162

answers:

1

I'm trying to build an email application in Visual Studio 2005 and I'm using .NET's System::Net::Mail::Smtp class to send emails, but I can't figure out how to authenticate SMTP with a server (right now I've been trying with smtp.gmail.com the most).

Here's my code:

SmtpClient^ client = gcnew SmtpClient("smtp.gmail.com");
NetworkCredential^ basicCredential = gcnew NetworkCredential("[email protected]", "password");
client->UseDefaultCredentials = false;
client->Credentials = basicCredential;

The thing that annoys me is that it doesn't throw any SmtpExceptions so it's communicating with the server alright, it's just not authenticating. I've also tried setting:

client->EnableSsl = true;

but that also doesn't work.

Any ideas on how to make this work?

Edit: I've removed my answer to this question because it was wrong. Having turned off the Firewall does nothing, it seems like this code works on and off. Does anyone know if Gmail has some sort of spam feature that dumps anything that doesn't have a legitimate looking title/body? Or any other reason why this code works once out of ten tries??

Second edit: This is what my MailMessage looks like:

MailAddress^ from = gcnew MailAddress(fromAddr, fromName, System::Text::Encoding::UTF8);
MailAddress^ to = gcnew MailAddress((String^) toAddr[0]);
MailMessage^ message = gcnew MailMessage(from, to);
MailAddress^ cc = gcnew MailAddress(ccAddr);
message->CC->Add(cc);
MailAddress^ bcc = gcnew MailAddress(bccAddr);
message->Bcc->Add(bcc);
message->Body = body;
message->BodyEncoding = System::Text::Encoding::UTF8;
message->Subject = sub;
message->SubjectEncoding = System::Text::Encoding::UTF8;
message->Attachments->Add(attachment);

This seems to work more consistently now, but I haven't changed anything in my code.. It must be something to do with the actual SMTP server?

+2  A: 

When you tried enabling ssl did you also change the port to 587, the GMail SSL SMTP port?

Jherico
Yeah, I've tried that with no luck as well.
hora
Maybe you should include a longer code sample?
Jherico