System.Runtime.InteropServices.COMException(0x8004020F): The server rejected one or more recipient addresses.

博客给出了一段邮件发送的代码示例。代码中创建邮件消息对象,设置收件人、发件人、主题和正文等信息。若用户名和密码不为空,会添加认证字段。最后指定 SMTP 服务器并尝试发送邮件,若发送失败则返回 false。
这个问题
需要设置可以通过指定邮件服务器进行relay的ip地址

smtp->properties->access->relay
add the ip address


   public bool Send(string to, string from, string subject, string message)

         {

              try

              {

                   MailMessage em = new MailMessage();

                   em.To = to;

                   em.From = from;

                   em.Subject = subject;

                   em.Body = message;

 

                   //Found out how to send authenticated email via System.Web.Mail at http://SystemWebMail.com (fact 3.8)

                   if(this.UserName != null && this.Password != null)

                   {

                       em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");   //basic authentication

                       em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", this.UserName); //set your username here

                       em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", this.Password); //set your password here

                   }

 

                   SmtpMail.SmtpServer = this.SmtpServer;

                   SmtpMail.Send(em);

                   return true;

              }

              catch

              {

                   return false;

              }

          }

 

     }

  

在C#创建新Excel文件时出现`System.Runtime.InteropServices.COMException`错误代码`0x800A03EC`,可尝试以下解决办法: ### 检查.NET 1.1是否安装 Interop需要.NET 1.1,而Windows Server 2008不会预装该版本,需检查并安装.NET 1.1 [^2]。 ### 创建并设置桌面目录权限 对于64位Windows系统,需创建目录`C:\Windows\SysWOW64\config\systemprofile\Desktop`;对于32位Windows系统,则创建`C:\Windows\System32\config\systemprofile\Desktop`。同时,要为该`Desktop`目录设置完全控制权限,例如在Win7、IIS 7和DefaultAppPool环境下,需为用户`IIS AppPool\DefaultAppPool`设置权限 [^3]。 ### 代码示例 以下是使用`Microsoft.Office.Interop.Excel`创建新Excel文件的代码示例,可在实际使用时添加异常处理: ```csharp using Excel = Microsoft.Office.Interop.Excel; using System; class Program { static void Main() { try { Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Add(); Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1]; worksheet.Cells[1, 1] = "Hello, Excel!"; string filePath = @"C:\Test\NewExcelFile.xlsx"; workbook.SaveAs(filePath); workbook.Close(); excelApp.Quit(); } catch (System.Runtime.InteropServices.COMException ex) { if (ex.ErrorCode == 0x800A03EC) { Console.WriteLine("出现错误代码0x800A03EC,请检查相关设置。"); } else { Console.WriteLine($"其他COM异常: {ex.Message}"); } } catch (Exception ex) { Console.WriteLine($"发生其他异常: {ex.Message}"); } } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值