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

时间:2013-05-09 11:30:35

标签: c# winforms email input send

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

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

1 个答案:

答案 0 :(得分:1)

以下代码用于使用自定义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.");
        }
    }
}

希望这有帮助!