c# - 如何在C#中单击按钮并打开窗口发送电子邮件

标签 c# html winforms email

我想创建一个按钮,以便用户可以单击,它将打开一个新的小窗口,他们可以在其中发送电子邮件。该窗口将具有“发件人”、“收件人”、“主题”、“内容”字段,所有这些字段都将具有默认文本,用户可以编辑它们(“发件人”字段除外)。见下图:

enter image description here

我尝试过的:

我通过以下方式创建了电子邮件表单:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<p>
From:<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</p>
To:<asp:Literal ID="Literal2" runat="server"></asp:Literal>
<p>
Subject:<asp:Literal ID="Literal3" runat="server"></asp:Literal>
</p>
Content:<asp:Literal ID="Literal4" runat="server"></asp:Literal>
</form>
</body>
</html>

然后我尝试将此表单与我当前的代码链接: 当前代码:我可以通过此代码发送电子邮件:

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtpclientaddresshere");
mail.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="debabbb8bfabb2aa98acb1b39bb3bfb7b29ebab1b3bfb7b0f0bdb1b3" rel="noreferrer noopener nofollow">[email protected]</a>");
mail.To.Add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86e3ebe7efeab7c6ffe7eee9e9a8e5e9eb" rel="noreferrer noopener nofollow">[email protected]</a>,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e1848c80888dd3a19880898e8ecf828e8c" rel="noreferrer noopener nofollow">[email protected]</a>");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail";
SmtpServer.Credentials = new System.Net.NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="761b0f051b02060513040013043605191b13021e1f18115815191b" rel="noreferrer noopener nofollow">[email protected]</a>", "");
SmtpServer.Send(mail);

现在我不知道使用上面的表格作为电子邮件窗口是否正确?我如何格式化所有字段并将其链接到我的工作代码?

最佳答案

我所做的是这样的:

public void SendEmail(string _from, string _fromDisplayName, string _to, string _toDisplayName, string _subject, string _body, string _password)
    {
        try
        {
            SmtpClient _smtp = new SmtpClient();

            MailMessage _message = new MailMessage();

            _message.From = new MailAddress(_from, _fromDisplayName); // Your email address and your full name

            _message.To.Add(new MailAddress(_to, _toDisplayName)); // The recipient email address and the recipient full name // Cannot be edited

            _message.Subject = _subject; // The subject of the email
            _message.Body = _body; // The body of the email

            _smtp.Port = 587; // Google mail port
            _smtp.Host = "smtp.gmail.com"; // Google mail address
            _smtp.EnableSsl = true;
            _smtp.UseDefaultCredentials = false;
            _smtp.Credentials = new NetworkCredential(_from, _password); // Login the gmail using your email address and password

            _smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            _smtp.Send(_message);

            ShowMessageBox("Your message has been successfully sent.", "Success", 2);
        }

        catch (Exception ex)
        {
            ShowMessageBox("Message : " + ex.Message + "\n\nEither your e-mail or password incorrect. (Are you using Gmail account?)", "Error", 1);
        }
    }

我这样使用它:

SendEmail(textBox2.Text, textBox5.Text, textBox3.Text, "YOUR_FULL_NAME", textBox4.Text, textBox6.Text, "YOUR_EMAIL_PASSWORD");

这是图片:

enter image description here

(尽管我使用的是 WinForms 而不是 Windows 演示文稿表单)。

希望这个答案对你有帮助。

干杯!

关于c# - 如何在C#中单击按钮并打开窗口发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27687067/

相关文章:

c# - 为什么 lambda 比 IL 注入(inject)动态方法快?

html - 透明背景颜色,但在 IE 6 和 7 中不透明文本

html - 如何将 Clojure 函数连接到 html 页面?

c# - 如何在 Windows 窗体应用程序中使用 Monaco 编辑器?

c# - Windows Server 2008 中没有气球工具提示关闭按钮

c# - Selenium - 搜索带有类和文本的元素

c# - Json.NET 问题

php - .JPEG .JPG 高质量图像不在 PHP HTML 中上传

c# - 在 winform 中显示 PDF

c# - 控制更改其他 UserControl 上的控制