【问题标题】:How to send mail in ASP.Net from google, yahoo or other mail domains?如何在 ASP.Net 中从 google、yahoo 或其他邮件域发送邮件?
【发布时间】:2011-07-08 11:04:15
【问题描述】:

我有一个“联系我们”页面,用户将在其中提供他们的电子邮件 ID 和查询,并在提交表单时,网络管理员会收到该电子邮件。

如果我将他们的电子邮件 ID 配置为“来自”MailAddress 并发送邮件,如果 ID 来自流行的邮件域(如 gmail 或 hotmail)但可以与其他不受欢迎或不存在的域(如 me@)一起使用,它将无法这样做abcxyzmail.om 没有提供任何凭据!

在我正确配置 SMTP 和网络凭据后,它可以与 gmail 一起使用。 目的是让收到电子邮件的我网站的管理员能够在他的邮件客户端中点击回复按钮,并在“联系我们”页面中看到填充了“发件人”字段的“收件人”字段。 是否有任何适当的方法可以做到这一点或提示或技巧来完成它。

这是我的代码

    MailMessage emailMessage = new MailMessage();
    MailAddress emailTo = new MailAddress("admin@webdev.co.nz", "Web Dev");
    MailAddress emailFrom = new MailAddress(tbEmail.Text);
    SmtpClient localhost = new SmtpClient("localhost");

    emailMessage.To.Add(emailTo);
    emailMessage.From = emailFrom;
    emailMessage.Subject = "Enquiry / Feedback";
    emailMessage.Body = "Name: " + tbName.Text +
            "\nAddress: " + tbEmail.Text +
            "\nComments: " + tbComments.Text;//emails body

    localhost.Send(emailMessage);

谢谢

席德

【问题讨论】:

标签: asp.net email mailmessage


【解决方案1】:

不知道您为什么会在这里遇到问题 - 我们有一些系统可以做到这一点而没有任何问题。但是邮件是一头笨拙的野兽。我敢打赌服务器上的配置设置搞砸了——你有多少控制权?

无论如何,更正确的方法是使用 EmailMessage.ReplyTo (2.0/3.5) 或 EmailMessage.ReplyToList (4.0) 属性来发送消息。这可能会绕过导致此问题的服务器上的任何配置。

【讨论】:

  • 宾果游戏!这正是我所需要的!非常感谢。
  • 呵呵。我通常会得到“Wyatt Earp”。 . .
【解决方案2】:

这是因为您使用本地主机发送电子邮件 - 您需要一个电子邮件服务器。如果您实际上有一个 GMail(或其他)帐户 - 然后使用具有正确凭据的服务器。

【讨论】:

  • 如果是这样,为什么来自其他不存在的域的邮件或来自我域的邮件会到达我的收件箱?
  • 你误解了他的问题。
【解决方案3】:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net.Mail;


public class YourClass
{
    private void SendMailFromGmail(string vFrom, string vTo, string vGmailID, string vGmailPass, string vMailText, string vSMPTDNS, string vSubject)
    {
        MailMessage MyMailMessage = new MailMessage();
        SmtpClient SMTPServer = new SmtpClient(vSMPTDNS);
        var _with1 = SMTPServer;
        //Start by creating a mail message object

        //From requires an instance of the MailAddress type

        MyMailMessage.From = new MailAddress(vFrom);

        //To is a collection of MailAddress types
        MyMailMessage.To.Add(vTo);    
        MyMailMessage.Subject = vSubject;
        MyMailMessage.Body = vMailText;
        //Create the SMTPClient object and specify the SMTP GMail server
        _with1.Port = 587;

        _with1.Credentials = new System.Net.NetworkCredential(vGmailID, vGmailPass);
        _with1.EnableSsl = true;

        try {
            _with1.Send(MyMailMessage);
            string lNewVariable5 = "Email Sent";
        //MessageBox.Show(lNewVariable5)
        } catch (SmtpException ex) {
            throw ex;
        }
    }

    public void Main()
    {
        string vFrom = "from_address_here@gmail.com";
        string vTo = "to_address_here@domain_name_here";
        string vGmailID = "account uid";
        string vGmailPass = " account pwd";
        string vMailText = "This is the test text for Gmail email";
        string vSMPTDNS = "smtp.gmail.com";
        string vSubject = "GMail Test";

        SendMailFromGmail(vFrom, vTo, vGmailID, vGmailPass, vMailText, vSMPTDNS, vSubject);
    }



}

【讨论】:

    【解决方案4】:

    添加回复标题

    mail.Headers.Add( "Reply-To", "users.email.@hisprovider.com" );
    

    这将使电子邮件客户端填充此地址而不是来自地址。这是你想要的。以上代码未经测试。

    【讨论】:

    • 几乎是答案。电子邮件的回复属性是我需要的。
    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 2019-09-10
    • 2021-06-17
    • 2023-03-28
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多