【发布时间】:2012-04-06 21:59:20
【问题描述】:
我的程序中有一个函数可以从 google 帐户发送电子邮件。
我不久前编写了这段代码,它过去工作正常,它按预期发送电子邮件。但是,现在我无法让它工作。 (我现在正在使用 Windows 7 64 位,如果这会有所作为的话)。
我得到的错误(这是代码中的第一条错误消息)是:
system.web.httpexceptions:消息无法发送到 smtp 服务器。传输错误代码为 0x80040217。服务器响应 不可用 --> system.reflection.targetinvocationexception: 调用的目标已抛出异常。 --> system.runtime.interopservice.comexception (0x80040211):消息 无法发送到 smtp 服务器。传输错误代码是 0x80040217。服务器响应不可用。
这是代码:
void sendEmail(string [] emailList, int emailLength, string fileName)
{
int i = 0; //variable to act as temporary index into emailList array
try
{
try
{
MailMessage message = new MailMessage();
//Because using google server, requires SSL
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","theusername@gmail.com" );
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","thepassword" );
message.From = "theusername@gmail.com";
message.To = emailList[i];
//add Bcc
while (++i < emailLength)
{
message.Bcc = emailList[i];
}//end while
message.Subject = "Test Subject";
message.Body = "Test Body";
MailAttachment fileAttach = new MailAttachment(fileName);
message.Attachments.Add(fileAttach);
try
{
SmtpMail.SmtpServer = "smtp.gmail.com";
提前谢谢你!
【问题讨论】:
-
0x80040217 表示服务器拒绝了您的请求。检查此调试步骤:social.msdn.microsoft.com/Forums/en-US/sqlreportingservices/…
-
是的,你给我的链接对我有帮助,thanx devshorts
-
找到解决方案后发布
-
我发送的用户名和密码有误。