【问题标题】:What objects and functions can i use to send an email?我可以使用哪些对象和功能来发送电子邮件?
【发布时间】:2011-08-22 04:13:06
【问题描述】:
        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);

【问题讨论】:

标签: c# asp.net email


【解决方案1】:

您可以使用 System.Net.Mail 类。

以下是使用 gmail 的示例:Sending email in .NET through Gmail

【讨论】:

    【解决方案2】:

    您可以使用 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 发送电子邮件。

    【讨论】:

    • @Dmitry Makovetskiyd - 现在,这仅适用于 Gmail。但是,您可以添加更多选项,以便您可以从配置文件中更改端口号等。但是,其余部分相当灵活。
    猜你喜欢
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2011-03-05
    • 2011-11-15
    • 2020-03-26
    • 2012-12-12
    • 2011-05-26
    相关资源
    最近更新 更多