通过我的hotmail帐户发送电子邮件并更改'from'



我在我的网站上建立了一个网络表单,允许访问者向我发送消息。一些非常基本的东西(供我个人使用)。在我的后端(c# .Net MVC)上,我使用SmtpClient发送邮件。我为此目的使用我的hotmail帐户。它有效。请注意,from等于Credentials中使用的用户名。

SmtpClient _client = new SmtpClient();
_client.Host = "smtp.live.com";
_client.Port = 587;
_client.UseDefaultCredentials = true;
_client.Credentials = new System.Net.NetworkCredential("ttttt@hotmail.com", "mypassword");
_client.EnableSsl = true;
_client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailAddress to = new MailAddress("ttttt@gmail.com");
MailAddress from = new MailAddress("ttttt@hotmail.com");
MailMessage mail = new MailMessage(from, to);
mail.Subject = "The subject";
mail.Body = "The message";
_client.Send(mail);

当我收到邮件时,发件人等于收件人(我自己:)这并不理想。

  • 寄件人:约翰·多伊
  • 致:约翰·多伊
  • 主题
  • 我的主题
  • 留言
  • 我的留言

我希望在发件人(from)中包含访问者的电子邮件地址。所以我试图改变它,但它不起作用。我收到以下错误消息:

System.Net.Mail.SmtpException Transaction 失败。服务器响应为:5.2.0 STORAGERV。Submit.Exception:SendAsDeniedException.MapiExceptionSendAsDenied;由于消息无法提交消息的永久异常,无法处理消息。

我知道出于安全原因,这不起作用。我不能"以"他人的名义发送电子邮件。

在放弃之前,我来到这里,希望有人能给我一个替代方案。

PS:我知道我可以使用mail.ReplyToList.Add(emailofvisitor);然后当我按Reply这是访问者的电子邮件时,但这仍然不理想,因为我仍然在from字段中看到我的ttttt@hotmail.com

希望我没有误解您的请求,我假设您想更改发件人的显示名称,这就是我在项目中所做的,将以下内容添加到您的 Web.Config 或 App.Config 文件中: 用您想要的任何内容填充发件人部分,它将将其显示为发件人姓名。

<system.net>
<mailSettings>
<smtp from="YourDisplayName &lt;YourEmail&gt;" deliveryMethod="Network">
<network defaultCredentials="false" enableSsl="true" host="hostname" port="587" userName="yourusername" password="yourpassword"/>
</smtp>
</mailSettings>
</system.net>

C# 代码:

SmtpClient smtpClient = new SmtpClient();
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
MailMessage message = new MailMessage();
message.From = new MailAddress(smtpSection.From);
MailAddress to = new MailAddress("youremail@gmail.com");
message.To.Add(to);
message.Subject = "The subject";
message.Body = "The message";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);

相关内容

最新更新