2017-04-13 59 views
0

我使用WebApi发送邮件。邮件发送成功。我想从邮件中更改,然后使用下面的代码进行更改,但它会以“[email protected]”的形式从邮件中提取。我在webconfig中使用[email protected],我想从[email protected]设置。 但没有按照以下代码工作,当我收到的邮件总是从'[email protected]'而不是'[email protected]' 注意:我使用上述电子邮件只提问问题,而开发我使用我的真实邮件ID。使用WepAPi发送邮件时发生的变化

有没有其他办法可以达到这个目的?或者我需要改变任何事情。

下面是我的代码来发送邮件:

public static bool SendMail(string toAddress, string subject, string body) 
    { 
     try 
     { 

      MailMessage msg = new MailMessage(); 
      msg.From = new MailAddress("[email protected]"); 
      msg.To.Add(new MailAddress(toAddress)); 
      msg.Subject = subject; 
      msg.Body = body; 
      msg.IsBodyHtml = true; 
      SmtpClient smtp = new SmtpClient(); 
      smtp.EnableSsl = true;    
      smtp.Send(msg); 
      return true; 
     } 
     catch (Exception ex) 
     { 

      return false; 
     } 
    } 

下面是webconfig SMPT设置:

<mailSettings>  
<smtp from="[email protected]"> 
    <network host="smtp.gmail.com" port="587" userName="[email protected]" password="test" /> 
    </smtp> 
</mailSettings> 

也尝试用下面的webconfig设置:

<smtp deliveryMethod="Network"> 
    <network host="smtp.gmail.com" port="587" userName="[email protected]" password="test" /> 
    </smtp>  
</mailSettings> 
+0

[在c#中通过gmail发送邮件时更改发件人地址的可能重复项](http://stackoverflow.com/questions/3871577/change-sender-address-when-sending-mail-through-great-gmail-in-c -sharp) –

回答

0

你不能! !

您正在使用google SMTP服务器,它不允许更改发件人地址,如果您想更改发件人地址,请尝试使用允许的其他smtp提供程序。

+0

嗨@Vinay,我在旧项目中的wcfservice中使用了相同的代码,我可以在该项目中更改,但不能在webapi中工作。 – Hitesh

+0

@Hitesh您是否使用过WCF服务中gmail的SMTP提供程序? – Rex

+0

是的我在wcf服务中有相同的代码和相同的webconfig设置。 – Hitesh