Email发送图片

public class MailSent

    {

        MailMessage m = new MailMessage();

        SmtpClient sc = new SmtpClient();

        public string Sent()

        {

            try

            {

                //发送方

                m.From = new MailAddress("发送方Email", "名称", Encoding.UTF8);

                m.To.Add(new MailAddress("接收方Email", "名称"));//接收方

                //添加附件

                m.Attachments.Add(new Attachment(@"C:\Documents and Settings\Gong\My Documents\My Pictures\_MG_1300.jpg"));

                m.Attachments[0].ContentId = "Synair";

                m.Attachments[0].ContentDisposition.Inline = true;

                m.Attachments[0].NameEncoding = m.SubjectEncoding = m.BodyEncoding = Encoding.UTF8;

                m.Subject = "测试图片";//设置附件的 MIME 内容

                m.IsBodyHtml = true;//指示邮件正文是否为 Html 格式

                //设置附件正文

                m.Body = string.Format("<img src=\"{0}\"><br/><a href=\"{0}\" target=\"_blank\">点击在新窗口打开图片</a></img>", "cid:" + m.Attachments[0].ContentId);

                sc.Host = "smtp.gmail.com";//Gmail SMTP 服务器

                sc.Port = 587;//Gmail端口号

                sc.EnableSsl = true;//使用ssl加密

                sc.Credentials = new NetworkCredential("用户名", "密码");

                sc.Send(m);//发送

                return "发送成功";

            }

            catch (Exception ex)

            {

                return ex.Message;

            }

        }

    }

posted @ 2009-07-04 10:38  小巩  阅读(211)  评论(0)    收藏  举报