Skip to main content
 首页 » 编程设计

c#之使用多个 from 字段从 MVC C# 发送电子邮件

2024年01月01日42zhenyulu

我正在制作一个在线系统,我有多个客户,我的客户有多个联系人。现在我希望我的客户能够向他的联系人发送电子邮件,这样当他的联系人收到电子邮件时,他们会在 From:abc@abc.com & ABC company 字段中看到他的公司名称和他的电子邮件地址。

我正在使用 MVCMailer 发送电子邮件,它工作得很好,但是当我收到测试电子邮件时,它显示的不是我的客户电子邮件地址(TO 和 NAME),而是我在 web.config 文件中配置的电子邮件。我怎样才能更改发件人地址,以便我客户的联系人可以看到他的电子邮件地址而不是我的?

==========================

   <system.net> 
     <mailSettings> 
      <!-- Method#1: Configure smtp server credentials --> 
     <smtp from="xyz@xyz.com"> 
    <network enableSsl="true" host="smtp.xyz.com" port="587" userName="xyz@xyz.com" password="ABC" /> 
  </smtp> 
  <!-- Method#2: Dump emails to a local directory --> 
  <!-- 
  <smtp from="some-email@gmail.com" deliveryMethod="SpecifiedPickupDirectory"> 
    <network host="localhost" /> 
    <specifiedPickupDirectory pickupDirectoryLocation="c:\temp\"/> 
  </smtp> 
  --> 
</mailSettings> 

=======================================

这是我在 C# 类中使用的代码

   var mailMessage = new MailMessage { Subject = eSubject }; 
        mailMessage.To.Add(Email); 
        mailMessage.Bcc.Add("xyz@xyz.com"); 
        mailMessage.ReplyToList.Add( new MailAddress(From, ContactPerson)); 
        mailMessage.Sender = new MailAddress("xyz@xyz.com", ContactPerson); 
 
        //mailMessage.To.Add("some-email@example.com"); 
        //ViewBag.Data = someObject; 
        ViewData = new ViewDataDictionary(obj); 
 
 
        SmtpClient smtp = new SmtpClient("192.168.1.2",25); 
        smtp.Credentials = new NetworkCredential("xyz@xyz.com", "test"); 
 
        smtp.Send("xyz@xyz.com","xyz@xyz.com","test","test"); 
        PopulateBody(mailMessage, viewName, null); 
 
 
        SmtpClient smtp = new SmtpClient("192.168.1.2",25); 
        smtp.Credentials = new NetworkCredential("xyz@xyz.com", "test"); 
 
        //smtp.Send("xyz@xyz.com","xyz@xyz.com","test","test"); 
        PopulateBody(mailMessage, viewName, null); 
 
        return mailMessage; 

========================

请告诉我如何解决这个问题,甚至是否可以使用 mvc c# 解决这个问题?

沙特阿拉伯

请您参考如下方法:

看看你的代码:

mailMessage.Sender = new MailAddress("xyz@xyz.com", ContactPerson); 
 
smtp.Send("xyz@xyz.com","xyz@xyz.com","test","test"); 

我想这里到处都是你告诉 MvcMailer 你的发件人是“xyz@xyz.com”。我认为在这里您应该更改为您的客户电子邮件和姓名。
顺便说一句,检查“smtp.Send”的注释。确保您知道所有参数的含义。