繁体   English   中英

发送输入到电子邮件? WinForm C#

[英]Sending inputs to email? WinForm C#

我正在一个简单的WinForm项目中,我有一个文本框,用户可以输入他的名字。 当他单击按钮时,我希望能够将此输入发送到电子邮件地址或类似的地址。

这可能吗? 如果是这样,我该怎么办?

以下代码用于通过自定义SMTP客户端发送电子邮件:

using System;
using System.Net.Mail;

    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.customsmtp.com");

                mail.From = new MailAddress("fromEmail@fromemail.com");
                mail.To.Add("toemail@toemail.com");
                mail.Subject = "Your Subject";
                mail.Body = "Your Textbox Here!";
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Seems some problem!");
            }

            Console.WriteLine("Email sent successfully!");
            Console.ReadLine();
        }

    }

下面的示例使用您的gmail用户名和密码从您的gmail帐户发送电子邮件:

using System;
using System.Net;
using System.Net.Mail;

namespace GMailSample
{
    class SimpleSmtpSend
    {
        static void Main(string[] args)
        {
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);           
            client.EnableSsl = true;
            MailAddress from = new MailAddress("YourGmailUserName@gmail.com", "[ Your full name here]");           
            MailAddress to = new MailAddress("your recipient e-mail address", "Your recepient name");
            MailMessage message = new MailMessage(from, to);
            message.Body = "This is a test e-mail message sent using gmail as a relay server ";
            message.Subject = "Gmail test email with SSL and Credentials";
            NetworkCredential myCreds = new NetworkCredential("YourGmailUserName@gmail.com", "YourPassword", "");           
            client.Credentials = myCreds;
            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception is:" + ex.ToString());
            }
            Console.WriteLine("Goodbye.");
        }
    }
}

希望这可以帮助!

暂无
暂无

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

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