我可以使用哪些对象和函数来发送电子邮件
本文关键字:函数 电子邮件 对象 可以使 我可以 | 更新日期: 2023-09-27 17:55:37
MailMessage mail = new MailMessage();
mail.To.Add("makovetskiyd@yahoo.co.uk");
mail.From = new MailAddress("makovetskiyd@yahoo.co.uk");
mail.Subject = "Test Email";
string Body = "Welcome to CodeDigest.Com!!";
mail.Body = Body;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];//i get nuller point exception here.. what does the class configuration manager do?
smtp.Send(mail);
您可以使用 Microsoft (System.Net.Mail) 中的内置类。 例如,以下是发送电子邮件的快速简便方法:
public static void SendEmail(string messageText, string subjectText,
string fromAddress, string toAddress, string ccAddress,
string bccAddress, string hostName, string attachments,
string userName, string password)
{
try
{
string[] toAddressList = toAddress.Split(';');
string[] ccAddressList = ccAddress.Split(';');
string[] bccAddressList = bccAddress.Split(';');
string[] attachmentList = attachments.Split(';');
MailMessage mail = new MailMessage();
//Loads the To address field
foreach (string address in toAddressList)
{
if (address.Length > 0)
{
mail.To.Add(address);
}
}
//Loads the CC address field
foreach (string address in ccAddressList)
{
if (address.Length > 0)
{
mail.CC.Add(address);
}
}
//Loads the BCC address field
foreach (string address in bccAddressList)
{
if (address.Length > 0)
{
mail.Bcc.Add(address);
}
}
//Loads the attachment list
foreach (string attachment in attachmentList)
{
if (attachment.Length > 0)
{
mail.Attachments.Add(new Attachment(attachment));
}
}
mail.From = new MailAddress(fromAddress);
mail.Subject = subjectText;
string Body = messageText;
mail.Body = Body;
SmtpClient smtp = new SmtpClient();
smtp.Host = hostName;
smtp.Credentials = new System.Net.NetworkCredential(userName,password);
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(mail);
mail.Dispose();
smtp.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
在此示例中,您可以通过 Gmail 发送电子邮件。
您可以使用
System.Net.Mail类。
以下是如何使用 Gmail 执行此操作的示例:通过 Gmail 在 .NET 中发送电子邮件