邮件内容调用图片格式为:<img src=\"cid:Email001\">
html
发送邮件的服务端代码为:程序员
SmtpClient 发送邮件的对象 //代码省略web
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); mailMessage.From="发送者邮箱"; mailMessage.To.Add("收件人邮件列表"); mailMessage.CC.Add("抄送人邮件列表"); mailMessage.Subject = subject; AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(content,null,"text/html"); LinkedResource lrImage = new LinkedResource("a.jpg","image/gif"); lrImage.ContentId = "Email001"; htmlBody.LinkedResources.Add(lrImage); mailMessage.AlternateViews.Add(htmlBody); SmtpClient.Send(mailMessage);
SmtpClient smtp = new SmtpClient(); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Host = "smtp.163.com"; smtp.Credentials = new NetworkCredential("outofmemory", "**"); MailMessage mm = new MailMessage(); mm.From = new MailAddress("outofmemory@163.com", "无敌outofmemory测试"); mm.To.Add("outofmemory@vip.qq.com"); mm.Subject = "发送带图片邮件"; string plainTextBody = "若是你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容"; mm.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain")); ////HTML格式邮件的内容 string htmlBodyContent = "若是你的看到这个, 说明你是在以 HTML 格式查看邮件 "; htmlBodyContent += "为程序员服务 "; //注意此处嵌入的图片资源 AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html"); LinkedResource lrImage = new LinkedResource(@"d:\1.jpg", "image/gif"); lrImage.ContentId = "weblogo"; //此处的ContentId 对应 htmlBodyContent 内容中的 cid: ,若是设置不正确,请不会显示图片 htmlBody.LinkedResources.Add(lrImage); mm.AlternateViews.Add(htmlBody); ////要求回执的标志 mm.Headers.Add("Disposition-Notification-To", "abc@163.com"); ////自定义邮件头 mm.Headers.Add("X-Website", "http://outofmemory.cn/"); ////针对 LOTUS DOMINO SERVER,插入回执头 mm.Headers.Add("ReturnReceipt", "1"); mm.Priority = MailPriority.Normal; //优先级 mm.ReplyTo = new MailAddress("outofmemory@163.com", "我本身"); ////若是发送失败,SMTP 服务器将发送 失败邮件告诉我 mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; ////异步发送完成时的处理事件 smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted); ////开始异步发送 smtp.SendAsync(mm, null); void smtp_SendCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Cancelled) { MessageBox.Show("发送被取消"); } else { if (e.Error == null) { MessageBox.Show("发送成功"); } else { MessageBox.Show("发送失败: " + e.Error.Message); } } }