开发者

How can I make SMTP authenticated in .NET?

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("username@gmail.com", "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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜