我在关注这个:post
几秒钟后意识到身体是一个常数,我不能给它传递一个字符串。有什么快速的方法可以稍微改变一下这段代码并得到我需要的东西吗?
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");
你去掉那个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);
}