C#联系表格

时间:2016-06-22 19:10:58

标签: c# .net gmail

我正在尝试在我的网站上建立一个联系表单,客户可以在那里为我发送电子邮件(ADM)。 所以我使用stmp客户端来做到这一点,但是当我从联系表单发送电子邮件时,看起来我发送给自己。 这是代码。

MailMessage mailMessage = new MailMessage();
        MailAddress fromAddress = new MailAddress(command.Email); //sender email address 
        mailMessage.From = fromAddress;
        mailMessage.To.Add("myEmail@gmail.com");    //receiver email address               
        mailMessage.Body = command.Message;
        mailMessage.IsBodyHtml = true;
        mailMessage.Subject = "Contact Us";

        SmtpClient smtpClient = new SmtpClient();
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.Port = 587;
        smtpClient.EnableSsl = true;
        smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtpClient.Credentials = new System.Net.NetworkCredential("myEmail@gmail.com", "password");
        smtpClient.Send(mailMessage);

如何设置正确的电子邮件,以便我可以从客户那里收到,而不是我自己哈哈。?

To me Image

2 个答案:

答案 0 :(得分:0)

Google smtp服务器会自动更改“发件人”字段以匹配用于登录的凭据。如果smtp服务器允许任何人从任何地址发送邮件,则其他邮件服务器可能不会信任它,并且邮件将会被忽略或标记为垃圾邮件。

答案 1 :(得分:0)

除了@tsandy的答案之外,还有一些你需要改变的东西。所以我猜command.Email是客户的电子邮件地址。您的代码可能如下所示。

MailMessage mailMessage = new MailMessage();
MailAddress fromAddress = new MailAddress("myEmail@gmail.com"); //sender email address 
mailMessage.From = fromAddress;
mailMessage.To.Add("myEmail@gmail.com");    //receiver email address               
mailMessage.Body = command.Message;
mailMessage.IsBodyHtml = true;
mailMessage.DisplayName = command.email; //This line is added
mailMessage.Subject = "Contact Us";

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential("myEmail@gmail.com", "password");
smtpClient.Send(mailMessage);

在这种情况下,您就像给自己发送一封电子邮件,但在邮件签名中,您至少会看到客户的电子邮件