2
Answers

Send email - doesn't work with variables

I'm working in Visual Studio 2012 expres, C#.

THIS IS MY CODE:


=======================================================

var fromAddress = new MailAddress("[email protected]", "Info Mail");
var toAddress = new MailAddress("[email protected]", "Info Mail");
const string fromPassword = "xxxxxxx";
string subject = textBox1.Text;
const string body = "body";

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);
}

=======================================================



 
PROBLEM IS:
string subject = textBox1.Text;
const string body = "body";

BODY works, because it's defined as a text. But SUBJECT doesn't work, because it's variable. If I change SUBJECT to text, it works. But I need it as a variable.


THIS ERROR I GET:
An unhandled exception of type 'System.ArgumentException' occurred in System.dll. Additional information: The specified string is not in the form required for a subject.
Answers (2)