Send emial with attachment via web service

        public bool SendMailWithAttachmentBytes(string from, string to, string subject, string body, byte[] data, string fileName)

        {

            MailAddress _from = new MailAddress(from);

            MailAddress _to = new MailAddress(to);

            MailMessage message = new MailMessage(_from, _to);



            message.To.Add(_from);

            message.ReplyTo = new MailAddress("[email protected]");

            message.Sender = _from;

            message.CC.Add(from);

            

            message.Body = body;

            message.Subject = subject;

            message.IsBodyHtml = true;



            if (data.Length > 0)

            {

                MemoryStream ms = new MemoryStream(data);

                Attachment attachment = new Attachment(ms, fileName);

                message.Attachments.Add(attachment);

            }



            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);

            client.EnableSsl = true;



            NetworkCredential myCreds = new NetworkCredential("<GmailAccount>", "<Password>");

            client.Credentials = myCreds;



            try

            {

                client.Send(message);

            }

            catch (Exception ex)

            {

                Console.WriteLine("Exception is:" + ex.ToString());

                return false;

            }



            return true;

        }

  

你可能感兴趣的:(web Service)