【问题标题】:Sending email through my hotmail account and changing the 'from'通过我的 hotmail 帐户发送电子邮件并更改“发件人”
【发布时间】:2019-07-22 15:12:50
【问题描述】:

我在我的网站上构建了一个网络表单,以允许访问者向我发送消息。一些非常基本的东西(供我个人使用)。在我的后端(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);

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

  • 发件人:John Doe
  • 致:John Doe
  • 主题:我的主题
  • 消息:我的消息

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

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

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

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

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

【问题讨论】:

  • 它不好。不要走黑暗的道路。只需有一个像“contactform@domain.com”这样的发件人,并将用户邮件放在主题或回复地址中。将他们的名字放在“发件人”地址中意味着可能没有发生的事情。
  • 作为替代方案,您可以只公开您的 wmaol 地址并让他们使用它;-)
  • 我知道这很丑,但如果只是关于光学,你可以使用DisplayName 来改变你所看到的。
  • 谢谢。你可能是对的!
  • 感谢@Barns 的建议,但正如你所说......这很丑;)

标签: c# .net smtpclient


【解决方案1】:

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

 <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);

【讨论】:

    猜你喜欢
    • 2016-02-22
    • 2015-01-30
    • 2016-04-19
    • 2012-11-04
    • 2011-05-21
    • 2020-09-07
    • 1970-01-01
    • 2011-07-21
    • 2011-12-13
    相关资源
    最近更新 更多