电子邮件发送:类库

时间:2016-06-30 12:24:47

标签: c# .net email

我写了一个简单的发送电子邮件的课程。 (.NET 4.5,VS 2012)。我想改进这个类来查找错误并附加文件。

public class MyEmailClass
    {
        SmtpClient client = new SmtpClient();

        public MyEmailClass()
        {
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("myaddress@gmail.com", "mypassword");
        }

        public string sendEmail(MailAddress fromAddress, string toAddresses, string subject, string emailBody, string ccAddresses = "", string bccAddresses = "")
        {
            client.Timeout = 100000;//more than 1 s

            MailMessage message = new MailMessage();

            message.From = fromAddress;
            message.To.Add(toAddresses);
            message.Subject = subject;
            message.Body = emailBody;
            if (ccAddresses != "") { message.CC.Add(ccAddresses); }
            if (bccAddresses != "") { message.Bcc.Add(bccAddresses); }

            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            client.Send(message);
            // Clean up.
            message.Dispose();

            return "Mail Sent";
        }

    }

任何建议和建议将不胜感激。 (如果我可以为此目的使用任何预先编写的课程/包,建议我。)

谢谢你!红宝石。

0 个答案:

没有答案