-2

我在关注这个:post

几秒钟后意识到身体是一个常数,我不能给它传递一个字符串。有什么快速的方法可以稍微改变一下这段代码并得到我需要的东西吗?

4

2 回答 2

1
public void PostMessage(string body,string subject)
        {
            var fromAddress = new MailAddress("from@gmail.com", "From Name");
            var toAddress = new MailAddress("to@example.com", "To Name");
            const string fromPassword = "fromPassword";

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body,
            })
            {
                smtp.Send(message);
            }
        }

你可以这样称呼它:

PostMessage("MAH BODY", "SUBJECT");
于 2013-06-01T10:29:46.443 回答
0

你去掉那个const位...

const string body = "Body";

变成:

string body = bodyPassedIn; //where bodyPassedIn = is being passed into the method

您甚至根本不需要该变量:

using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = bodyPassedIn // here!
                     })
{
    smtp.Send(message);
}
于 2013-06-01T10:28:22.183 回答