I am trying to send mail with multiple attachment in c# but I am getting this error
A recipient must be specified while sending mail
Here is my code for sending mail with attachment
string to = txtto.Text; //To address
string from = "xxx@mail.com"; //From address
MailMessage message = new MailMessage();
message.From = new MailAddress(from);
if (fileuploading.HasFile)//Attaching document
{
string FileNamess = fileuploading.PostedFile.FileName;
string FileName = Path.GetFileName(fileuploading.PostedFile.FileName);
message.Attachments.Add(new System.Net.Mail.Attachment(fileuploading.PostedFile.InputStream,FileName));
}
string mailbody = editor.Text;
message.Subject = txtsubject.Text;
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("xxx@mail.com","xxxxx");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
try
{
client.Send(message);
}
catch (Exception ex)
{
throw ex;
}
You have an unused string "to". You need to add this string to recipient list message.To
.
To do that refer following snippet;
string to = txtto.Text; //To address
string from = "xxx@mail.com"; //From address
MailMessage message = new MailMessage();
message.From = new MailAddress(from);
message.To.Add(to); //Add this line to your code
For above example to work, your string to
should contain a recipient address in format "xxx@mail.com".