如何在类方法调用中传递电子邮件地址参数?

时间:2019-02-09 13:08:37

标签: c#

在尝试将电子邮件地址作为参数传递给类方法时,出现错误。错误是:无法将字符串转换为system.net mail.mailaddress。有人可以解释我的问题吗?

方法调用

var fromAddress = "email1@noname.com";
var toAddress = "email2@noname.com;email3@noname.com";
SendEmail _sendEmail = new SendEmail();
_sendEmail.SendMail(fromAddress, toAddress);

类方法

public class SendEmail
    {
        public void SendMail(MailAddress fromAddress, MailAddress toAddress)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress(fromAddress);
                mail.To.Add("toAddress");
                mail.Subject = "Test Mail - 1";

                mail.IsBodyHtml = true;
                string htmlBody;

                htmlBody = "Write some HTML code here";

                mail.Body = htmlBody;

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

只需执行以下操作:

var fromAddress =  new MailAddress("email1@noname.com");
var toAddress = new MailAddress("email2@noname.com");

SendEmail _sendEmail = new SendEmail();
_sendEmail.SendMail(fromAddress, toAddress);

或将方法参数类型从MailAddress更改为string

public void SendMail(string fromAddress, string toAddress)
{
}

请不要同时做这两者!然后您将再次面临类似的问题。

此外,在SendMail()方法中,如果mail.To.Add("toAddress");传递MailAddress类型,则将mail.To.Add(toAddress);行更改为toAddress;如果传递字符串类型,则将mail.To.Add(new MailAddress(toAddress));更改为toAddress

如果您的mail.To.Add(toAddress);包含多个用逗号分隔的地址,请使用方法参数作为字符串,而不要使用foreach(string emailTo in toAddress.Split(';')) { mail.To.Add(new MailAddress(emailTo)); } 行,请执行以下操作:

background-position

答案 1 :(得分:1)

错误已清除。 SendMail方法需要两个MailAddress,并且您传递两个字符串。此外,在SendMail中,您可以像对待字符串一样处理这两个参数。因此,只需更改SendMail方法的签名即可接收两个字符串

public class SendEmail
{
    public void SendMail(string fromAddress, string toAddress)
    {
        try
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress(fromAddress);
            // Note also the error on this line. 
            // You should put each single address in the To field, 
            // not all together
            foreach(string s in toAddress.Split(';'))
               mail.To.Add(s);
            mail.Subject = "Test Mail - 1";

            mail.IsBodyHtml = true;
            string htmlBody;

            htmlBody = "Write some HTML code here";

            mail.Body = htmlBody;

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

最后,请注意,您不能在mail.To.Add方法上放置多个地址。您需要将它们一一放置,因此需要循环将输入字符串拆分成单个邮件地址以添加