我正在开发一个简单的 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.");
}
}
}
希望这可以帮助!