MailDefinition类仅适用于ASP吗?它似乎拥有我需要的一切,但显然我需要在Web.Config文件中添加SMTP服务器信息。我想在WinForms应用程序中使用此类。这可能吗?
由于
答案 0 :(得分:1)
当然,您应该可以从WinForms应用程序中使用它。您需要将SMTP主机名传递给System.Net.Mail.SmtpClient构造函数。像这样:
using System.Net.Mail;
using System.Web.UI.WebControls;
// namespace etc
private void SendEmail()
{
string to = "to@somewhere.com,to2@somewhere.com";
MailDefinition mailDefinition = new MailDefinition();
mailDefinition.IsBodyHtml = false;
string host = "smtpserver"; // Your SMTP server name.
string from = "from@somewhere.com";
int port = -1; // Your SMTP port number. Defaults to 25.
mailDefinition.From = from;
mailDefinition.Subject = "Boring email";
mailDefinition.CC = "cc@somwhere.com,cc2@somwhere.com";
List<System.Net.Mail.Attachment> mailAttachments = new List<System.Net.Mail.Attachment>();
// Add any attachments here
using (MailMessage mailMessage = mailDefinition.CreateMailMessage(to, null, "Email body", new System.Web.UI.Control()))
{
SmtpClient smtpClient = new SmtpClient(host);
if (port != -1)
{
smtpClient.Port = port;
}
foreach (System.Net.Mail.Attachment mailAttachment in mailAttachments)
{
mailMessage.Attachments.Add(mailAttachment);
}
smtpClient.Send(mailMessage);
}
}