简体   繁体   English

如何从 ASP.Net/C# Webforms 中的任何 Email 地址发送 email

[英]How to send an email from any Email Address in ASP.Net/C# Webforms

I am a beginner programmer in C#, I want to create a contact form, where the user can enter any email address as the "From" address, and they can send a message.我是 C# 的初学者程序员,我想创建一个联系表,用户可以在其中输入任何 email 地址作为“发件人”地址,他们可以发送消息。 Every time it sends, it says the "From" address is the credentials that I am using.每次发送时,它都会说“发件人”地址是我正在使用的凭据。 How do I change the From address to the address based on the user's input like any other contact form.如何像任何其他联系表一样根据用户的输入将发件人地址更改为地址。 Should I use a different Smtp Server?我应该使用不同的 Smtp 服务器吗? Thanks谢谢

    protected void sendbttn_Click(object sender, EventArgs e)
    {
        try
       {



            MailMessage message = new MailMessage();
            message.From = new MailAddress(fromtxt.Text);
          
            message.To.Add("toEmail");
            message.Subject = "Subject: " + subjecttxt.Text;
            message.Body = mesgtxt.Text;
            message.IsBodyHtml = true;

            SmtpClient smtpClient = new SmtpClient();
           
           smtpClient.Host = "smtp.gmail.com";
           smtpClient.Port = 587;

        smtpClient.UseDefaultCredentials = false;
            
           smtpClient.Credentials = new System.Net.NetworkCredential("Credemail", "Credpassword");
           
            smtpClient.EnableSsl = true;
            smtpClient.Send(message);
            MessageBox.Show("Your email has been sent!");

            fromtxt.Text = "";
            subjecttxt.Text = "";
            mesgtxt.Text = "";
        }

        catch
        {
            MessageBox.Show("Something went wrong, please try again");
        }

If your host is not registered as official mail provider, you should never even think about sending E-Mail in the name of somebody else.如果您的主机没有注册为官方邮件提供商,您甚至都不应该考虑以其他人的名义发送电子邮件。

Instead use a dedicated address as sender and to authenticate at the smart host and remember to store the credentials in a save location so nobody can "adopt" them - the program sourcecode as in the example is definitely not a save place, store them in IIS property fields.而是使用专用地址作为发件人并在智能主机上进行身份验证,并记住将凭据存储在保存位置,这样就没有人可以“采用”它们 - 示例中的程序源代码绝对不是保存位置,将它们存储在 IIS属性字段。

The address entered in the form may be used as reply-to and possibly as the senders display name.在表单中输入的地址可以用作回复,也可以用作发件人的显示名称。

The body will not be html, too.正文也不会是 html。 The property therefore should be 'false'.因此该属性应为“false”。

string credMail = "example@gmail.com";
string credPasswd = "example_p@asswd";

MailMessage message = new MailMessage();
message.From = new MailAddress(credMail,fromtxt.Text);
message.To.Add("toEmail");
message.Subject = "Subject: " + subjecttxt;
message.Body = mesgtxt.Text;
message.IsBodyHtml = false;
message.ReplyToList.Add(fromtxt.Text);

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(credMail, credPasswd);
smtpClient.EnableSsl = true;
smtpClient.Send(message);
MessageBox.Show("Your email has been sent!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM