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